blob: 94ee9675b5b0c68c83dc2fdbd6343fc8e56506cc [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"
dea31012005-04-17 16:05:31 -050040
41/* AlpaArray for assignment of scsid for scan-down and bind_method */
42static uint8_t lpfcAlpaArray[] = {
43 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
44 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
45 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
46 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
47 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
48 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
49 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
50 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
51 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
52 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
53 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
54 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
55 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
56};
57
James Smart2e0fef82007-06-17 19:56:36 -050058static void lpfc_disc_timeout_handler(struct lpfc_vport *);
dea31012005-04-17 16:05:31 -050059
James Smartc01f3202006-08-18 17:47:08 -040060void
61lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050062{
James Smartc01f3202006-08-18 17:47:08 -040063 struct lpfc_rport_data *rdata;
64 struct lpfc_nodelist * ndlp;
65 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050066
James Smartc01f3202006-08-18 17:47:08 -040067 rdata = rport->dd_data;
68 ndlp = rdata->pnode;
69
70 if (!ndlp) {
71 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
72 printk(KERN_ERR "Cannot find remote node"
73 " to terminate I/O Data x%x\n",
74 rport->port_id);
dea31012005-04-17 16:05:31 -050075 return;
76 }
77
James Smart2e0fef82007-06-17 19:56:36 -050078 phba = ndlp->vport->phba;
James Smart1a169682006-03-07 15:04:06 -050079
James Smartc01f3202006-08-18 17:47:08 -040080 if (ndlp->nlp_sid != NLP_NO_SID) {
81 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
82 ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
83 }
James Smartc01f3202006-08-18 17:47:08 -040084
85 return;
86}
87
88/*
89 * This function will be called when dev_loss_tmo fire.
90 */
91void
92lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
93{
94 struct lpfc_rport_data *rdata;
95 struct lpfc_nodelist * ndlp;
96 uint8_t *name;
97 int warn_on = 0;
98 struct lpfc_hba *phba;
James Smart2e0fef82007-06-17 19:56:36 -050099 struct lpfc_vport *vport;
James Smart92d7f7b2007-06-17 19:56:38 -0500100 int put_node;
101 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400102
103 rdata = rport->dd_data;
104 ndlp = rdata->pnode;
105
106 if (!ndlp) {
James Smart92d7f7b2007-06-17 19:56:38 -0500107 if (rport->scsi_target_id != -1) {
James Smartc01f3202006-08-18 17:47:08 -0400108 printk(KERN_ERR "Cannot find remote node"
James Smart92d7f7b2007-06-17 19:56:38 -0500109 " for rport in dev_loss_tmo_callbk x%x\n",
110 rport->port_id);
111 }
James Smartc01f3202006-08-18 17:47:08 -0400112 return;
113 }
114
James Smart92d7f7b2007-06-17 19:56:38 -0500115 if (ndlp->nlp_type & NLP_FABRIC) {
116 /* We will clean up these Nodes in linkup */
117 put_node = rdata->pnode != NULL;
118 put_rport = ndlp->rport != NULL;
119 rdata->pnode = NULL;
120 ndlp->rport = NULL;
121 if (put_node)
122 lpfc_nlp_put(ndlp);
123 if (put_rport)
124 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400125 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500126 }
James Smart82085712007-04-25 09:52:41 -0400127
James Smartc01f3202006-08-18 17:47:08 -0400128 name = (uint8_t *)&ndlp->nlp_portname;
James Smart2e0fef82007-06-17 19:56:36 -0500129 vport = ndlp->vport;
130 phba = vport->phba;
dea31012005-04-17 16:05:31 -0500131
James Smart92d7f7b2007-06-17 19:56:38 -0500132 if (!(vport->load_flag & FC_UNLOADING) &&
133 ndlp->nlp_state == NLP_STE_MAPPED_NODE)
134 return;
135
136
dea31012005-04-17 16:05:31 -0500137 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400138 warn_on = 1;
dea31012005-04-17 16:05:31 -0500139 /* flush the target */
140 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -0500141 ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500142 }
James Smart2e0fef82007-06-17 19:56:36 -0500143 if (vport->load_flag & FC_UNLOADING)
James Smartc01f3202006-08-18 17:47:08 -0400144 warn_on = 0;
145
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400146 if (warn_on) {
147 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -0500148 "%d (%d):0203 Devloss timeout on "
James Smart488d1462006-03-07 15:02:37 -0500149 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
150 "NPort x%x Data: x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500151 phba->brd_no, vport->vpi,
James Smart488d1462006-03-07 15:02:37 -0500152 *name, *(name+1), *(name+2), *(name+3),
153 *(name+4), *(name+5), *(name+6), *(name+7),
154 ndlp->nlp_DID, ndlp->nlp_flag,
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400155 ndlp->nlp_state, ndlp->nlp_rpi);
156 } else {
157 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -0500158 "%d (%d):0204 Devloss timeout on "
James Smart488d1462006-03-07 15:02:37 -0500159 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
160 "NPort x%x Data: x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500161 phba->brd_no, vport->vpi,
James Smart488d1462006-03-07 15:02:37 -0500162 *name, *(name+1), *(name+2), *(name+3),
163 *(name+4), *(name+5), *(name+6), *(name+7),
164 ndlp->nlp_DID, ndlp->nlp_flag,
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400165 ndlp->nlp_state, ndlp->nlp_rpi);
166 }
167
James Smart2e0fef82007-06-17 19:56:36 -0500168 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400169 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400170 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
171 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500172 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart1dcb58e2007-04-25 09:51:30 -0400173 else {
James Smart92d7f7b2007-06-17 19:56:38 -0500174 put_node = rdata->pnode != NULL;
175 put_rport = ndlp->rport != NULL;
James Smart1dcb58e2007-04-25 09:51:30 -0400176 rdata->pnode = NULL;
177 ndlp->rport = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -0500178 if (put_node)
179 lpfc_nlp_put(ndlp);
180 if (put_rport)
181 put_device(&rport->dev);
James Smart1dcb58e2007-04-25 09:51:30 -0400182 }
James Smart92d7f7b2007-06-17 19:56:38 -0500183 return;
184}
James Smartc01f3202006-08-18 17:47:08 -0400185
James Smart92d7f7b2007-06-17 19:56:38 -0500186
187void
188lpfc_worker_wake_up(struct lpfc_hba *phba)
189{
190 wake_up(phba->work_wait);
dea31012005-04-17 16:05:31 -0500191 return;
192}
193
194static void
James Smart2e0fef82007-06-17 19:56:36 -0500195lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500196{
197 struct lpfc_work_evt *evtp = NULL;
198 struct lpfc_nodelist *ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -0500199 struct lpfc_vport *vport;
dea31012005-04-17 16:05:31 -0500200 int free_evt;
201
James Smart2e0fef82007-06-17 19:56:36 -0500202 spin_lock_irq(&phba->hbalock);
203 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500204 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
205 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500206 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500207 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500208 switch (evtp->evt) {
James Smart92d7f7b2007-06-17 19:56:38 -0500209 case LPFC_EVT_DEV_LOSS:
210 free_evt = 0; /* evt is part of ndlp */
211 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
212 vport = ndlp->vport;
213 if (!vport)
214 break;
215 if (!(vport->load_flag & FC_UNLOADING) &&
216 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
217 !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
218 lpfc_disc_state_machine(vport, ndlp, NULL,
219 NLP_EVT_DEVICE_RM);
220 }
221 break;
dea31012005-04-17 16:05:31 -0500222 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500223 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500224 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500225 free_evt = 0; /* evt is part of ndlp */
dea31012005-04-17 16:05:31 -0500226 break;
227 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500228 if (phba->link_state < LPFC_LINK_DOWN)
229 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500230 else
James Smart2e0fef82007-06-17 19:56:36 -0500231 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500232 complete((struct completion *)(evtp->evt_arg2));
233 break;
James Smart46fa3112007-04-25 09:51:45 -0400234 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500235 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400236 lpfc_offline_prep(phba);
237 *(int *)(evtp->evt_arg1) = 0;
238 complete((struct completion *)(evtp->evt_arg2));
239 break;
240 case LPFC_EVT_OFFLINE:
241 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500242 lpfc_sli_brdrestart(phba);
243 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400244 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
245 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500246 complete((struct completion *)(evtp->evt_arg2));
247 break;
248 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400249 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500250 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500251 lpfc_sli_brdreset(phba);
252 lpfc_hba_down_post(phba);
253 *(int *)(evtp->evt_arg1) =
254 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400255 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500256 complete((struct completion *)(evtp->evt_arg2));
257 break;
258 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400259 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500260 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500261 = (phba->pport->stopped)
262 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400263 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500264 complete((struct completion *)(evtp->evt_arg2));
265 break;
266 }
267 if (free_evt)
268 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500269 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500270 }
James Smart2e0fef82007-06-17 19:56:36 -0500271 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500272
273}
274
275static void
James Smart2e0fef82007-06-17 19:56:36 -0500276lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500277{
278 struct lpfc_sli_ring *pring;
James Smart2e0fef82007-06-17 19:56:36 -0500279 uint32_t ha_copy, control, work_port_events;
280 struct lpfc_vport *vport;
James Smarted957682007-06-17 19:56:37 -0500281 int i;
dea31012005-04-17 16:05:31 -0500282
James Smart2e0fef82007-06-17 19:56:36 -0500283 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500284 ha_copy = phba->work_ha;
285 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500286 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500287
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500288 if (ha_copy & HA_ERATT)
dea31012005-04-17 16:05:31 -0500289 lpfc_handle_eratt(phba);
290
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500291 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500292 lpfc_sli_handle_mb_event(phba);
293
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500294 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500295 lpfc_handle_latt(phba);
296
James Smart2e0fef82007-06-17 19:56:36 -0500297 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500298 list_for_each_entry(vport, &phba->port_list, listentry) {
299 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
300
301 if (!scsi_host_get(shost)) {
302 continue;
303 }
304 spin_unlock_irq(&phba->hbalock);
305 work_port_events = vport->work_port_events;
306
307 if (work_port_events & WORKER_DISC_TMO)
308 lpfc_disc_timeout_handler(vport);
309
310 if (work_port_events & WORKER_ELS_TMO)
311 lpfc_els_timeout_handler(vport);
312
313 if (work_port_events & WORKER_MBOX_TMO)
314 lpfc_mbox_timeout_handler(phba);
315
316 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
317 lpfc_unblock_fabric_iocbs(phba);
318
319 if (work_port_events & WORKER_FDMI_TMO)
320 lpfc_fdmi_timeout_handler(vport);
321
322 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
323 lpfc_ramp_down_queue_handler(phba);
324
325 if (work_port_events & WORKER_RAMP_UP_QUEUE)
326 lpfc_ramp_up_queue_handler(phba);
327
328 spin_lock_irq(&vport->work_port_lock);
329 vport->work_port_events &= ~work_port_events;
330 spin_unlock_irq(&vport->work_port_lock);
331 scsi_host_put(shost);
332 spin_lock_irq(&phba->hbalock);
333 }
James Smart2e0fef82007-06-17 19:56:36 -0500334 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500335
336 for (i = 0; i < phba->sli.num_rings; i++, ha_copy >>= 4) {
337 pring = &phba->sli.ring[i];
338 if ((ha_copy & HA_RXATT)
339 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
340 if (pring->flag & LPFC_STOP_IOCB_MASK) {
341 pring->flag |= LPFC_DEFERRED_RING_EVENT;
342 } else {
343 lpfc_sli_handle_slow_ring_event(phba, pring,
344 (ha_copy &
345 HA_RXMASK));
346 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
347 }
348 /*
349 * Turn on Ring interrupts
350 */
James Smart2e0fef82007-06-17 19:56:36 -0500351 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500352 control = readl(phba->HCregaddr);
353 control |= (HC_R0INT_ENA << i);
354 writel(control, phba->HCregaddr);
355 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500356 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500357 }
358 }
359
James Smart2e0fef82007-06-17 19:56:36 -0500360 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500361}
362
363static int
James Smart2e0fef82007-06-17 19:56:36 -0500364check_work_wait_done(struct lpfc_hba *phba)
365{
James Smart92d7f7b2007-06-17 19:56:38 -0500366 struct lpfc_vport *vport;
367 struct lpfc_sli_ring *pring;
368 int i, rc = 0;
James Smarted957682007-06-17 19:56:37 -0500369
James Smart2e0fef82007-06-17 19:56:36 -0500370 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500371 list_for_each_entry(vport, &phba->port_list, listentry) {
372 if (vport->work_port_events) {
373 rc = 1;
374 goto exit;
375 }
376 }
James Smart2e0fef82007-06-17 19:56:36 -0500377
James Smart92d7f7b2007-06-17 19:56:38 -0500378 if (phba->work_ha || (!list_empty(&phba->work_list)) ||
379 kthread_should_stop()) {
James Smart2e0fef82007-06-17 19:56:36 -0500380 rc = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500381 goto exit;
382 }
383 for (i = 0; i < phba->sli.num_rings; i++) {
384 pring = &phba->sli.ring[i];
385 if (pring->flag & LPFC_DEFERRED_RING_EVENT) {
386 rc = 1;
387 goto exit;
388 }
389 }
390exit:
391 if (rc)
392 phba->work_found++;
393 else
394 phba->work_found = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500395
396 spin_unlock_irq(&phba->hbalock);
397 return rc;
dea31012005-04-17 16:05:31 -0500398}
399
James Smart92d7f7b2007-06-17 19:56:38 -0500400
dea31012005-04-17 16:05:31 -0500401int
402lpfc_do_work(void *p)
403{
404 struct lpfc_hba *phba = p;
405 int rc;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800406 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
dea31012005-04-17 16:05:31 -0500407
408 set_user_nice(current, -20);
409 phba->work_wait = &work_waitq;
James Smart92d7f7b2007-06-17 19:56:38 -0500410 phba->work_found = 0;
dea31012005-04-17 16:05:31 -0500411
412 while (1) {
413
414 rc = wait_event_interruptible(work_waitq,
James Smart92d7f7b2007-06-17 19:56:38 -0500415 check_work_wait_done(phba));
416
dea31012005-04-17 16:05:31 -0500417 BUG_ON(rc);
418
419 if (kthread_should_stop())
420 break;
421
422 lpfc_work_done(phba);
423
James Smart92d7f7b2007-06-17 19:56:38 -0500424 /* If there is alot of slow ring work, like during link up
425 * check_work_wait_done() may cause this thread to not give
426 * up the CPU for very long periods of time. This may cause
427 * soft lockups or other problems. To avoid these situations
428 * give up the CPU here after LPFC_MAX_WORKER_ITERATION
429 * consecutive iterations.
430 */
431 if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
432 phba->work_found = 0;
433 schedule();
434 }
dea31012005-04-17 16:05:31 -0500435 }
436 phba->work_wait = NULL;
437 return 0;
438}
439
440/*
441 * This is only called to handle FC worker events. Since this a rare
442 * occurance, we allocate a struct lpfc_work_evt structure here instead of
443 * embedding it in the IOCB.
444 */
445int
James Smart2e0fef82007-06-17 19:56:36 -0500446lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500447 uint32_t evt)
448{
449 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500450 unsigned long flags;
dea31012005-04-17 16:05:31 -0500451
452 /*
453 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
454 * be queued to worker thread for processing
455 */
James Smart92d7f7b2007-06-17 19:56:38 -0500456 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500457 if (!evtp)
458 return 0;
459
460 evtp->evt_arg1 = arg1;
461 evtp->evt_arg2 = arg2;
462 evtp->evt = evt;
463
James Smarted957682007-06-17 19:56:37 -0500464 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400465 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea31012005-04-17 16:05:31 -0500466 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -0500467 lpfc_worker_wake_up(phba);
James Smarted957682007-06-17 19:56:37 -0500468 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500469
470 return 1;
471}
472
James Smart92d7f7b2007-06-17 19:56:38 -0500473void
474lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
475{
476 struct lpfc_hba *phba = vport->phba;
477 struct lpfc_nodelist *ndlp, *next_ndlp;
478 int rc;
479
480 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
481 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
482 continue;
483
484 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN)
485 lpfc_unreg_rpi(vport, ndlp);
486
487 /* Leave Fabric nodes alone on link down */
488 if (!remove && ndlp->nlp_type & NLP_FABRIC)
489 continue;
490 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
491 remove
492 ? NLP_EVT_DEVICE_RM
493 : NLP_EVT_DEVICE_RECOVERY);
494 }
495 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
496 lpfc_mbx_unreg_vpi(vport);
497 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
498 }
499}
500
501static void
502lpfc_linkdown_port(struct lpfc_vport *vport)
503{
504 struct lpfc_nodelist *ndlp, *next_ndlp;
505 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
506
507 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
508
509 /* Cleanup any outstanding RSCN activity */
510 lpfc_els_flush_rscn(vport);
511
512 /* Cleanup any outstanding ELS commands */
513 lpfc_els_flush_cmd(vport);
514
515 lpfc_cleanup_rpis(vport, 0);
516
517 /* free any ndlp's on unused list */
518 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
519 /* free any ndlp's in unused state */
520 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
521 lpfc_drop_node(vport, ndlp);
522
523 /* Turn off discovery timer if its running */
524 lpfc_can_disctmo(vport);
525}
526
dea31012005-04-17 16:05:31 -0500527int
James Smart685f0bf2007-04-25 09:53:08 -0400528lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500529{
James Smart2e0fef82007-06-17 19:56:36 -0500530 struct lpfc_vport *vport = phba->pport;
531 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500532 struct lpfc_vport *port_iterator;
James Smart685f0bf2007-04-25 09:53:08 -0400533 LPFC_MBOXQ_t *mb;
dea31012005-04-17 16:05:31 -0500534
James Smart2e0fef82007-06-17 19:56:36 -0500535 if (phba->link_state == LPFC_LINK_DOWN) {
536 return 0;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500537 }
James Smart2e0fef82007-06-17 19:56:36 -0500538 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500539 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500540 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500541 phba->pport->fc_flag &= ~FC_LBIT;
542 }
James Smart2e0fef82007-06-17 19:56:36 -0500543 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500544
James Smart92d7f7b2007-06-17 19:56:38 -0500545 list_for_each_entry(port_iterator, &phba->port_list, listentry) {
546
547 /* Issue a LINK DOWN event to all nodes */
548 lpfc_linkdown_port(port_iterator);
549 }
James Smartd2873e42006-08-18 17:46:43 -0400550
dea31012005-04-17 16:05:31 -0500551 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500552 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
553 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500554 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500555 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500556 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
dea31012005-04-17 16:05:31 -0500557 if (lpfc_sli_issue_mbox(phba, mb, (MBX_NOWAIT | MBX_STOP_IOCB))
558 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500559 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500560 }
561 }
562
dea31012005-04-17 16:05:31 -0500563 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500564 if (phba->pport->fc_flag & FC_PT2PT) {
565 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500566 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
567 if (mb) {
dea31012005-04-17 16:05:31 -0500568 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500569 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500570 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500571 if (lpfc_sli_issue_mbox(phba, mb,
572 (MBX_NOWAIT | MBX_STOP_IOCB))
dea31012005-04-17 16:05:31 -0500573 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500574 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500575 }
576 }
James Smart2e0fef82007-06-17 19:56:36 -0500577 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500578 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500579 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500580 }
James Smart2e0fef82007-06-17 19:56:36 -0500581
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500582 return 0;
dea31012005-04-17 16:05:31 -0500583}
584
James Smart92d7f7b2007-06-17 19:56:38 -0500585static void
586lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500587{
James Smart92d7f7b2007-06-17 19:56:38 -0500588 struct lpfc_nodelist *ndlp;
589
590 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
591 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
592 continue;
593
594 if (ndlp->nlp_type & NLP_FABRIC) {
595 /* On Linkup its safe to clean up the ndlp
596 * from Fabric connections.
597 */
598 if (ndlp->nlp_DID != Fabric_DID)
599 lpfc_unreg_rpi(vport, ndlp);
600 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
601 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
602 /* Fail outstanding IO now since device is
603 * marked for PLOGI.
604 */
605 lpfc_unreg_rpi(vport, ndlp);
606 }
607 }
608}
609
610static void
611lpfc_linkup_port(struct lpfc_vport *vport)
612{
613 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500614 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -0500615 struct lpfc_hba *phba = vport->phba;
616
617 if ((vport->load_flag & FC_UNLOADING) != 0)
618 return;
619
620 /* If NPIV is not enabled, only bring the physical port up */
621 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
622 (vport != phba->pport))
623 return;
dea31012005-04-17 16:05:31 -0500624
James Smart2e0fef82007-06-17 19:56:36 -0500625 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400626
James Smart2e0fef82007-06-17 19:56:36 -0500627 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500628 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
629 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
630 vport->fc_flag |= FC_NDISC_ACTIVE;
631 vport->fc_ns_retry = 0;
632 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500633
James Smart92d7f7b2007-06-17 19:56:38 -0500634 if (vport->fc_flag & FC_LBIT)
635 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500636
James Smart92d7f7b2007-06-17 19:56:38 -0500637 /* free any ndlp's in unused state */
James Smart2e0fef82007-06-17 19:56:36 -0500638 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart92d7f7b2007-06-17 19:56:38 -0500639 nlp_listp)
James Smart685f0bf2007-04-25 09:53:08 -0400640 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
James Smart2e0fef82007-06-17 19:56:36 -0500641 lpfc_drop_node(vport, ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500642}
643
644static int
645lpfc_linkup(struct lpfc_hba *phba)
646{
647 struct lpfc_vport *vport;
648
649 phba->link_state = LPFC_LINK_UP;
650
651 /* Unblock fabric iocbs if they are blocked */
652 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
653 del_timer_sync(&phba->fabric_block_timer);
654
655 list_for_each_entry(vport, &phba->port_list, listentry) {
656 lpfc_linkup_port(vport);
James Smart685f0bf2007-04-25 09:53:08 -0400657 }
James Smart92d7f7b2007-06-17 19:56:38 -0500658 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
659 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500660
661 return 0;
662}
663
664/*
665 * This routine handles processing a CLEAR_LA mailbox
666 * command upon completion. It is setup in the LPFC_MBOXQ
667 * as the completion routine when the command is
668 * handed off to the SLI layer.
669 */
670void
James Smart2e0fef82007-06-17 19:56:36 -0500671lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500672{
James Smart2e0fef82007-06-17 19:56:36 -0500673 struct lpfc_vport *vport = pmb->vport;
674 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
675 struct lpfc_sli *psli = &phba->sli;
676 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500677 uint32_t control;
678
dea31012005-04-17 16:05:31 -0500679 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500680 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500681 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
682 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
683
684 /* Check for error */
685 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500686 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
dea31012005-04-17 16:05:31 -0500687 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smart92d7f7b2007-06-17 19:56:38 -0500688 "%d (%d):0320 CLEAR_LA mbxStatus error x%x hba "
dea31012005-04-17 16:05:31 -0500689 "state x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500690 phba->brd_no, vport->vpi, mb->mbxStatus,
691 vport->port_state);
dea31012005-04-17 16:05:31 -0500692
James Smart2e0fef82007-06-17 19:56:36 -0500693 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500694 goto out;
695 }
696
James Smart92d7f7b2007-06-17 19:56:38 -0500697 if (vport->port_type == LPFC_PHYSICAL_PORT)
698 phba->link_state = LPFC_HBA_READY;
699
700 spin_lock_irq(&phba->hbalock);
701 psli->sli_flag |= LPFC_PROCESS_LA;
702 control = readl(phba->HCregaddr);
703 control |= HC_LAINT_ENA;
704 writel(control, phba->HCregaddr);
705 readl(phba->HCregaddr); /* flush */
706 spin_unlock_irq(&phba->hbalock);
707 return;
dea31012005-04-17 16:05:31 -0500708
James Smart2e0fef82007-06-17 19:56:36 -0500709 vport->num_disc_nodes = 0;
710 /* go thru NPR nodes and issue ELS PLOGIs */
711 if (vport->fc_npr_cnt)
712 lpfc_els_disc_plogi(vport);
713
714 if (!vport->num_disc_nodes) {
715 spin_lock_irq(shost->host_lock);
716 vport->fc_flag &= ~FC_NDISC_ACTIVE;
717 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500718 }
719
James Smart2e0fef82007-06-17 19:56:36 -0500720 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -0500721
722out:
723 /* Device Discovery completes */
James Smarted957682007-06-17 19:56:37 -0500724 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -0500725 "%d (%d):0225 Device Discovery completes\n",
726 phba->brd_no, vport->vpi);
dea31012005-04-17 16:05:31 -0500727
James Smart2e0fef82007-06-17 19:56:36 -0500728 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500729
James Smart2e0fef82007-06-17 19:56:36 -0500730 spin_lock_irq(shost->host_lock);
731 vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
732 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500733
734 del_timer_sync(&phba->fc_estabtmo);
735
James Smart2e0fef82007-06-17 19:56:36 -0500736 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500737
738 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500739
740 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500741 psli->sli_flag |= LPFC_PROCESS_LA;
742 control = readl(phba->HCregaddr);
743 control |= HC_LAINT_ENA;
744 writel(control, phba->HCregaddr);
745 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500746 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500747
748 return;
749}
750
James Smart2e0fef82007-06-17 19:56:36 -0500751
dea31012005-04-17 16:05:31 -0500752static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500753lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500754{
James Smart2e0fef82007-06-17 19:56:36 -0500755 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500756
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500757 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500758 goto out;
dea31012005-04-17 16:05:31 -0500759
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500760 mempool_free(pmb, phba->mbox_mem_pool);
761
762 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500763 vport->fc_flag & FC_PUBLIC_LOOP &&
764 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500765 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500766 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500767 * LPFC_LOCAL_CFG_LINK while waiting for FAN
768 */
James Smart2e0fef82007-06-17 19:56:36 -0500769 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500770 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500771 }
dea31012005-04-17 16:05:31 -0500772
James Smart2e0fef82007-06-17 19:56:36 -0500773 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500774 * LPFC_FLOGI while waiting for FLOGI cmpl
775 */
James Smart92d7f7b2007-06-17 19:56:38 -0500776 if (vport->port_state != LPFC_FLOGI) {
777 vport->port_state = LPFC_FLOGI;
778 lpfc_set_disctmo(vport);
779 lpfc_initial_flogi(vport);
780 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500781 return;
dea31012005-04-17 16:05:31 -0500782
783out:
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500784 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smart92d7f7b2007-06-17 19:56:38 -0500785 "%d (%d):0306 CONFIG_LINK mbxStatus error x%x "
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500786 "HBA state x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500787 phba->brd_no, vport->vpi, pmb->mb.mbxStatus,
788 vport->port_state);
789
790 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500791
792 lpfc_linkdown(phba);
793
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500794 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -0500795 "%d (%d):0200 CONFIG_LINK bad hba state x%x\n",
796 phba->brd_no, vport->vpi, vport->port_state);
dea31012005-04-17 16:05:31 -0500797
James Smart92d7f7b2007-06-17 19:56:38 -0500798 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500799 return;
800}
801
802static void
James Smart2e0fef82007-06-17 19:56:36 -0500803lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500804{
dea31012005-04-17 16:05:31 -0500805 MAILBOX_t *mb = &pmb->mb;
806 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500807 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500808
809
810 /* Check for error */
811 if (mb->mbxStatus) {
812 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
813 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smart92d7f7b2007-06-17 19:56:38 -0500814 "%d (%d):0319 READ_SPARAM mbxStatus error x%x "
dea31012005-04-17 16:05:31 -0500815 "hba state x%x>\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500816 phba->brd_no, vport->vpi, mb->mbxStatus,
817 vport->port_state);
dea31012005-04-17 16:05:31 -0500818
819 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -0500820 goto out;
821 }
822
James Smart2e0fef82007-06-17 19:56:36 -0500823 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -0500824 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500825 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500826 u64_to_wwn(phba->cfg_soft_wwnn,
827 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400828 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500829 u64_to_wwn(phba->cfg_soft_wwpn,
830 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500831 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
832 sizeof(vport->fc_nodename));
833 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
834 sizeof(vport->fc_portname));
835 if (vport->port_type == LPFC_PHYSICAL_PORT) {
836 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
837 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
838 }
839
dea31012005-04-17 16:05:31 -0500840 lpfc_mbuf_free(phba, mp->virt, mp->phys);
841 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -0500842 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500843 return;
844
845out:
846 pmb->context1 = NULL;
847 lpfc_mbuf_free(phba, mp->virt, mp->phys);
848 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -0500849 lpfc_issue_clear_la(phba, vport);
850 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500851 return;
852}
853
854static void
James Smart92d7f7b2007-06-17 19:56:38 -0500855lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -0500856{
James Smart92d7f7b2007-06-17 19:56:38 -0500857 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500858 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -0500859 int i;
James Smart14691152006-12-02 13:34:28 -0500860 struct lpfc_dmabuf *mp;
861 int rc;
862
dea31012005-04-17 16:05:31 -0500863 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
864 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
865
James Smart92d7f7b2007-06-17 19:56:38 -0500866 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500867 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -0500868 case LA_1GHZ_LINK:
869 phba->fc_linkspeed = LA_1GHZ_LINK;
870 break;
871 case LA_2GHZ_LINK:
872 phba->fc_linkspeed = LA_2GHZ_LINK;
873 break;
874 case LA_4GHZ_LINK:
875 phba->fc_linkspeed = LA_4GHZ_LINK;
876 break;
877 case LA_8GHZ_LINK:
878 phba->fc_linkspeed = LA_8GHZ_LINK;
879 break;
880 default:
881 phba->fc_linkspeed = LA_UNKNW_LINK;
882 break;
dea31012005-04-17 16:05:31 -0500883 }
884
885 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -0500886 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -0500887
888 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500889 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -0500890
James Smart92d7f7b2007-06-17 19:56:38 -0500891 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -0500892 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -0500893 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500894
James Smart2e0fef82007-06-17 19:56:36 -0500895 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -0500896 i = la->un.lilpBde64.tus.f.bdeSize;
897
898 if (i == 0) {
899 phba->alpa_map[0] = 0;
900 } else {
901 if (phba->cfg_log_verbose & LOG_LINK_EVENT) {
902 int numalpa, j, k;
903 union {
904 uint8_t pamap[16];
905 struct {
906 uint32_t wd1;
907 uint32_t wd2;
908 uint32_t wd3;
909 uint32_t wd4;
910 } pa;
911 } un;
912 numalpa = phba->alpa_map[0];
913 j = 0;
914 while (j < numalpa) {
915 memset(un.pamap, 0, 16);
916 for (k = 1; j < numalpa; k++) {
917 un.pamap[k - 1] =
918 phba->alpa_map[j + 1];
919 j++;
920 if (k == 16)
921 break;
922 }
923 /* Link Up Event ALPA map */
924 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -0500925 KERN_WARNING,
926 LOG_LINK_EVENT,
927 "%d:1304 Link Up Event "
928 "ALPA map Data: x%x "
929 "x%x x%x x%x\n",
930 phba->brd_no,
931 un.pa.wd1, un.pa.wd2,
932 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -0500933 }
934 }
935 }
936 } else {
James Smart92d7f7b2007-06-17 19:56:38 -0500937 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
938 if (phba->max_vpi && lpfc_npiv_enable &&
939 (phba->sli_rev == 3))
940 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
941 }
James Smart2e0fef82007-06-17 19:56:36 -0500942 vport->fc_myDID = phba->fc_pref_DID;
943 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500944 }
James Smart92d7f7b2007-06-17 19:56:38 -0500945 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500946
947 lpfc_linkup(phba);
948 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -0500949 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -0500950 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -0500951 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart14691152006-12-02 13:34:28 -0500952 rc = lpfc_sli_issue_mbox(phba, sparam_mbox,
James Smart92d7f7b2007-06-17 19:56:38 -0500953 (MBX_NOWAIT | MBX_STOP_IOCB));
James Smart14691152006-12-02 13:34:28 -0500954 if (rc == MBX_NOT_FINISHED) {
955 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
956 lpfc_mbuf_free(phba, mp->virt, mp->phys);
957 kfree(mp);
958 mempool_free(sparam_mbox, phba->mbox_mem_pool);
959 if (cfglink_mbox)
960 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500961 goto out;
James Smart14691152006-12-02 13:34:28 -0500962 }
dea31012005-04-17 16:05:31 -0500963 }
964
965 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -0500966 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -0500967 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -0500968 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500969 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart14691152006-12-02 13:34:28 -0500970 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox,
James Smart92d7f7b2007-06-17 19:56:38 -0500971 (MBX_NOWAIT | MBX_STOP_IOCB));
972 if (rc != MBX_NOT_FINISHED)
973 return;
974 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500975 }
James Smart92d7f7b2007-06-17 19:56:38 -0500976out:
977 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
978 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
979 "%d (%d):0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
980 phba->brd_no, vport->vpi,
981 vport->port_state, sparam_mbox, cfglink_mbox);
982
983 lpfc_issue_clear_la(phba, vport);
984 return;
dea31012005-04-17 16:05:31 -0500985}
986
987static void
James Smart2e0fef82007-06-17 19:56:36 -0500988lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
989{
dea31012005-04-17 16:05:31 -0500990 uint32_t control;
991 struct lpfc_sli *psli = &phba->sli;
992
993 lpfc_linkdown(phba);
994
995 /* turn on Link Attention interrupts - no CLEAR_LA needed */
James Smart2e0fef82007-06-17 19:56:36 -0500996 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500997 psli->sli_flag |= LPFC_PROCESS_LA;
998 control = readl(phba->HCregaddr);
999 control |= HC_LAINT_ENA;
1000 writel(control, phba->HCregaddr);
1001 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001002 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001003}
1004
1005/*
1006 * This routine handles processing a READ_LA mailbox
1007 * command upon completion. It is setup in the LPFC_MBOXQ
1008 * as the completion routine when the command is
1009 * handed off to the SLI layer.
1010 */
1011void
James Smart2e0fef82007-06-17 19:56:36 -05001012lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001013{
James Smart2e0fef82007-06-17 19:56:36 -05001014 struct lpfc_vport *vport = pmb->vport;
1015 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001016 READ_LA_VAR *la;
1017 MAILBOX_t *mb = &pmb->mb;
1018 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1019
1020 /* Check for error */
1021 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001022 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
dea31012005-04-17 16:05:31 -05001023 "%d:1307 READ_LA mbox error x%x state x%x\n",
James Smarted957682007-06-17 19:56:37 -05001024 phba->brd_no, mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001025 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001026 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001027 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1028 }
1029
1030 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1031
1032 memcpy(&phba->alpa_map[0], mp->virt, 128);
1033
James Smart2e0fef82007-06-17 19:56:36 -05001034 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001035 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001036 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001037 else
James Smart2e0fef82007-06-17 19:56:36 -05001038 vport->fc_flag &= ~FC_BYPASSED_MODE;
1039 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001040
dea31012005-04-17 16:05:31 -05001041 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001042 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001043 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001044 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001045 if (phba->fc_eventTag != 0)
1046 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001047 }
dea31012005-04-17 16:05:31 -05001048
1049 phba->fc_eventTag = la->eventTag;
1050
1051 if (la->attType == AT_LINK_UP) {
1052 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001053 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart5b8bd0c2007-04-25 09:52:49 -04001054 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1055 "%d:1306 Link Up Event in loop back mode "
1056 "x%x received Data: x%x x%x x%x x%x\n",
1057 phba->brd_no, la->eventTag, phba->fc_eventTag,
1058 la->granted_AL_PA, la->UlnkSpeed,
1059 phba->alpa_map[0]);
1060 } else {
1061 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
dea31012005-04-17 16:05:31 -05001062 "%d:1303 Link Up Event x%x received "
1063 "Data: x%x x%x x%x x%x\n",
1064 phba->brd_no, la->eventTag, phba->fc_eventTag,
1065 la->granted_AL_PA, la->UlnkSpeed,
1066 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001067 }
James Smart92d7f7b2007-06-17 19:56:38 -05001068 lpfc_mbx_process_link_up(phba, la);
dea31012005-04-17 16:05:31 -05001069 } else {
1070 phba->fc_stat.LinkDown++;
1071 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1072 "%d:1305 Link Down Event x%x received "
1073 "Data: x%x x%x x%x\n",
1074 phba->brd_no, la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001075 phba->pport->port_state, vport->fc_flag);
dea31012005-04-17 16:05:31 -05001076 lpfc_mbx_issue_link_down(phba);
1077 }
1078
1079lpfc_mbx_cmpl_read_la_free_mbuf:
1080 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1081 kfree(mp);
1082 mempool_free(pmb, phba->mbox_mem_pool);
1083 return;
1084}
1085
1086/*
1087 * This routine handles processing a REG_LOGIN mailbox
1088 * command upon completion. It is setup in the LPFC_MBOXQ
1089 * as the completion routine when the command is
1090 * handed off to the SLI layer.
1091 */
1092void
James Smart2e0fef82007-06-17 19:56:36 -05001093lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001094{
James Smart2e0fef82007-06-17 19:56:36 -05001095 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001096 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001097 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001098
dea31012005-04-17 16:05:31 -05001099 pmb->context1 = NULL;
1100
1101 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001102 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001103 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1104 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001105 mempool_free(pmb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001106 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001107
1108 return;
1109}
1110
James Smart92d7f7b2007-06-17 19:56:38 -05001111static void
1112lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1113{
1114 MAILBOX_t *mb = &pmb->mb;
1115 struct lpfc_vport *vport = pmb->vport;
1116 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1117
1118 switch (mb->mbxStatus) {
1119 case 0x0011:
1120 case 0x0020:
1121 case 0x9700:
1122 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1123 "%d (%d):0911 cmpl_unreg_vpi, "
1124 "mb status = 0x%x\n",
1125 phba->brd_no, vport->vpi, mb->mbxStatus);
1126 break;
1127 default:
1128 phba->vpi_cnt--;
1129 }
1130 vport->unreg_vpi_cmpl = VPORT_OK;
1131 mempool_free(pmb, phba->mbox_mem_pool);
1132 /*
1133 * This shost reference might have been taken at the beginning of
1134 * lpfc_vport_delete()
1135 */
1136 if (vport->load_flag & FC_UNLOADING)
1137 scsi_host_put(shost);
1138}
1139
1140void
1141lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1142{
1143 struct lpfc_hba *phba = vport->phba;
1144 LPFC_MBOXQ_t *mbox;
1145 int rc;
1146
1147 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1148 if (!mbox)
1149 return;
1150
1151 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1152 mbox->vport = vport;
1153 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
1154 rc = lpfc_sli_issue_mbox(phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
1155 if (rc == MBX_NOT_FINISHED) {
1156 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1157 "%d (%d):1800 Could not issue unreg_vpi\n",
1158 phba->brd_no, vport->vpi);
1159 mempool_free(mbox, phba->mbox_mem_pool);
1160 vport->unreg_vpi_cmpl = VPORT_ERROR;
1161 }
1162}
1163
1164static void
1165lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1166{
1167 struct lpfc_vport *vport = pmb->vport;
1168 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1169 MAILBOX_t *mb = &pmb->mb;
1170
1171 switch (mb->mbxStatus) {
1172 case 0x0011:
1173 case 0x9601:
1174 case 0x9602:
1175 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1176 "%d (%d):0912 cmpl_reg_vpi, mb status = 0x%x\n",
1177 phba->brd_no, vport->vpi, mb->mbxStatus);
1178 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1179 spin_lock_irq(shost->host_lock);
1180 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1181 spin_unlock_irq(shost->host_lock);
1182 vport->fc_myDID = 0;
1183 goto out;
1184 }
1185 phba->vpi_cnt++;
1186
1187 vport->num_disc_nodes = 0;
1188 /* go thru NPR list and issue ELS PLOGIs */
1189 if (vport->fc_npr_cnt)
1190 lpfc_els_disc_plogi(vport);
1191
1192 if (!vport->num_disc_nodes) {
1193 spin_lock_irq(shost->host_lock);
1194 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1195 spin_unlock_irq(shost->host_lock);
1196 lpfc_can_disctmo(vport);
1197 }
1198 vport->port_state = LPFC_VPORT_READY;
1199
1200out:
1201 mempool_free(pmb, phba->mbox_mem_pool);
1202 return;
1203}
1204
dea31012005-04-17 16:05:31 -05001205/*
1206 * This routine handles processing a Fabric REG_LOGIN mailbox
1207 * command upon completion. It is setup in the LPFC_MBOXQ
1208 * as the completion routine when the command is
1209 * handed off to the SLI layer.
1210 */
1211void
James Smart2e0fef82007-06-17 19:56:36 -05001212lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001213{
James Smart92d7f7b2007-06-17 19:56:38 -05001214 struct lpfc_vport *vport = pmb->vport;
1215 struct lpfc_vport *next_vport;
James Smart2e0fef82007-06-17 19:56:36 -05001216 MAILBOX_t *mb = &pmb->mb;
1217 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001218 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05001219 ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001220
James Smart329f9bc2007-04-25 09:53:01 -04001221 pmb->context1 = NULL;
1222 pmb->context2 = NULL;
1223
dea31012005-04-17 16:05:31 -05001224 if (mb->mbxStatus) {
1225 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1226 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001227 mempool_free(pmb, phba->mbox_mem_pool);
1228 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001229
James Smart92d7f7b2007-06-17 19:56:38 -05001230 if (phba->fc_topology == TOPOLOGY_LOOP) {
1231 /* FLOGI failed, use loop map to make discovery list */
1232 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001233
James Smart92d7f7b2007-06-17 19:56:38 -05001234 /* Start discovery */
1235 lpfc_disc_start(vport);
1236 return;
1237 }
1238
1239 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1240 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1241 "%d (%d):0258 Register Fabric login error: 0x%x\n",
1242 phba->brd_no, vport->vpi, mb->mbxStatus);
1243
dea31012005-04-17 16:05:31 -05001244 return;
1245 }
1246
dea31012005-04-17 16:05:31 -05001247 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001248 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001249 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001250
James Smart329f9bc2007-04-25 09:53:01 -04001251 lpfc_nlp_put(ndlp); /* Drop the reference from the mbox */
1252
James Smart2e0fef82007-06-17 19:56:36 -05001253 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart92d7f7b2007-06-17 19:56:38 -05001254 list_for_each_entry(next_vport, &phba->port_list, listentry) {
1255 if (next_vport->port_type == LPFC_PHYSICAL_PORT)
1256 continue;
dea31012005-04-17 16:05:31 -05001257
James Smart92d7f7b2007-06-17 19:56:38 -05001258 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1259 lpfc_initial_fdisc(next_vport);
1260 else {
1261 if (phba->sli3_options &
1262 LPFC_SLI3_NPIV_ENABLED) {
1263 lpfc_vport_set_state(vport,
1264 FC_VPORT_NO_FABRIC_SUPP);
1265 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
1266 "%d (%d):0259 No NPIV Fabric "
1267 "support\n",
1268 phba->brd_no, vport->vpi);
1269 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001270 }
1271 }
James Smart92d7f7b2007-06-17 19:56:38 -05001272 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001273 }
1274
1275 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1276 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001277 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001278 return;
1279}
1280
1281/*
1282 * This routine handles processing a NameServer REG_LOGIN mailbox
1283 * command upon completion. It is setup in the LPFC_MBOXQ
1284 * as the completion routine when the command is
1285 * handed off to the SLI layer.
1286 */
1287void
James Smart2e0fef82007-06-17 19:56:36 -05001288lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001289{
James Smart2e0fef82007-06-17 19:56:36 -05001290 MAILBOX_t *mb = &pmb->mb;
1291 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1292 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1293 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001294
1295 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001296out:
James Smart329f9bc2007-04-25 09:53:01 -04001297 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001298 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1299 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001300 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05001301 lpfc_drop_node(vport, ndlp);
dea31012005-04-17 16:05:31 -05001302
James Smart92d7f7b2007-06-17 19:56:38 -05001303 if (phba->fc_topology == TOPOLOGY_LOOP) {
1304 /*
1305 * RegLogin failed, use loop map to make discovery
1306 * list
1307 */
1308 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001309
James Smart92d7f7b2007-06-17 19:56:38 -05001310 /* Start discovery */
1311 lpfc_disc_start(vport);
1312 return;
1313 }
1314 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1315 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
1316 "%d (%d):0260 Register NameServer error: 0x%x\n",
1317 phba->brd_no, vport->vpi, mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001318 return;
1319 }
1320
1321 pmb->context1 = NULL;
1322
dea31012005-04-17 16:05:31 -05001323 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001324 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001325 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001326
James Smart2e0fef82007-06-17 19:56:36 -05001327 if (vport->port_state < LPFC_VPORT_READY) {
1328 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001329 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1330 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1331 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1332 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1333 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1334
1335 /* Issue SCR just before NameServer GID_FT Query */
1336 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001337 }
1338
James Smart2e0fef82007-06-17 19:56:36 -05001339 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001340 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001341 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001342 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001343 goto out;
dea31012005-04-17 16:05:31 -05001344 }
1345
James Smart329f9bc2007-04-25 09:53:01 -04001346 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001347 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1348 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001349 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001350
1351 return;
1352}
1353
1354static void
James Smart2e0fef82007-06-17 19:56:36 -05001355lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001356{
James Smart2e0fef82007-06-17 19:56:36 -05001357 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1358 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001359 struct lpfc_rport_data *rdata;
1360 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001361 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001362
1363 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001364 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1365 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001366 rport_ids.port_id = ndlp->nlp_DID;
1367 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001368
James Smart329f9bc2007-04-25 09:53:01 -04001369 /*
1370 * We leave our node pointer in rport->dd_data when we unregister a
1371 * FCP target port. But fc_remote_port_add zeros the space to which
1372 * rport->dd_data points. So, if we're reusing a previously
1373 * registered port, drop the reference that we took the last time we
1374 * registered the port.
1375 */
1376 if (ndlp->rport && ndlp->rport->dd_data &&
James Smart92d7f7b2007-06-17 19:56:38 -05001377 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp) {
James Smart329f9bc2007-04-25 09:53:01 -04001378 lpfc_nlp_put(ndlp);
1379 }
James Smart2e0fef82007-06-17 19:56:36 -05001380 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001381 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001382 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1383 "Warning: fc_remote_port_add failed\n");
1384 return;
1385 }
1386
1387 /* initialize static port data */
1388 rport->maxframe_size = ndlp->nlp_maxframe;
1389 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001390 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001391 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001392
1393 if (ndlp->nlp_type & NLP_FCP_TARGET)
1394 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1395 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1396 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
James Smart92d7f7b2007-06-17 19:56:38 -05001397 del_timer_sync(&ndlp->nlp_initiator_tmr);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001398
1399
1400 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1401 fc_remote_port_rolechg(rport, rport_ids.roles);
1402
James Smart071fbd3d2006-04-15 11:53:20 -04001403 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001404 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001405 ndlp->nlp_sid = rport->scsi_target_id;
1406 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001407 return;
1408}
1409
1410static void
James Smart2e0fef82007-06-17 19:56:36 -05001411lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001412{
1413 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001414
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001415 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001416
1417 return;
1418}
1419
James Smartde0c5b32007-04-25 09:52:27 -04001420static void
James Smart2e0fef82007-06-17 19:56:36 -05001421lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001422{
James Smart2e0fef82007-06-17 19:56:36 -05001423 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1424
1425 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001426 switch (state) {
1427 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001428 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001429 break;
1430 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001431 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001432 break;
1433 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001434 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001435 break;
1436 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001437 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001438 break;
1439 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001440 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001441 break;
1442 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001443 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001444 break;
1445 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001446 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001447 break;
1448 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001449 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001450 break;
1451 }
James Smart2e0fef82007-06-17 19:56:36 -05001452 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001453}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001454
James Smartde0c5b32007-04-25 09:52:27 -04001455static void
James Smart2e0fef82007-06-17 19:56:36 -05001456lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001457 int old_state, int new_state)
1458{
James Smart2e0fef82007-06-17 19:56:36 -05001459 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1460
James Smartde0c5b32007-04-25 09:52:27 -04001461 if (new_state == NLP_STE_UNMAPPED_NODE) {
1462 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1463 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1464 ndlp->nlp_type |= NLP_FC_NODE;
1465 }
1466 if (new_state == NLP_STE_MAPPED_NODE)
1467 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1468 if (new_state == NLP_STE_NPR_NODE)
1469 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001470
James Smartde0c5b32007-04-25 09:52:27 -04001471 /* Transport interface */
1472 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1473 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001474 vport->phba->nport_event_cnt++;
1475 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001476 }
dea31012005-04-17 16:05:31 -05001477
James Smartde0c5b32007-04-25 09:52:27 -04001478 if (new_state == NLP_STE_MAPPED_NODE ||
1479 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001480 vport->phba->nport_event_cnt++;
dea31012005-04-17 16:05:31 -05001481 /*
1482 * Tell the fc transport about the port, if we haven't
1483 * already. If we have, and it's a scsi entity, be
1484 * sure to unblock any attached scsi devices
1485 */
James Smart2e0fef82007-06-17 19:56:36 -05001486 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001487 }
dea31012005-04-17 16:05:31 -05001488
1489 /*
1490 * if we added to Mapped list, but the remote port
1491 * registration failed or assigned a target id outside
1492 * our presentable range - move the node to the
1493 * Unmapped List
1494 */
James Smartde0c5b32007-04-25 09:52:27 -04001495 if (new_state == NLP_STE_MAPPED_NODE &&
1496 (!ndlp->rport ||
1497 ndlp->rport->scsi_target_id == -1 ||
1498 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001499 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001500 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001501 spin_unlock_irq(shost->host_lock);
1502 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001503 }
James Smartde0c5b32007-04-25 09:52:27 -04001504}
1505
James Smart685f0bf2007-04-25 09:53:08 -04001506static char *
1507lpfc_nlp_state_name(char *buffer, size_t size, int state)
1508{
1509 static char *states[] = {
1510 [NLP_STE_UNUSED_NODE] = "UNUSED",
1511 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1512 [NLP_STE_ADISC_ISSUE] = "ADISC",
1513 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1514 [NLP_STE_PRLI_ISSUE] = "PRLI",
1515 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1516 [NLP_STE_MAPPED_NODE] = "MAPPED",
1517 [NLP_STE_NPR_NODE] = "NPR",
1518 };
1519
1520 if (state < ARRAY_SIZE(states) && states[state])
1521 strlcpy(buffer, states[state], size);
1522 else
1523 snprintf(buffer, size, "unknown (%d)", state);
1524 return buffer;
1525}
1526
James Smartde0c5b32007-04-25 09:52:27 -04001527void
James Smart2e0fef82007-06-17 19:56:36 -05001528lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1529 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001530{
James Smart2e0fef82007-06-17 19:56:36 -05001531 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001532 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001533 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001534
James Smart2e0fef82007-06-17 19:56:36 -05001535 lpfc_printf_log(vport->phba, KERN_INFO, LOG_NODE,
James Smart685f0bf2007-04-25 09:53:08 -04001536 "%d:0904 NPort state transition x%06x, %s -> %s\n",
James Smart2e0fef82007-06-17 19:56:36 -05001537 vport->phba->brd_no,
James Smart685f0bf2007-04-25 09:53:08 -04001538 ndlp->nlp_DID,
1539 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1540 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smartde0c5b32007-04-25 09:52:27 -04001541 if (old_state == NLP_STE_NPR_NODE &&
1542 (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1543 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001544 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001545 if (old_state == NLP_STE_UNMAPPED_NODE) {
1546 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1547 ndlp->nlp_type &= ~NLP_FC_NODE;
1548 }
1549
James Smart685f0bf2007-04-25 09:53:08 -04001550 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001551 spin_lock_irq(shost->host_lock);
1552 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1553 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001554 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001555 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001556
1557 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001558 lpfc_nlp_counters(vport, state, 1);
1559 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001560}
1561
1562void
James Smart2e0fef82007-06-17 19:56:36 -05001563lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001564{
James Smart2e0fef82007-06-17 19:56:36 -05001565 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1566
James Smartde0c5b32007-04-25 09:52:27 -04001567 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
James Smart2e0fef82007-06-17 19:56:36 -05001568 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001569 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001570 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1571 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001572 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001573 spin_unlock_irq(shost->host_lock);
1574 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state, 0);
James Smartde0c5b32007-04-25 09:52:27 -04001575}
1576
1577void
James Smart2e0fef82007-06-17 19:56:36 -05001578lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001579{
James Smart2e0fef82007-06-17 19:56:36 -05001580 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1581
James Smartde0c5b32007-04-25 09:52:27 -04001582 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
James Smart2e0fef82007-06-17 19:56:36 -05001583 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001584 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001585 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1586 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001587 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001588 spin_unlock_irq(shost->host_lock);
James Smart329f9bc2007-04-25 09:53:01 -04001589 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001590}
1591
1592/*
1593 * Start / ReStart rescue timer for Discovery / RSCN handling
1594 */
1595void
James Smart2e0fef82007-06-17 19:56:36 -05001596lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001597{
James Smart2e0fef82007-06-17 19:56:36 -05001598 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1599 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001600 uint32_t tmo;
1601
James Smart2e0fef82007-06-17 19:56:36 -05001602 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001603 /* For FAN, timeout should be greater then edtov */
1604 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1605 } else {
1606 /* Normal discovery timeout should be > then ELS/CT timeout
1607 * FC spec states we need 3 * ratov for CT requests
1608 */
1609 tmo = ((phba->fc_ratov * 3) + 3);
1610 }
dea31012005-04-17 16:05:31 -05001611
James Smart2e0fef82007-06-17 19:56:36 -05001612 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1613 spin_lock_irq(shost->host_lock);
1614 vport->fc_flag |= FC_DISC_TMO;
1615 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001616
1617 /* Start Discovery Timer state <hba_state> */
1618 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05001619 "%d (%d):0247 Start Discovery Timer state x%x "
dea31012005-04-17 16:05:31 -05001620 "Data: x%x x%lx x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001621 phba->brd_no, vport->vpi, vport->port_state, tmo,
James Smart2e0fef82007-06-17 19:56:36 -05001622 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1623 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001624
1625 return;
1626}
1627
1628/*
1629 * Cancel rescue timer for Discovery / RSCN handling
1630 */
1631int
James Smart2e0fef82007-06-17 19:56:36 -05001632lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001633{
James Smart2e0fef82007-06-17 19:56:36 -05001634 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1635 struct lpfc_hba *phba = vport->phba;
1636 unsigned long iflags;
1637
dea31012005-04-17 16:05:31 -05001638 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05001639 if (vport->fc_flag & FC_DISC_TMO) {
1640 spin_lock_irqsave(shost->host_lock, iflags);
1641 vport->fc_flag &= ~FC_DISC_TMO;
1642 spin_unlock_irqrestore(shost->host_lock, iflags);
1643 del_timer_sync(&vport->fc_disctmo);
1644 spin_lock_irqsave(&vport->work_port_lock, iflags);
1645 vport->work_port_events &= ~WORKER_DISC_TMO;
1646 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05001647 }
1648
1649 /* Cancel Discovery Timer state <hba_state> */
1650 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05001651 "%d (%d):0248 Cancel Discovery Timer state x%x "
dea31012005-04-17 16:05:31 -05001652 "Data: x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001653 phba->brd_no, vport->vpi, vport->port_state,
1654 vport->fc_flag, vport->fc_plogi_cnt,
1655 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001656
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001657 return 0;
dea31012005-04-17 16:05:31 -05001658}
1659
1660/*
1661 * Check specified ring for outstanding IOCB on the SLI queue
1662 * Return true if iocb matches the specified nport
1663 */
1664int
James Smart2e0fef82007-06-17 19:56:36 -05001665lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1666 struct lpfc_sli_ring *pring,
1667 struct lpfc_iocbq *iocb,
1668 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001669{
James Smart2e0fef82007-06-17 19:56:36 -05001670 struct lpfc_sli *psli = &phba->sli;
1671 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001672 struct lpfc_vport *vport = ndlp->vport;
1673
1674 if (iocb->vport != vport)
1675 return 0;
1676
dea31012005-04-17 16:05:31 -05001677 if (pring->ringno == LPFC_ELS_RING) {
1678 switch (icmd->ulpCommand) {
1679 case CMD_GEN_REQUEST64_CR:
1680 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001681 return 1;
dea31012005-04-17 16:05:31 -05001682 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04001683 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1684 return 1;
dea31012005-04-17 16:05:31 -05001685 case CMD_XMIT_ELS_RSP64_CX:
1686 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001687 return 1;
dea31012005-04-17 16:05:31 -05001688 }
James Smarta4bc3372006-12-02 13:34:16 -05001689 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05001690
1691 } else if (pring->ringno == psli->fcp_ring) {
1692 /* Skip match check if waiting to relogin to FCP target */
1693 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001694 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001695 return 0;
dea31012005-04-17 16:05:31 -05001696 }
1697 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001698 return 1;
dea31012005-04-17 16:05:31 -05001699 }
1700 } else if (pring->ringno == psli->next_ring) {
1701
1702 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001703 return 0;
dea31012005-04-17 16:05:31 -05001704}
1705
1706/*
1707 * Free resources / clean up outstanding I/Os
1708 * associated with nlp_rpi in the LPFC_NODELIST entry.
1709 */
1710static int
James Smart2e0fef82007-06-17 19:56:36 -05001711lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001712{
James Smart2534ba72007-04-25 09:52:20 -04001713 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001714 struct lpfc_sli *psli;
1715 struct lpfc_sli_ring *pring;
1716 struct lpfc_iocbq *iocb, *next_iocb;
1717 IOCB_t *icmd;
1718 uint32_t rpi, i;
1719
James Smart92d7f7b2007-06-17 19:56:38 -05001720 lpfc_fabric_abort_nport(ndlp);
1721
dea31012005-04-17 16:05:31 -05001722 /*
1723 * Everything that matches on txcmplq will be returned
1724 * by firmware with a no rpi error.
1725 */
1726 psli = &phba->sli;
1727 rpi = ndlp->nlp_rpi;
1728 if (rpi) {
1729 /* Now process each ring */
1730 for (i = 0; i < psli->num_rings; i++) {
1731 pring = &psli->ring[i];
1732
James Smart2e0fef82007-06-17 19:56:36 -05001733 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001734 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05001735 list) {
dea31012005-04-17 16:05:31 -05001736 /*
1737 * Check to see if iocb matches the nport we are
1738 * looking for
1739 */
James Smart92d7f7b2007-06-17 19:56:38 -05001740 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1741 ndlp))) {
dea31012005-04-17 16:05:31 -05001742 /* It matches, so deque and call compl
1743 with an error */
James Smart2534ba72007-04-25 09:52:20 -04001744 list_move_tail(&iocb->list,
1745 &completions);
dea31012005-04-17 16:05:31 -05001746 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001747 }
1748 }
James Smart2e0fef82007-06-17 19:56:36 -05001749 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001750 }
1751 }
James Smart2534ba72007-04-25 09:52:20 -04001752
1753 while (!list_empty(&completions)) {
1754 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05001755 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04001756
James Smart2e0fef82007-06-17 19:56:36 -05001757 if (!iocb->iocb_cmpl)
1758 lpfc_sli_release_iocbq(phba, iocb);
1759 else {
James Smart2534ba72007-04-25 09:52:20 -04001760 icmd = &iocb->iocb;
1761 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1762 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
James Smart2e0fef82007-06-17 19:56:36 -05001763 (iocb->iocb_cmpl)(phba, iocb, iocb);
1764 }
James Smart2534ba72007-04-25 09:52:20 -04001765 }
1766
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001767 return 0;
dea31012005-04-17 16:05:31 -05001768}
1769
1770/*
1771 * Free rpi associated with LPFC_NODELIST entry.
1772 * This routine is called from lpfc_freenode(), when we are removing
1773 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1774 * LOGO that completes successfully, and we are waiting to PLOGI back
1775 * to the remote NPort. In addition, it is called after we receive
1776 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1777 * we are waiting to PLOGI back to the remote NPort.
1778 */
1779int
James Smart2e0fef82007-06-17 19:56:36 -05001780lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001781{
James Smart2e0fef82007-06-17 19:56:36 -05001782 struct lpfc_hba *phba = vport->phba;
1783 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05001784 int rc;
1785
1786 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05001787 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1788 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001789 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05001790 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001791 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1792 rc = lpfc_sli_issue_mbox(phba, mbox,
1793 (MBX_NOWAIT | MBX_STOP_IOCB));
dea31012005-04-17 16:05:31 -05001794 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05001795 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001796 }
dea31012005-04-17 16:05:31 -05001797 lpfc_no_rpi(phba, ndlp);
1798 ndlp->nlp_rpi = 0;
1799 return 1;
1800 }
1801 return 0;
1802}
1803
James Smart92d7f7b2007-06-17 19:56:38 -05001804void
1805lpfc_unreg_all_rpis(struct lpfc_vport *vport)
1806{
1807 struct lpfc_hba *phba = vport->phba;
1808 LPFC_MBOXQ_t *mbox;
1809 int rc;
1810
1811 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1812 if (mbox) {
1813 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
1814 mbox->vport = vport;
1815 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1816 rc = lpfc_sli_issue_mbox(phba, mbox,
1817 (MBX_NOWAIT | MBX_STOP_IOCB));
1818 if (rc == MBX_NOT_FINISHED) {
1819 mempool_free(mbox, phba->mbox_mem_pool);
1820 }
1821 }
1822}
1823
1824void
1825lpfc_unreg_default_rpis(struct lpfc_vport *vport)
1826{
1827 struct lpfc_hba *phba = vport->phba;
1828 LPFC_MBOXQ_t *mbox;
1829 int rc;
1830
1831 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1832 if (mbox) {
1833 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
1834 mbox->vport = vport;
1835 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1836 rc = lpfc_sli_issue_mbox(phba, mbox,
1837 (MBX_NOWAIT | MBX_STOP_IOCB));
1838 if (rc == MBX_NOT_FINISHED) {
1839 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1840 "%d (%d):1815 Could not issue "
1841 "unreg_did (default rpis)\n",
1842 phba->brd_no, vport->vpi);
1843 mempool_free(mbox, phba->mbox_mem_pool);
1844 }
1845 }
1846}
1847
dea31012005-04-17 16:05:31 -05001848/*
1849 * Free resources associated with LPFC_NODELIST entry
1850 * so it can be freed.
1851 */
1852static int
James Smart2e0fef82007-06-17 19:56:36 -05001853lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001854{
James Smart2e0fef82007-06-17 19:56:36 -05001855 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1856 struct lpfc_hba *phba = vport->phba;
1857 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05001858 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05001859
1860 /* Cleanup node for NPort <nlp_DID> */
1861 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
James Smart92d7f7b2007-06-17 19:56:38 -05001862 "%d (%d):0900 Cleanup node for NPort x%x "
dea31012005-04-17 16:05:31 -05001863 "Data: x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001864 phba->brd_no, vport->vpi, ndlp->nlp_DID, ndlp->nlp_flag,
dea31012005-04-17 16:05:31 -05001865 ndlp->nlp_state, ndlp->nlp_rpi);
1866
James Smart2e0fef82007-06-17 19:56:36 -05001867 lpfc_dequeue_node(vport, ndlp);
dea31012005-04-17 16:05:31 -05001868
dea31012005-04-17 16:05:31 -05001869 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1870 if ((mb = phba->sli.mbox_active)) {
1871 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1872 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1873 mb->context2 = NULL;
1874 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1875 }
1876 }
James Smart33ccf8d2006-08-17 11:57:58 -04001877
James Smart2e0fef82007-06-17 19:56:36 -05001878 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001879 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1880 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001881 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05001882 mp = (struct lpfc_dmabuf *) (mb->context1);
1883 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05001884 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001885 kfree(mp);
1886 }
1887 list_del(&mb->list);
1888 mempool_free(mb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001889 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001890 }
1891 }
James Smart2e0fef82007-06-17 19:56:36 -05001892 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001893
James Smart07951072007-04-25 09:51:38 -04001894 lpfc_els_abort(phba,ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05001895 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04001896 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05001897 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001898
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05001899 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05001900 del_timer_sync(&ndlp->nlp_delayfunc);
James Smart92d7f7b2007-06-17 19:56:38 -05001901 del_timer_sync(&ndlp->nlp_initiator_tmr);
dea31012005-04-17 16:05:31 -05001902
dea31012005-04-17 16:05:31 -05001903 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1904 list_del_init(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05001905 if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
1906 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05001907
James Smart2e0fef82007-06-17 19:56:36 -05001908 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05001909
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001910 return 0;
dea31012005-04-17 16:05:31 -05001911}
1912
1913/*
1914 * Check to see if we can free the nlp back to the freelist.
1915 * If we are in the middle of using the nlp in the discovery state
1916 * machine, defer the free till we reach the end of the state machine.
1917 */
James Smart329f9bc2007-04-25 09:53:01 -04001918static void
James Smart2e0fef82007-06-17 19:56:36 -05001919lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001920{
James Smart1dcb58e2007-04-25 09:51:30 -04001921 struct lpfc_rport_data *rdata;
dea31012005-04-17 16:05:31 -05001922
1923 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
James Smart2e0fef82007-06-17 19:56:36 -05001924 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea31012005-04-17 16:05:31 -05001925 }
1926
James Smart2e0fef82007-06-17 19:56:36 -05001927 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04001928
James Smart2e0fef82007-06-17 19:56:36 -05001929 /*
James Smart92d7f7b2007-06-17 19:56:38 -05001930 * We can get here with a non-NULL ndlp->rport because when we
1931 * unregister a rport we don't break the rport/node linkage. So if we
1932 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05001933 */
James Smart92d7f7b2007-06-17 19:56:38 -05001934 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04001935 rdata = ndlp->rport->dd_data;
1936 rdata->pnode = NULL;
1937 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05001938 }
dea31012005-04-17 16:05:31 -05001939}
1940
1941static int
James Smart2e0fef82007-06-17 19:56:36 -05001942lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1943 uint32_t did)
dea31012005-04-17 16:05:31 -05001944{
James Smart2e0fef82007-06-17 19:56:36 -05001945 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05001946
1947 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001948 return 0;
dea31012005-04-17 16:05:31 -05001949
1950 if (ndlp->nlp_DID == 0) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001951 return 0;
dea31012005-04-17 16:05:31 -05001952 }
1953
1954 /* First check for Direct match */
1955 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001956 return 1;
dea31012005-04-17 16:05:31 -05001957
1958 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05001959 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05001960 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001961 return 0;
dea31012005-04-17 16:05:31 -05001962 }
1963
1964 matchdid.un.word = did;
1965 ndlpdid.un.word = ndlp->nlp_DID;
1966 if (matchdid.un.b.id == ndlpdid.un.b.id) {
1967 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
1968 (mydid.un.b.area == matchdid.un.b.area)) {
1969 if ((ndlpdid.un.b.domain == 0) &&
1970 (ndlpdid.un.b.area == 0)) {
1971 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001972 return 1;
dea31012005-04-17 16:05:31 -05001973 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001974 return 0;
dea31012005-04-17 16:05:31 -05001975 }
1976
1977 matchdid.un.word = ndlp->nlp_DID;
1978 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
1979 (mydid.un.b.area == ndlpdid.un.b.area)) {
1980 if ((matchdid.un.b.domain == 0) &&
1981 (matchdid.un.b.area == 0)) {
1982 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001983 return 1;
dea31012005-04-17 16:05:31 -05001984 }
1985 }
1986 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001987 return 0;
dea31012005-04-17 16:05:31 -05001988}
1989
James Smart685f0bf2007-04-25 09:53:08 -04001990/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05001991static struct lpfc_nodelist *
1992__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05001993{
James Smart2e0fef82007-06-17 19:56:36 -05001994 struct lpfc_hba *phba = vport->phba;
James Smart2fb9bd82006-12-02 13:33:57 -05001995 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05001996 uint32_t data1;
1997
James Smart2e0fef82007-06-17 19:56:36 -05001998 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1999 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002000 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2001 ((uint32_t) ndlp->nlp_xri << 16) |
2002 ((uint32_t) ndlp->nlp_type << 8) |
2003 ((uint32_t) ndlp->nlp_rpi & 0xff));
2004 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
James Smart92d7f7b2007-06-17 19:56:38 -05002005 "%d (%d):0929 FIND node DID "
James Smart685f0bf2007-04-25 09:53:08 -04002006 " Data: x%p x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002007 phba->brd_no, vport->vpi,
James Smart685f0bf2007-04-25 09:53:08 -04002008 ndlp, ndlp->nlp_DID,
2009 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002010 return ndlp;
dea31012005-04-17 16:05:31 -05002011 }
2012 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002013
dea31012005-04-17 16:05:31 -05002014 /* FIND node did <did> NOT FOUND */
James Smart2fb9bd82006-12-02 13:33:57 -05002015 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
James Smart92d7f7b2007-06-17 19:56:38 -05002016 "%d (%d):0932 FIND node did x%x NOT FOUND.\n",
2017 phba->brd_no, vport->vpi, did);
dea31012005-04-17 16:05:31 -05002018 return NULL;
2019}
2020
2021struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002022lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002023{
James Smart2e0fef82007-06-17 19:56:36 -05002024 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002025 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002026
James Smart2e0fef82007-06-17 19:56:36 -05002027 spin_lock_irq(shost->host_lock);
2028 ndlp = __lpfc_findnode_did(vport, did);
2029 spin_unlock_irq(shost->host_lock);
2030 return ndlp;
2031}
2032
2033struct lpfc_nodelist *
2034lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2035{
2036 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2037 struct lpfc_nodelist *ndlp;
2038
2039 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002040 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002041 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2042 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002043 return NULL;
2044 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002045 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002046 if (!ndlp)
2047 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002048 lpfc_nlp_init(vport, ndlp, did);
2049 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2050 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002051 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002052 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002053 return ndlp;
2054 }
James Smart2e0fef82007-06-17 19:56:36 -05002055 if (vport->fc_flag & FC_RSCN_MODE) {
2056 if (lpfc_rscn_payload_check(vport, did)) {
2057 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002058 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002059 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002060
2061 /* Since this node is marked for discovery,
2062 * delay timeout is not needed.
2063 */
James Smartfdcebe22006-03-07 15:04:01 -05002064 if (ndlp->nlp_flag & NLP_DELAY_TMO)
James Smart2e0fef82007-06-17 19:56:36 -05002065 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04002066 } else
dea31012005-04-17 16:05:31 -05002067 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002068 } else {
James Smart685f0bf2007-04-25 09:53:08 -04002069 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
2070 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE)
dea31012005-04-17 16:05:31 -05002071 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002072 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2073 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002074 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002075 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002076 }
2077 return ndlp;
2078}
2079
2080/* Build a list of nodes to discover based on the loopmap */
2081void
James Smart2e0fef82007-06-17 19:56:36 -05002082lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002083{
James Smart2e0fef82007-06-17 19:56:36 -05002084 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002085 int j;
2086 uint32_t alpa, index;
2087
James Smart2e0fef82007-06-17 19:56:36 -05002088 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002089 return;
James Smart2e0fef82007-06-17 19:56:36 -05002090
2091 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002092 return;
dea31012005-04-17 16:05:31 -05002093
2094 /* Check for loop map present or not */
2095 if (phba->alpa_map[0]) {
2096 for (j = 1; j <= phba->alpa_map[0]; j++) {
2097 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002098 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002099 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002100 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002101 }
2102 } else {
2103 /* No alpamap, so try all alpa's */
2104 for (j = 0; j < FC_MAXLOOP; j++) {
2105 /* If cfg_scan_down is set, start from highest
2106 * ALPA (0xef) to lowest (0x1).
2107 */
2108 if (phba->cfg_scan_down)
2109 index = j;
2110 else
2111 index = FC_MAXLOOP - j - 1;
2112 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002113 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002114 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002115 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002116 }
2117 }
2118 return;
2119}
2120
dea31012005-04-17 16:05:31 -05002121void
James Smart2e0fef82007-06-17 19:56:36 -05002122lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002123{
dea31012005-04-17 16:05:31 -05002124 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002125 struct lpfc_sli *psli = &phba->sli;
2126 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2127 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2128 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2129 int rc;
2130
James Smart92d7f7b2007-06-17 19:56:38 -05002131 /*
2132 * if it's not a physical port or if we already send
2133 * clear_la then don't send it.
2134 */
2135 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2136 (vport->port_type != LPFC_PHYSICAL_PORT))
2137 return;
2138
James Smart2e0fef82007-06-17 19:56:36 -05002139 /* Link up discovery */
2140 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2141 phba->link_state = LPFC_CLEAR_LA;
2142 lpfc_clear_la(phba, mbox);
2143 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2144 mbox->vport = vport;
2145 rc = lpfc_sli_issue_mbox(phba, mbox, (MBX_NOWAIT |
2146 MBX_STOP_IOCB));
2147 if (rc == MBX_NOT_FINISHED) {
2148 mempool_free(mbox, phba->mbox_mem_pool);
2149 lpfc_disc_flush_list(vport);
2150 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2151 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2152 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002153 phba->link_state = LPFC_HBA_ERROR;
2154 }
2155 }
2156}
2157
2158/* Reg_vpi to tell firmware to resume normal operations */
2159void
2160lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2161{
2162 LPFC_MBOXQ_t *regvpimbox;
2163
2164 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2165 if (regvpimbox) {
2166 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2167 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2168 regvpimbox->vport = vport;
2169 if (lpfc_sli_issue_mbox(phba, regvpimbox,
2170 (MBX_NOWAIT | MBX_STOP_IOCB))
2171 == MBX_NOT_FINISHED) {
2172 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002173 }
2174 }
2175}
2176
2177/* Start Link up / RSCN discovery on NPR nodes */
2178void
2179lpfc_disc_start(struct lpfc_vport *vport)
2180{
2181 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2182 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002183 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002184 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002185 int did_changed;
dea31012005-04-17 16:05:31 -05002186
James Smart2e0fef82007-06-17 19:56:36 -05002187 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002188 return;
James Smart2e0fef82007-06-17 19:56:36 -05002189
2190 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002191 clear_la_pending = 1;
2192 else
2193 clear_la_pending = 0;
2194
James Smart2e0fef82007-06-17 19:56:36 -05002195 if (vport->port_state < LPFC_VPORT_READY)
2196 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002197
James Smart2e0fef82007-06-17 19:56:36 -05002198 lpfc_set_disctmo(vport);
2199
2200 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002201 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002202 else
dea31012005-04-17 16:05:31 -05002203 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002204
2205 vport->fc_prevDID = vport->fc_myDID;
2206 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002207
2208 /* Start Discovery state <hba_state> */
2209 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002210 "%d (%d):0202 Start Discovery hba state x%x "
dea31012005-04-17 16:05:31 -05002211 "Data: x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002212 phba->brd_no, vport->vpi, vport->port_state,
2213 vport->fc_flag, vport->fc_plogi_cnt,
2214 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002215
2216 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002217 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002218
2219 if (num_sent)
2220 return;
2221
James Smart92d7f7b2007-06-17 19:56:38 -05002222 /*
2223 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2224 * continue discovery.
2225 */
2226 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2227 !(vport->fc_flag & FC_RSCN_MODE)) {
2228 lpfc_issue_reg_vpi(phba, vport);
2229 return;
2230 }
James Smart2e0fef82007-06-17 19:56:36 -05002231
James Smart92d7f7b2007-06-17 19:56:38 -05002232 /*
2233 * For SLI2, we need to set port_state to READY and continue
2234 * discovery.
2235 */
2236 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2237 /* If we get here, there is nothing to ADISC */
2238 if (vport->port_type == LPFC_PHYSICAL_PORT)
2239 lpfc_issue_clear_la(phba, vport);
2240
2241 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002242 vport->num_disc_nodes = 0;
2243 /* go thru NPR nodes and issue ELS PLOGIs */
2244 if (vport->fc_npr_cnt)
2245 lpfc_els_disc_plogi(vport);
2246
2247 if (!vport->num_disc_nodes) {
2248 spin_lock_irq(shost->host_lock);
2249 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2250 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002251 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002252 }
2253 }
James Smart92d7f7b2007-06-17 19:56:38 -05002254 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002255 } else {
2256 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002257 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002258
2259 if (num_sent)
2260 return;
2261
James Smart2e0fef82007-06-17 19:56:36 -05002262 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002263 /* Check to see if more RSCNs came in while we
2264 * were processing this one.
2265 */
James Smart2e0fef82007-06-17 19:56:36 -05002266 if ((vport->fc_rscn_id_cnt == 0) &&
2267 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2268 spin_lock_irq(shost->host_lock);
2269 vport->fc_flag &= ~FC_RSCN_MODE;
2270 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002271 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002272 } else
James Smart2e0fef82007-06-17 19:56:36 -05002273 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002274 }
2275 }
2276 return;
2277}
2278
2279/*
2280 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2281 * ring the match the sppecified nodelist.
2282 */
2283static void
James Smart2e0fef82007-06-17 19:56:36 -05002284lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002285{
James Smart2534ba72007-04-25 09:52:20 -04002286 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002287 struct lpfc_sli *psli;
2288 IOCB_t *icmd;
2289 struct lpfc_iocbq *iocb, *next_iocb;
2290 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002291
2292 psli = &phba->sli;
2293 pring = &psli->ring[LPFC_ELS_RING];
2294
2295 /* Error matching iocb on txq or txcmplq
2296 * First check the txq.
2297 */
James Smart2e0fef82007-06-17 19:56:36 -05002298 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002299 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2300 if (iocb->context1 != ndlp) {
2301 continue;
2302 }
2303 icmd = &iocb->iocb;
2304 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2305 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2306
James Smart2534ba72007-04-25 09:52:20 -04002307 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002308 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002309 }
2310 }
2311
2312 /* Next check the txcmplq */
2313 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2314 if (iocb->context1 != ndlp) {
2315 continue;
2316 }
2317 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002318 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2319 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002320 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002321 }
2322 }
James Smart2e0fef82007-06-17 19:56:36 -05002323 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002324
2325 while (!list_empty(&completions)) {
2326 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002327 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002328
James Smart2e0fef82007-06-17 19:56:36 -05002329 if (!iocb->iocb_cmpl)
2330 lpfc_sli_release_iocbq(phba, iocb);
2331 else {
James Smart2534ba72007-04-25 09:52:20 -04002332 icmd = &iocb->iocb;
2333 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2334 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2335 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002336 }
James Smart2534ba72007-04-25 09:52:20 -04002337 }
dea31012005-04-17 16:05:31 -05002338}
2339
2340void
James Smart2e0fef82007-06-17 19:56:36 -05002341lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002342{
2343 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002344 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002345
James Smart2e0fef82007-06-17 19:56:36 -05002346 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2347 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002348 nlp_listp) {
2349 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2350 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2351 lpfc_free_tx(phba, ndlp);
2352 lpfc_nlp_put(ndlp);
2353 }
dea31012005-04-17 16:05:31 -05002354 }
2355 }
dea31012005-04-17 16:05:31 -05002356}
2357
James Smart92d7f7b2007-06-17 19:56:38 -05002358void
2359lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2360{
2361 lpfc_els_flush_rscn(vport);
2362 lpfc_els_flush_cmd(vport);
2363 lpfc_disc_flush_list(vport);
2364}
2365
dea31012005-04-17 16:05:31 -05002366/*****************************************************************************/
2367/*
2368 * NAME: lpfc_disc_timeout
2369 *
2370 * FUNCTION: Fibre Channel driver discovery timeout routine.
2371 *
2372 * EXECUTION ENVIRONMENT: interrupt only
2373 *
2374 * CALLED FROM:
2375 * Timer function
2376 *
2377 * RETURNS:
2378 * none
2379 */
2380/*****************************************************************************/
2381void
2382lpfc_disc_timeout(unsigned long ptr)
2383{
James Smart2e0fef82007-06-17 19:56:36 -05002384 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2385 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002386 unsigned long flags = 0;
2387
2388 if (unlikely(!phba))
2389 return;
2390
James Smart2e0fef82007-06-17 19:56:36 -05002391 if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2392 spin_lock_irqsave(&vport->work_port_lock, flags);
2393 vport->work_port_events |= WORKER_DISC_TMO;
2394 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2395
James Smart92d7f7b2007-06-17 19:56:38 -05002396 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002397 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05002398 lpfc_worker_wake_up(phba);
2399 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002400 }
dea31012005-04-17 16:05:31 -05002401 return;
2402}
2403
2404static void
James Smart2e0fef82007-06-17 19:56:36 -05002405lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002406{
James Smart2e0fef82007-06-17 19:56:36 -05002407 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2408 struct lpfc_hba *phba = vport->phba;
2409 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002410 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002411 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002412 int rc, clrlaerr = 0;
2413
James Smart2e0fef82007-06-17 19:56:36 -05002414 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002415 return;
2416
James Smart2e0fef82007-06-17 19:56:36 -05002417 spin_lock_irq(shost->host_lock);
2418 vport->fc_flag &= ~FC_DISC_TMO;
2419 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002420
James Smart2e0fef82007-06-17 19:56:36 -05002421 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002422
2423 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002424 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2425 * FAN
2426 */
2427 /* FAN timeout */
James Smarted957682007-06-17 19:56:37 -05002428 lpfc_printf_log(phba, KERN_WARNING, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002429 "%d (%d):0221 FAN timeout\n",
2430 phba->brd_no, vport->vpi);
dea31012005-04-17 16:05:31 -05002431
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002432 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002433 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002434 nlp_listp) {
2435 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2436 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002437 if (ndlp->nlp_type & NLP_FABRIC) {
2438 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002439 lpfc_drop_node(vport, ndlp);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002440 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002441 /* Fail outstanding IO now since device
2442 * is marked for PLOGI.
2443 */
James Smart2e0fef82007-06-17 19:56:36 -05002444 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002445 }
2446 }
James Smart92d7f7b2007-06-17 19:56:38 -05002447 if (vport->port_state != LPFC_FLOGI) {
2448 vport->port_state = LPFC_FLOGI;
2449 lpfc_set_disctmo(vport);
2450 lpfc_initial_flogi(vport);
2451 }
dea31012005-04-17 16:05:31 -05002452 break;
2453
James Smart92d7f7b2007-06-17 19:56:38 -05002454 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002455 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002456 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002457 /* Initial FLOGI timeout */
James Smarted957682007-06-17 19:56:37 -05002458 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002459 "%d (%d):0222 Initial %s timeout\n",
2460 phba->brd_no, vport->vpi,
2461 vport->vpi ? "FLOGI" : "FDISC");
dea31012005-04-17 16:05:31 -05002462
2463 /* Assume no Fabric and go on with discovery.
2464 * Check for outstanding ELS FLOGI to abort.
2465 */
2466
2467 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002468 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002469
2470 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002471 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002472 break;
2473
2474 case LPFC_FABRIC_CFG_LINK:
2475 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2476 NameServer login */
2477 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002478 "%d (%d):0223 Timeout while waiting for "
2479 "NameServer login\n",
2480 phba->brd_no, vport->vpi);
dea31012005-04-17 16:05:31 -05002481
2482 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002483 ndlp = lpfc_findnode_did(vport, NameServer_DID);
dea31012005-04-17 16:05:31 -05002484 if (ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04002485 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002486 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002487 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002488 break;
2489
2490 case LPFC_NS_QRY:
2491 /* Check for wait for NameServer Rsp timeout */
2492 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002493 "%d (%d):0224 NameServer Query timeout "
dea31012005-04-17 16:05:31 -05002494 "Data: x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002495 phba->brd_no, vport->vpi,
James Smart2e0fef82007-06-17 19:56:36 -05002496 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002497
James Smart92d7f7b2007-06-17 19:56:38 -05002498 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2499 /* Try it one more time */
2500 vport->fc_ns_retry++;
2501 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2502 vport->fc_ns_retry, 0);
2503 if (rc == 0)
2504 break;
dea31012005-04-17 16:05:31 -05002505 }
James Smart92d7f7b2007-06-17 19:56:38 -05002506 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002507
James Smart92d7f7b2007-06-17 19:56:38 -05002508 /*
2509 * Discovery is over.
2510 * set port_state to PORT_READY if SLI2.
2511 * cmpl_reg_vpi will set port_state to READY for SLI3.
2512 */
2513 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2514 lpfc_issue_reg_vpi(phba, vport);
2515 else { /* NPIV Not enabled */
2516 lpfc_issue_clear_la(phba, vport);
2517 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002518 }
2519
2520 /* Setup and issue mailbox INITIALIZE LINK command */
2521 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2522 if (!initlinkmbox) {
2523 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002524 "%d (%d):0206 Device Discovery "
dea31012005-04-17 16:05:31 -05002525 "completion error\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002526 phba->brd_no, vport->vpi);
James Smart2e0fef82007-06-17 19:56:36 -05002527 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002528 break;
2529 }
2530
2531 lpfc_linkdown(phba);
2532 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2533 phba->cfg_link_speed);
2534 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002535 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002536 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
dea31012005-04-17 16:05:31 -05002537 rc = lpfc_sli_issue_mbox(phba, initlinkmbox,
2538 (MBX_NOWAIT | MBX_STOP_IOCB));
James Smart5b8bd0c2007-04-25 09:52:49 -04002539 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002540 if (rc == MBX_NOT_FINISHED)
2541 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2542
2543 break;
2544
2545 case LPFC_DISC_AUTH:
2546 /* Node Authentication timeout */
James Smarted957682007-06-17 19:56:37 -05002547 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002548 "%d (%d):0227 Node Authentication timeout\n",
2549 phba->brd_no, vport->vpi);
James Smart2e0fef82007-06-17 19:56:36 -05002550 lpfc_disc_flush_list(vport);
2551
James Smart92d7f7b2007-06-17 19:56:38 -05002552 /*
2553 * set port_state to PORT_READY if SLI2.
2554 * cmpl_reg_vpi will set port_state to READY for SLI3.
2555 */
2556 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2557 lpfc_issue_reg_vpi(phba, vport);
2558 else { /* NPIV Not enabled */
2559 lpfc_issue_clear_la(phba, vport);
2560 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002561 }
2562 break;
2563
James Smart2e0fef82007-06-17 19:56:36 -05002564 case LPFC_VPORT_READY:
2565 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarted957682007-06-17 19:56:37 -05002566 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002567 "%d (%d):0231 RSCN timeout Data: x%x "
2568 "x%x\n",
2569 phba->brd_no, vport->vpi,
James Smart2e0fef82007-06-17 19:56:36 -05002570 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002571
2572 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05002573 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05002574
James Smart2e0fef82007-06-17 19:56:36 -05002575 lpfc_els_flush_rscn(vport);
2576 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05002577 }
2578 break;
James Smart2e0fef82007-06-17 19:56:36 -05002579
James Smart92d7f7b2007-06-17 19:56:38 -05002580 default:
James Smarted957682007-06-17 19:56:37 -05002581 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002582 "%d (%d):0229 Unexpected discovery timeout, "
2583 "vport State x%x\n",
2584 phba->brd_no, vport->vpi, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05002585
2586 break;
2587 }
2588
2589 switch (phba->link_state) {
2590 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05002591 /* CLEAR LA timeout */
James Smarted957682007-06-17 19:56:37 -05002592 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002593 "%d (%d):0228 CLEAR LA timeout\n",
2594 phba->brd_no, vport->vpi);
James Smart2e0fef82007-06-17 19:56:36 -05002595 clrlaerr = 1;
2596 break;
2597
2598 case LPFC_LINK_UNKNOWN:
2599 case LPFC_WARM_START:
2600 case LPFC_INIT_START:
2601 case LPFC_INIT_MBX_CMDS:
2602 case LPFC_LINK_DOWN:
2603 case LPFC_LINK_UP:
2604 case LPFC_HBA_ERROR:
James Smarted957682007-06-17 19:56:37 -05002605 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart92d7f7b2007-06-17 19:56:38 -05002606 "%d (%d):0230 Unexpected timeout, hba link "
James Smart2e0fef82007-06-17 19:56:36 -05002607 "state x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002608 phba->brd_no, vport->vpi, phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05002609 clrlaerr = 1;
2610 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002611
2612 case LPFC_HBA_READY:
2613 break;
dea31012005-04-17 16:05:31 -05002614 }
2615
2616 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05002617 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05002618 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002619 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2620 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05002621 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002622 }
2623
2624 return;
2625}
2626
dea31012005-04-17 16:05:31 -05002627/*
2628 * This routine handles processing a NameServer REG_LOGIN mailbox
2629 * command upon completion. It is setup in the LPFC_MBOXQ
2630 * as the completion routine when the command is
2631 * handed off to the SLI layer.
2632 */
2633void
James Smart2e0fef82007-06-17 19:56:36 -05002634lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002635{
James Smart2e0fef82007-06-17 19:56:36 -05002636 MAILBOX_t *mb = &pmb->mb;
2637 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2638 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2639 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002640
2641 pmb->context1 = NULL;
2642
dea31012005-04-17 16:05:31 -05002643 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002644 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002645 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002646
James Smart2e0fef82007-06-17 19:56:36 -05002647 /*
2648 * Start issuing Fabric-Device Management Interface (FDMI) command to
2649 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2650 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05002651 */
James Smart2e0fef82007-06-17 19:56:36 -05002652
2653 if (phba->cfg_fdmi_on == 1)
2654 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2655 else
2656 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05002657
James Smart329f9bc2007-04-25 09:53:01 -04002658 /* Mailbox took a reference to the node */
2659 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002660 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2661 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002662 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002663
2664 return;
2665}
2666
James Smart685f0bf2007-04-25 09:53:08 -04002667static int
2668lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2669{
2670 uint16_t *rpi = param;
2671
2672 return ndlp->nlp_rpi == *rpi;
2673}
2674
2675static int
2676lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2677{
2678 return memcmp(&ndlp->nlp_portname, param,
2679 sizeof(ndlp->nlp_portname)) == 0;
2680}
2681
dea31012005-04-17 16:05:31 -05002682struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002683__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05002684{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002685 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002686
James Smart2e0fef82007-06-17 19:56:36 -05002687 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart685f0bf2007-04-25 09:53:08 -04002688 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE &&
2689 filter(ndlp, param))
2690 return ndlp;
2691 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002692 return NULL;
dea31012005-04-17 16:05:31 -05002693}
2694
James Smart685f0bf2007-04-25 09:53:08 -04002695/*
2696 * Search node lists for a remote port matching filter criteria
James Smart92d7f7b2007-06-17 19:56:38 -05002697 * Caller needs to hold host_lock before calling this routine.
James Smart685f0bf2007-04-25 09:53:08 -04002698 */
2699struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002700lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
James Smart685f0bf2007-04-25 09:53:08 -04002701{
James Smart2e0fef82007-06-17 19:56:36 -05002702 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart685f0bf2007-04-25 09:53:08 -04002703 struct lpfc_nodelist *ndlp;
2704
James Smart2e0fef82007-06-17 19:56:36 -05002705 spin_lock_irq(shost->host_lock);
2706 ndlp = __lpfc_find_node(vport, filter, param);
2707 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002708 return ndlp;
2709}
2710
2711/*
2712 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05002713 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04002714 */
2715struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002716__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04002717{
James Smart2e0fef82007-06-17 19:56:36 -05002718 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04002719}
2720
James Smart2534ba72007-04-25 09:52:20 -04002721struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002722lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart2534ba72007-04-25 09:52:20 -04002723{
James Smart2e0fef82007-06-17 19:56:36 -05002724 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2534ba72007-04-25 09:52:20 -04002725 struct lpfc_nodelist *ndlp;
2726
James Smart2e0fef82007-06-17 19:56:36 -05002727 spin_lock_irq(shost->host_lock);
2728 ndlp = __lpfc_findnode_rpi(vport, rpi);
2729 spin_unlock_irq(shost->host_lock);
James Smart2534ba72007-04-25 09:52:20 -04002730 return ndlp;
2731}
2732
James Smart488d1462006-03-07 15:02:37 -05002733/*
James Smart685f0bf2007-04-25 09:53:08 -04002734 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05002735 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05002736 */
2737struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002738lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05002739{
James Smart2e0fef82007-06-17 19:56:36 -05002740 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05002741 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05002742
James Smart2e0fef82007-06-17 19:56:36 -05002743 spin_lock_irq(shost->host_lock);
2744 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2745 spin_unlock_irq(shost->host_lock);
James Smart488d1462006-03-07 15:02:37 -05002746 return NULL;
2747}
2748
dea31012005-04-17 16:05:31 -05002749void
James Smart92d7f7b2007-06-17 19:56:38 -05002750lpfc_dev_loss_delay(unsigned long ptr)
2751{
2752 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2753 struct lpfc_vport *vport = ndlp->vport;
2754 struct lpfc_hba *phba = vport->phba;
2755 struct lpfc_work_evt *evtp = &ndlp->dev_loss_evt;
2756 unsigned long flags;
2757
2758 evtp = &ndlp->dev_loss_evt;
2759
2760 spin_lock_irqsave(&phba->hbalock, flags);
2761 if (!list_empty(&evtp->evt_listp)) {
2762 spin_unlock_irqrestore(&phba->hbalock, flags);
2763 return;
2764 }
2765
2766 evtp->evt_arg1 = ndlp;
2767 evtp->evt = LPFC_EVT_DEV_LOSS;
2768 list_add_tail(&evtp->evt_listp, &phba->work_list);
2769 if (phba->work_wait)
2770 lpfc_worker_wake_up(phba);
2771 spin_unlock_irqrestore(&phba->hbalock, flags);
2772 return;
2773}
2774
2775void
James Smart2e0fef82007-06-17 19:56:36 -05002776lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2777 uint32_t did)
dea31012005-04-17 16:05:31 -05002778{
2779 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -05002780 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05002781 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2782 init_timer(&ndlp->nlp_initiator_tmr);
2783 ndlp->nlp_initiator_tmr.function = lpfc_dev_loss_delay;
2784 ndlp->nlp_initiator_tmr.data = (unsigned long)ndlp;
dea31012005-04-17 16:05:31 -05002785 init_timer(&ndlp->nlp_delayfunc);
2786 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2787 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2788 ndlp->nlp_DID = did;
James Smart2e0fef82007-06-17 19:56:36 -05002789 ndlp->vport = vport;
dea31012005-04-17 16:05:31 -05002790 ndlp->nlp_sid = NLP_NO_SID;
James Smart685f0bf2007-04-25 09:53:08 -04002791 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart329f9bc2007-04-25 09:53:01 -04002792 kref_init(&ndlp->kref);
dea31012005-04-17 16:05:31 -05002793 return;
2794}
James Smart329f9bc2007-04-25 09:53:01 -04002795
2796void
2797lpfc_nlp_release(struct kref *kref)
2798{
2799 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
2800 kref);
James Smart2e0fef82007-06-17 19:56:36 -05002801 lpfc_nlp_remove(ndlp->vport, ndlp);
2802 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04002803}
2804
2805struct lpfc_nodelist *
2806lpfc_nlp_get(struct lpfc_nodelist *ndlp)
2807{
2808 if (ndlp)
2809 kref_get(&ndlp->kref);
2810 return ndlp;
2811}
2812
2813int
2814lpfc_nlp_put(struct lpfc_nodelist *ndlp)
2815{
2816 return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
2817}