blob: 0ebde246300566813a748b4adbe11225c124abdb [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. *
Jamie Wellnitz41415862006-02-28 19:25:27 -05004 * Copyright (C) 2004-2006 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"
39
40/* AlpaArray for assignment of scsid for scan-down and bind_method */
41static uint8_t lpfcAlpaArray[] = {
42 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
43 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
44 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
45 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
46 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
47 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
48 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
49 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
50 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
51 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
52 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
53 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
54 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
55};
56
57static void lpfc_disc_timeout_handler(struct lpfc_hba *);
58
James Smartc01f3202006-08-18 17:47:08 -040059void
60lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050061{
James Smartc01f3202006-08-18 17:47:08 -040062 struct lpfc_rport_data *rdata;
63 struct lpfc_nodelist * ndlp;
64 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050065
James Smartc01f3202006-08-18 17:47:08 -040066 rdata = rport->dd_data;
67 ndlp = rdata->pnode;
68
69 if (!ndlp) {
70 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
71 printk(KERN_ERR "Cannot find remote node"
72 " to terminate I/O Data x%x\n",
73 rport->port_id);
dea31012005-04-17 16:05:31 -050074 return;
75 }
76
James Smartc01f3202006-08-18 17:47:08 -040077 phba = ndlp->nlp_phba;
James Smart1a169682006-03-07 15:04:06 -050078
James Smartc01f3202006-08-18 17:47:08 -040079 spin_lock_irq(phba->host->host_lock);
80 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 }
84 spin_unlock_irq(phba->host->host_lock);
85
86 return;
87}
88
89/*
90 * This function will be called when dev_loss_tmo fire.
91 */
92void
93lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
94{
95 struct lpfc_rport_data *rdata;
96 struct lpfc_nodelist * ndlp;
97 uint8_t *name;
98 int warn_on = 0;
99 struct lpfc_hba *phba;
100
101 rdata = rport->dd_data;
102 ndlp = rdata->pnode;
103
104 if (!ndlp) {
105 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
106 printk(KERN_ERR "Cannot find remote node"
107 " for rport in dev_loss_tmo_callbk x%x\n",
108 rport->port_id);
109 return;
110 }
111
James Smart82085712007-04-25 09:52:41 -0400112 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
113 return;
114
James Smartc01f3202006-08-18 17:47:08 -0400115 name = (uint8_t *)&ndlp->nlp_portname;
116 phba = ndlp->nlp_phba;
117
118 spin_lock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -0500119
120 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400121 warn_on = 1;
dea31012005-04-17 16:05:31 -0500122 /* flush the target */
123 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
124 ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
125 }
James Smartc01f3202006-08-18 17:47:08 -0400126 if (phba->fc_flag & FC_UNLOADING)
127 warn_on = 0;
128
dea31012005-04-17 16:05:31 -0500129 spin_unlock_irq(phba->host->host_lock);
130
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400131 if (warn_on) {
132 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smartc01f3202006-08-18 17:47:08 -0400133 "%d:0203 Devloss timeout on "
James Smart488d1462006-03-07 15:02:37 -0500134 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
135 "NPort x%x Data: x%x x%x x%x\n",
136 phba->brd_no,
137 *name, *(name+1), *(name+2), *(name+3),
138 *(name+4), *(name+5), *(name+6), *(name+7),
139 ndlp->nlp_DID, ndlp->nlp_flag,
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400140 ndlp->nlp_state, ndlp->nlp_rpi);
141 } else {
142 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
James Smartc01f3202006-08-18 17:47:08 -0400143 "%d:0204 Devloss timeout on "
James Smart488d1462006-03-07 15:02:37 -0500144 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
145 "NPort x%x Data: x%x x%x x%x\n",
146 phba->brd_no,
147 *name, *(name+1), *(name+2), *(name+3),
148 *(name+4), *(name+5), *(name+6), *(name+7),
149 ndlp->nlp_DID, ndlp->nlp_flag,
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400150 ndlp->nlp_state, ndlp->nlp_rpi);
151 }
152
James Smart1dcb58e2007-04-25 09:51:30 -0400153 if (!(phba->fc_flag & FC_UNLOADING) &&
154 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400155 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
156 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smartc01f3202006-08-18 17:47:08 -0400157 lpfc_disc_state_machine(phba, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart1dcb58e2007-04-25 09:51:30 -0400158 else {
159 rdata->pnode = NULL;
160 ndlp->rport = NULL;
James Smart329f9bc2007-04-25 09:53:01 -0400161 lpfc_nlp_put(ndlp);
162 put_device(&rport->dev);
James Smart1dcb58e2007-04-25 09:51:30 -0400163 }
James Smartc01f3202006-08-18 17:47:08 -0400164
dea31012005-04-17 16:05:31 -0500165 return;
166}
167
168static void
169lpfc_work_list_done(struct lpfc_hba * phba)
170{
171 struct lpfc_work_evt *evtp = NULL;
172 struct lpfc_nodelist *ndlp;
173 int free_evt;
174
175 spin_lock_irq(phba->host->host_lock);
176 while(!list_empty(&phba->work_list)) {
177 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
178 evt_listp);
179 spin_unlock_irq(phba->host->host_lock);
180 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500181 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500182 case LPFC_EVT_ELS_RETRY:
183 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
184 lpfc_els_retry_delay_handler(ndlp);
185 free_evt = 0;
186 break;
187 case LPFC_EVT_ONLINE:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500188 if (phba->hba_state < LPFC_LINK_DOWN)
189 *(int *)(evtp->evt_arg1) = lpfc_online(phba);
190 else
191 *(int *)(evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500192 complete((struct completion *)(evtp->evt_arg2));
193 break;
James Smart46fa3112007-04-25 09:51:45 -0400194 case LPFC_EVT_OFFLINE_PREP:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500195 if (phba->hba_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400196 lpfc_offline_prep(phba);
197 *(int *)(evtp->evt_arg1) = 0;
198 complete((struct completion *)(evtp->evt_arg2));
199 break;
200 case LPFC_EVT_OFFLINE:
201 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500202 lpfc_sli_brdrestart(phba);
203 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400204 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
205 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500206 complete((struct completion *)(evtp->evt_arg2));
207 break;
208 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400209 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500210 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500211 lpfc_sli_brdreset(phba);
212 lpfc_hba_down_post(phba);
213 *(int *)(evtp->evt_arg1) =
214 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400215 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500216 complete((struct completion *)(evtp->evt_arg2));
217 break;
218 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400219 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500220 *(int *)(evtp->evt_arg1)
221 = (phba->stopped) ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400222 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500223 complete((struct completion *)(evtp->evt_arg2));
224 break;
225 }
226 if (free_evt)
227 kfree(evtp);
228 spin_lock_irq(phba->host->host_lock);
229 }
230 spin_unlock_irq(phba->host->host_lock);
231
232}
233
234static void
235lpfc_work_done(struct lpfc_hba * phba)
236{
237 struct lpfc_sli_ring *pring;
238 int i;
239 uint32_t ha_copy;
240 uint32_t control;
241 uint32_t work_hba_events;
242
243 spin_lock_irq(phba->host->host_lock);
244 ha_copy = phba->work_ha;
245 phba->work_ha = 0;
246 work_hba_events=phba->work_hba_events;
247 spin_unlock_irq(phba->host->host_lock);
248
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500249 if (ha_copy & HA_ERATT)
dea31012005-04-17 16:05:31 -0500250 lpfc_handle_eratt(phba);
251
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500252 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500253 lpfc_sli_handle_mb_event(phba);
254
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500255 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500256 lpfc_handle_latt(phba);
257
258 if (work_hba_events & WORKER_DISC_TMO)
259 lpfc_disc_timeout_handler(phba);
260
261 if (work_hba_events & WORKER_ELS_TMO)
262 lpfc_els_timeout_handler(phba);
263
264 if (work_hba_events & WORKER_MBOX_TMO)
265 lpfc_mbox_timeout_handler(phba);
266
267 if (work_hba_events & WORKER_FDMI_TMO)
268 lpfc_fdmi_tmo_handler(phba);
269
270 spin_lock_irq(phba->host->host_lock);
271 phba->work_hba_events &= ~work_hba_events;
272 spin_unlock_irq(phba->host->host_lock);
273
274 for (i = 0; i < phba->sli.num_rings; i++, ha_copy >>= 4) {
275 pring = &phba->sli.ring[i];
276 if ((ha_copy & HA_RXATT)
277 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
278 if (pring->flag & LPFC_STOP_IOCB_MASK) {
279 pring->flag |= LPFC_DEFERRED_RING_EVENT;
280 } else {
281 lpfc_sli_handle_slow_ring_event(phba, pring,
282 (ha_copy &
283 HA_RXMASK));
284 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
285 }
286 /*
287 * Turn on Ring interrupts
288 */
289 spin_lock_irq(phba->host->host_lock);
290 control = readl(phba->HCregaddr);
291 control |= (HC_R0INT_ENA << i);
292 writel(control, phba->HCregaddr);
293 readl(phba->HCregaddr); /* flush */
294 spin_unlock_irq(phba->host->host_lock);
295 }
296 }
297
298 lpfc_work_list_done (phba);
299
300}
301
302static int
303check_work_wait_done(struct lpfc_hba *phba) {
304
305 spin_lock_irq(phba->host->host_lock);
306 if (phba->work_ha ||
307 phba->work_hba_events ||
308 (!list_empty(&phba->work_list)) ||
309 kthread_should_stop()) {
310 spin_unlock_irq(phba->host->host_lock);
311 return 1;
312 } else {
313 spin_unlock_irq(phba->host->host_lock);
314 return 0;
315 }
316}
317
318int
319lpfc_do_work(void *p)
320{
321 struct lpfc_hba *phba = p;
322 int rc;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800323 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
dea31012005-04-17 16:05:31 -0500324
325 set_user_nice(current, -20);
326 phba->work_wait = &work_waitq;
327
328 while (1) {
329
330 rc = wait_event_interruptible(work_waitq,
331 check_work_wait_done(phba));
332 BUG_ON(rc);
333
334 if (kthread_should_stop())
335 break;
336
337 lpfc_work_done(phba);
338
339 }
340 phba->work_wait = NULL;
341 return 0;
342}
343
344/*
345 * This is only called to handle FC worker events. Since this a rare
346 * occurance, we allocate a struct lpfc_work_evt structure here instead of
347 * embedding it in the IOCB.
348 */
349int
350lpfc_workq_post_event(struct lpfc_hba * phba, void *arg1, void *arg2,
351 uint32_t evt)
352{
353 struct lpfc_work_evt *evtp;
354
355 /*
356 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
357 * be queued to worker thread for processing
358 */
359 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_KERNEL);
360 if (!evtp)
361 return 0;
362
363 evtp->evt_arg1 = arg1;
364 evtp->evt_arg2 = arg2;
365 evtp->evt = evt;
366
dea31012005-04-17 16:05:31 -0500367 spin_lock_irq(phba->host->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -0400368 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea31012005-04-17 16:05:31 -0500369 if (phba->work_wait)
370 wake_up(phba->work_wait);
371 spin_unlock_irq(phba->host->host_lock);
372
373 return 1;
374}
375
376int
James Smart685f0bf2007-04-25 09:53:08 -0400377lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500378{
379 struct lpfc_sli *psli;
380 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart685f0bf2007-04-25 09:53:08 -0400381 LPFC_MBOXQ_t *mb;
382 int rc;
dea31012005-04-17 16:05:31 -0500383
384 psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500385 /* sysfs or selective reset may call this routine to clean up */
Jamie Wellnitz5024ab12006-02-28 19:25:28 -0500386 if (phba->hba_state >= LPFC_LINK_DOWN) {
387 if (phba->hba_state == LPFC_LINK_DOWN)
388 return 0;
389
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500390 spin_lock_irq(phba->host->host_lock);
391 phba->hba_state = LPFC_LINK_DOWN;
392 spin_unlock_irq(phba->host->host_lock);
393 }
dea31012005-04-17 16:05:31 -0500394
James Smartd2873e42006-08-18 17:46:43 -0400395 fc_host_post_event(phba->host, fc_get_event_number(),
396 FCH_EVT_LINKDOWN, 0);
397
dea31012005-04-17 16:05:31 -0500398 /* Clean up any firmware default rpi's */
399 if ((mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
400 lpfc_unreg_did(phba, 0xffffffff, mb);
401 mb->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
402 if (lpfc_sli_issue_mbox(phba, mb, (MBX_NOWAIT | MBX_STOP_IOCB))
403 == MBX_NOT_FINISHED) {
404 mempool_free( mb, phba->mbox_mem_pool);
405 }
406 }
407
408 /* Cleanup any outstanding RSCN activity */
409 lpfc_els_flush_rscn(phba);
410
411 /* Cleanup any outstanding ELS commands */
412 lpfc_els_flush_cmd(phba);
413
James Smart685f0bf2007-04-25 09:53:08 -0400414 /*
415 * Issue a LINK DOWN event to all nodes.
416 */
417 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nodes, nlp_listp) {
418 /* free any ndlp's on unused list */
419 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
420 lpfc_drop_node(phba, ndlp);
421 else /* otherwise, force node recovery. */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500422 rc = lpfc_disc_state_machine(phba, ndlp, NULL,
James Smart685f0bf2007-04-25 09:53:08 -0400423 NLP_EVT_DEVICE_RECOVERY);
dea31012005-04-17 16:05:31 -0500424 }
425
dea31012005-04-17 16:05:31 -0500426 /* Setup myDID for link up if we are in pt2pt mode */
427 if (phba->fc_flag & FC_PT2PT) {
428 phba->fc_myDID = 0;
429 if ((mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
430 lpfc_config_link(phba, mb);
431 mb->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
432 if (lpfc_sli_issue_mbox
433 (phba, mb, (MBX_NOWAIT | MBX_STOP_IOCB))
434 == MBX_NOT_FINISHED) {
435 mempool_free( mb, phba->mbox_mem_pool);
436 }
437 }
438 spin_lock_irq(phba->host->host_lock);
439 phba->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
440 spin_unlock_irq(phba->host->host_lock);
441 }
442 spin_lock_irq(phba->host->host_lock);
443 phba->fc_flag &= ~FC_LBIT;
444 spin_unlock_irq(phba->host->host_lock);
445
446 /* Turn off discovery timer if its running */
447 lpfc_can_disctmo(phba);
448
449 /* Must process IOCBs on all rings to handle ABORTed I/Os */
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500450 return 0;
dea31012005-04-17 16:05:31 -0500451}
452
453static int
James Smart685f0bf2007-04-25 09:53:08 -0400454lpfc_linkup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500455{
456 struct lpfc_nodelist *ndlp, *next_ndlp;
457
James Smartd2873e42006-08-18 17:46:43 -0400458 fc_host_post_event(phba->host, fc_get_event_number(),
459 FCH_EVT_LINKUP, 0);
460
dea31012005-04-17 16:05:31 -0500461 spin_lock_irq(phba->host->host_lock);
462 phba->hba_state = LPFC_LINK_UP;
463 phba->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
464 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
465 phba->fc_flag |= FC_NDISC_ACTIVE;
466 phba->fc_ns_retry = 0;
467 spin_unlock_irq(phba->host->host_lock);
468
469
James Smart685f0bf2007-04-25 09:53:08 -0400470 if (phba->fc_flag & FC_LBIT) {
471 list_for_each_entry(ndlp, &phba->fc_nodes, nlp_listp) {
472 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500473 if (ndlp->nlp_type & NLP_FABRIC) {
James Smart685f0bf2007-04-25 09:53:08 -0400474 /*
475 * On Linkup its safe to clean up the
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500476 * ndlp from Fabric connections.
477 */
James Smartde0c5b32007-04-25 09:52:27 -0400478 lpfc_nlp_set_state(phba, ndlp,
James Smart685f0bf2007-04-25 09:53:08 -0400479 NLP_STE_UNUSED_NODE);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500480 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smart685f0bf2007-04-25 09:53:08 -0400481 /*
482 * Fail outstanding IO now since
483 * device is marked for PLOGI.
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500484 */
485 lpfc_unreg_rpi(phba, ndlp);
486 }
487 }
dea31012005-04-17 16:05:31 -0500488 }
489 }
490
491 /* free any ndlp's on unused list */
James Smart685f0bf2007-04-25 09:53:08 -0400492 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nodes,
493 nlp_listp) {
494 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
495 lpfc_drop_node(phba, ndlp);
496 }
dea31012005-04-17 16:05:31 -0500497
498 return 0;
499}
500
501/*
502 * This routine handles processing a CLEAR_LA mailbox
503 * command upon completion. It is setup in the LPFC_MBOXQ
504 * as the completion routine when the command is
505 * handed off to the SLI layer.
506 */
507void
508lpfc_mbx_cmpl_clear_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
509{
510 struct lpfc_sli *psli;
511 MAILBOX_t *mb;
512 uint32_t control;
513
514 psli = &phba->sli;
515 mb = &pmb->mb;
516 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500517 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500518 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
519 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
520
521 /* Check for error */
522 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
523 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
524 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
525 "%d:0320 CLEAR_LA mbxStatus error x%x hba "
526 "state x%x\n",
527 phba->brd_no, mb->mbxStatus, phba->hba_state);
528
529 phba->hba_state = LPFC_HBA_ERROR;
530 goto out;
531 }
532
533 if (phba->fc_flag & FC_ABORT_DISCOVERY)
534 goto out;
535
536 phba->num_disc_nodes = 0;
537 /* go thru NPR list and issue ELS PLOGIs */
538 if (phba->fc_npr_cnt) {
539 lpfc_els_disc_plogi(phba);
540 }
541
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500542 if (!phba->num_disc_nodes) {
dea31012005-04-17 16:05:31 -0500543 spin_lock_irq(phba->host->host_lock);
544 phba->fc_flag &= ~FC_NDISC_ACTIVE;
545 spin_unlock_irq(phba->host->host_lock);
546 }
547
548 phba->hba_state = LPFC_HBA_READY;
549
550out:
551 /* Device Discovery completes */
552 lpfc_printf_log(phba,
553 KERN_INFO,
554 LOG_DISCOVERY,
555 "%d:0225 Device Discovery completes\n",
556 phba->brd_no);
557
558 mempool_free( pmb, phba->mbox_mem_pool);
559
560 spin_lock_irq(phba->host->host_lock);
561 phba->fc_flag &= ~FC_ABORT_DISCOVERY;
562 if (phba->fc_flag & FC_ESTABLISH_LINK) {
563 phba->fc_flag &= ~FC_ESTABLISH_LINK;
564 }
565 spin_unlock_irq(phba->host->host_lock);
566
567 del_timer_sync(&phba->fc_estabtmo);
568
569 lpfc_can_disctmo(phba);
570
571 /* turn on Link Attention interrupts */
572 spin_lock_irq(phba->host->host_lock);
573 psli->sli_flag |= LPFC_PROCESS_LA;
574 control = readl(phba->HCregaddr);
575 control |= HC_LAINT_ENA;
576 writel(control, phba->HCregaddr);
577 readl(phba->HCregaddr); /* flush */
578 spin_unlock_irq(phba->host->host_lock);
579
580 return;
581}
582
583static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500584lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500585{
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500586 struct lpfc_sli *psli = &phba->sli;
587 int rc;
dea31012005-04-17 16:05:31 -0500588
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500589 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500590 goto out;
dea31012005-04-17 16:05:31 -0500591
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500592 mempool_free(pmb, phba->mbox_mem_pool);
593
594 if (phba->fc_topology == TOPOLOGY_LOOP &&
595 phba->fc_flag & FC_PUBLIC_LOOP &&
596 !(phba->fc_flag & FC_LBIT)) {
597 /* Need to wait for FAN - use discovery timer
598 * for timeout. hba_state is identically
599 * LPFC_LOCAL_CFG_LINK while waiting for FAN
600 */
601 lpfc_set_disctmo(phba);
602 return;
dea31012005-04-17 16:05:31 -0500603 }
604
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500605 /* Start discovery by sending a FLOGI. hba_state is identically
606 * LPFC_FLOGI while waiting for FLOGI cmpl
607 */
608 phba->hba_state = LPFC_FLOGI;
609 lpfc_set_disctmo(phba);
610 lpfc_initial_flogi(phba);
611 return;
dea31012005-04-17 16:05:31 -0500612
613out:
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500614 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
615 "%d:0306 CONFIG_LINK mbxStatus error x%x "
616 "HBA state x%x\n",
617 phba->brd_no, pmb->mb.mbxStatus, phba->hba_state);
618
619 lpfc_linkdown(phba);
620
621 phba->hba_state = LPFC_HBA_ERROR;
622
623 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
dea31012005-04-17 16:05:31 -0500624 "%d:0200 CONFIG_LINK bad hba state x%x\n",
625 phba->brd_no, phba->hba_state);
626
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500627 lpfc_clear_la(phba, pmb);
628 pmb->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
629 rc = lpfc_sli_issue_mbox(phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
630 if (rc == MBX_NOT_FINISHED) {
631 mempool_free(pmb, phba->mbox_mem_pool);
632 lpfc_disc_flush_list(phba);
James Smarta4bc3372006-12-02 13:34:16 -0500633 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500634 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
635 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
636 phba->hba_state = LPFC_HBA_READY;
dea31012005-04-17 16:05:31 -0500637 }
638 return;
639}
640
641static void
642lpfc_mbx_cmpl_read_sparam(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
643{
644 struct lpfc_sli *psli = &phba->sli;
645 MAILBOX_t *mb = &pmb->mb;
646 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
647
648
649 /* Check for error */
650 if (mb->mbxStatus) {
651 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
652 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
653 "%d:0319 READ_SPARAM mbxStatus error x%x "
654 "hba state x%x>\n",
655 phba->brd_no, mb->mbxStatus, phba->hba_state);
656
657 lpfc_linkdown(phba);
658 phba->hba_state = LPFC_HBA_ERROR;
659 goto out;
660 }
661
662 memcpy((uint8_t *) & phba->fc_sparam, (uint8_t *) mp->virt,
663 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500664 if (phba->cfg_soft_wwnn)
665 u64_to_wwn(phba->cfg_soft_wwnn, phba->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400666 if (phba->cfg_soft_wwpn)
667 u64_to_wwn(phba->cfg_soft_wwpn, phba->fc_sparam.portName.u.wwn);
dea31012005-04-17 16:05:31 -0500668 memcpy((uint8_t *) & phba->fc_nodename,
669 (uint8_t *) & phba->fc_sparam.nodeName,
670 sizeof (struct lpfc_name));
671 memcpy((uint8_t *) & phba->fc_portname,
672 (uint8_t *) & phba->fc_sparam.portName,
673 sizeof (struct lpfc_name));
674 lpfc_mbuf_free(phba, mp->virt, mp->phys);
675 kfree(mp);
676 mempool_free( pmb, phba->mbox_mem_pool);
677 return;
678
679out:
680 pmb->context1 = NULL;
681 lpfc_mbuf_free(phba, mp->virt, mp->phys);
682 kfree(mp);
683 if (phba->hba_state != LPFC_CLEAR_LA) {
684 lpfc_clear_la(phba, pmb);
685 pmb->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
686 if (lpfc_sli_issue_mbox(phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB))
687 == MBX_NOT_FINISHED) {
688 mempool_free( pmb, phba->mbox_mem_pool);
689 lpfc_disc_flush_list(phba);
James Smarta4bc3372006-12-02 13:34:16 -0500690 psli->ring[(psli->extra_ring)].flag &=
dea31012005-04-17 16:05:31 -0500691 ~LPFC_STOP_IOCB_EVENT;
692 psli->ring[(psli->fcp_ring)].flag &=
693 ~LPFC_STOP_IOCB_EVENT;
694 psli->ring[(psli->next_ring)].flag &=
695 ~LPFC_STOP_IOCB_EVENT;
696 phba->hba_state = LPFC_HBA_READY;
697 }
698 } else {
699 mempool_free( pmb, phba->mbox_mem_pool);
700 }
701 return;
702}
703
704static void
705lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
706{
707 int i;
708 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart14691152006-12-02 13:34:28 -0500709 struct lpfc_dmabuf *mp;
710 int rc;
711
dea31012005-04-17 16:05:31 -0500712 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
713 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
714
715 spin_lock_irq(phba->host->host_lock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500716 switch (la->UlnkSpeed) {
dea31012005-04-17 16:05:31 -0500717 case LA_1GHZ_LINK:
718 phba->fc_linkspeed = LA_1GHZ_LINK;
719 break;
720 case LA_2GHZ_LINK:
721 phba->fc_linkspeed = LA_2GHZ_LINK;
722 break;
723 case LA_4GHZ_LINK:
724 phba->fc_linkspeed = LA_4GHZ_LINK;
725 break;
726 default:
727 phba->fc_linkspeed = LA_UNKNW_LINK;
728 break;
729 }
730
731 phba->fc_topology = la->topology;
732
733 if (phba->fc_topology == TOPOLOGY_LOOP) {
734 /* Get Loop Map information */
735
736 if (la->il)
737 phba->fc_flag |= FC_LBIT;
738
739 phba->fc_myDID = la->granted_AL_PA;
740 i = la->un.lilpBde64.tus.f.bdeSize;
741
742 if (i == 0) {
743 phba->alpa_map[0] = 0;
744 } else {
745 if (phba->cfg_log_verbose & LOG_LINK_EVENT) {
746 int numalpa, j, k;
747 union {
748 uint8_t pamap[16];
749 struct {
750 uint32_t wd1;
751 uint32_t wd2;
752 uint32_t wd3;
753 uint32_t wd4;
754 } pa;
755 } un;
756 numalpa = phba->alpa_map[0];
757 j = 0;
758 while (j < numalpa) {
759 memset(un.pamap, 0, 16);
760 for (k = 1; j < numalpa; k++) {
761 un.pamap[k - 1] =
762 phba->alpa_map[j + 1];
763 j++;
764 if (k == 16)
765 break;
766 }
767 /* Link Up Event ALPA map */
768 lpfc_printf_log(phba,
769 KERN_WARNING,
770 LOG_LINK_EVENT,
771 "%d:1304 Link Up Event "
772 "ALPA map Data: x%x "
773 "x%x x%x x%x\n",
774 phba->brd_no,
775 un.pa.wd1, un.pa.wd2,
776 un.pa.wd3, un.pa.wd4);
777 }
778 }
779 }
780 } else {
781 phba->fc_myDID = phba->fc_pref_DID;
782 phba->fc_flag |= FC_LBIT;
783 }
784 spin_unlock_irq(phba->host->host_lock);
785
786 lpfc_linkup(phba);
787 if (sparam_mbox) {
788 lpfc_read_sparam(phba, sparam_mbox);
789 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart14691152006-12-02 13:34:28 -0500790 rc = lpfc_sli_issue_mbox(phba, sparam_mbox,
dea31012005-04-17 16:05:31 -0500791 (MBX_NOWAIT | MBX_STOP_IOCB));
James Smart14691152006-12-02 13:34:28 -0500792 if (rc == MBX_NOT_FINISHED) {
793 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
794 lpfc_mbuf_free(phba, mp->virt, mp->phys);
795 kfree(mp);
796 mempool_free(sparam_mbox, phba->mbox_mem_pool);
797 if (cfglink_mbox)
798 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
799 return;
800 }
dea31012005-04-17 16:05:31 -0500801 }
802
803 if (cfglink_mbox) {
804 phba->hba_state = LPFC_LOCAL_CFG_LINK;
805 lpfc_config_link(phba, cfglink_mbox);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500806 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart14691152006-12-02 13:34:28 -0500807 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox,
dea31012005-04-17 16:05:31 -0500808 (MBX_NOWAIT | MBX_STOP_IOCB));
James Smart14691152006-12-02 13:34:28 -0500809 if (rc == MBX_NOT_FINISHED)
810 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500811 }
812}
813
814static void
815lpfc_mbx_issue_link_down(struct lpfc_hba *phba) {
816 uint32_t control;
817 struct lpfc_sli *psli = &phba->sli;
818
819 lpfc_linkdown(phba);
820
821 /* turn on Link Attention interrupts - no CLEAR_LA needed */
822 spin_lock_irq(phba->host->host_lock);
823 psli->sli_flag |= LPFC_PROCESS_LA;
824 control = readl(phba->HCregaddr);
825 control |= HC_LAINT_ENA;
826 writel(control, phba->HCregaddr);
827 readl(phba->HCregaddr); /* flush */
828 spin_unlock_irq(phba->host->host_lock);
829}
830
831/*
832 * This routine handles processing a READ_LA mailbox
833 * command upon completion. It is setup in the LPFC_MBOXQ
834 * as the completion routine when the command is
835 * handed off to the SLI layer.
836 */
837void
838lpfc_mbx_cmpl_read_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
839{
840 READ_LA_VAR *la;
841 MAILBOX_t *mb = &pmb->mb;
842 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
843
844 /* Check for error */
845 if (mb->mbxStatus) {
846 lpfc_printf_log(phba,
847 KERN_INFO,
848 LOG_LINK_EVENT,
849 "%d:1307 READ_LA mbox error x%x state x%x\n",
850 phba->brd_no,
851 mb->mbxStatus, phba->hba_state);
852 lpfc_mbx_issue_link_down(phba);
853 phba->hba_state = LPFC_HBA_ERROR;
854 goto lpfc_mbx_cmpl_read_la_free_mbuf;
855 }
856
857 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
858
859 memcpy(&phba->alpa_map[0], mp->virt, 128);
860
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500861 spin_lock_irq(phba->host->host_lock);
862 if (la->pb)
863 phba->fc_flag |= FC_BYPASSED_MODE;
864 else
865 phba->fc_flag &= ~FC_BYPASSED_MODE;
866 spin_unlock_irq(phba->host->host_lock);
867
dea31012005-04-17 16:05:31 -0500868 if (((phba->fc_eventTag + 1) < la->eventTag) ||
869 (phba->fc_eventTag == la->eventTag)) {
870 phba->fc_stat.LinkMultiEvent++;
871 if (la->attType == AT_LINK_UP) {
872 if (phba->fc_eventTag != 0)
873 lpfc_linkdown(phba);
874 }
875 }
876
877 phba->fc_eventTag = la->eventTag;
878
879 if (la->attType == AT_LINK_UP) {
880 phba->fc_stat.LinkUp++;
James Smart5b8bd0c2007-04-25 09:52:49 -0400881 if (phba->fc_flag & FC_LOOPBACK_MODE) {
882 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
883 "%d:1306 Link Up Event in loop back mode "
884 "x%x received Data: x%x x%x x%x x%x\n",
885 phba->brd_no, la->eventTag, phba->fc_eventTag,
886 la->granted_AL_PA, la->UlnkSpeed,
887 phba->alpa_map[0]);
888 } else {
889 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
dea31012005-04-17 16:05:31 -0500890 "%d:1303 Link Up Event x%x received "
891 "Data: x%x x%x x%x x%x\n",
892 phba->brd_no, la->eventTag, phba->fc_eventTag,
893 la->granted_AL_PA, la->UlnkSpeed,
894 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -0400895 }
dea31012005-04-17 16:05:31 -0500896 lpfc_mbx_process_link_up(phba, la);
897 } else {
898 phba->fc_stat.LinkDown++;
899 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
900 "%d:1305 Link Down Event x%x received "
901 "Data: x%x x%x x%x\n",
902 phba->brd_no, la->eventTag, phba->fc_eventTag,
903 phba->hba_state, phba->fc_flag);
904 lpfc_mbx_issue_link_down(phba);
905 }
906
907lpfc_mbx_cmpl_read_la_free_mbuf:
908 lpfc_mbuf_free(phba, mp->virt, mp->phys);
909 kfree(mp);
910 mempool_free(pmb, phba->mbox_mem_pool);
911 return;
912}
913
914/*
915 * This routine handles processing a REG_LOGIN mailbox
916 * command upon completion. It is setup in the LPFC_MBOXQ
917 * as the completion routine when the command is
918 * handed off to the SLI layer.
919 */
920void
921lpfc_mbx_cmpl_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
922{
923 struct lpfc_sli *psli;
924 MAILBOX_t *mb;
925 struct lpfc_dmabuf *mp;
926 struct lpfc_nodelist *ndlp;
927
928 psli = &phba->sli;
929 mb = &pmb->mb;
930
931 ndlp = (struct lpfc_nodelist *) pmb->context2;
932 mp = (struct lpfc_dmabuf *) (pmb->context1);
933
934 pmb->context1 = NULL;
935
936 /* Good status, call state machine */
937 lpfc_disc_state_machine(phba, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
938 lpfc_mbuf_free(phba, mp->virt, mp->phys);
939 kfree(mp);
940 mempool_free( pmb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -0400941 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500942
943 return;
944}
945
946/*
947 * This routine handles processing a Fabric REG_LOGIN mailbox
948 * command upon completion. It is setup in the LPFC_MBOXQ
949 * as the completion routine when the command is
950 * handed off to the SLI layer.
951 */
952void
953lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
954{
955 struct lpfc_sli *psli;
956 MAILBOX_t *mb;
957 struct lpfc_dmabuf *mp;
958 struct lpfc_nodelist *ndlp;
959 struct lpfc_nodelist *ndlp_fdmi;
960
961
962 psli = &phba->sli;
963 mb = &pmb->mb;
964
965 ndlp = (struct lpfc_nodelist *) pmb->context2;
966 mp = (struct lpfc_dmabuf *) (pmb->context1);
967
James Smart329f9bc2007-04-25 09:53:01 -0400968 pmb->context1 = NULL;
969 pmb->context2 = NULL;
970
dea31012005-04-17 16:05:31 -0500971 if (mb->mbxStatus) {
972 lpfc_mbuf_free(phba, mp->virt, mp->phys);
973 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -0400974 mempool_free(pmb, phba->mbox_mem_pool);
975 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500976
977 /* FLOGI failed, so just use loop map to make discovery list */
978 lpfc_disc_list_loopmap(phba);
979
980 /* Start discovery */
981 lpfc_disc_start(phba);
982 return;
983 }
984
dea31012005-04-17 16:05:31 -0500985 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -0500986 ndlp->nlp_type |= NLP_FABRIC;
James Smartde0c5b32007-04-25 09:52:27 -0400987 lpfc_nlp_set_state(phba, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -0500988
James Smart329f9bc2007-04-25 09:53:01 -0400989 lpfc_nlp_put(ndlp); /* Drop the reference from the mbox */
990
dea31012005-04-17 16:05:31 -0500991 if (phba->hba_state == LPFC_FABRIC_CFG_LINK) {
992 /* This NPort has been assigned an NPort_ID by the fabric as a
993 * result of the completed fabric login. Issue a State Change
994 * Registration (SCR) ELS request to the fabric controller
995 * (SCR_DID) so that this NPort gets RSCN events from the
996 * fabric.
997 */
998 lpfc_issue_els_scr(phba, SCR_DID, 0);
999
James Smart685f0bf2007-04-25 09:53:08 -04001000 ndlp = lpfc_findnode_did(phba, NameServer_DID);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001001 if (!ndlp) {
1002 /* Allocate a new node instance. If the pool is empty,
1003 * start the discovery process and skip the Nameserver
1004 * login process. This is attempted again later on.
1005 * Otherwise, issue a Port Login (PLOGI) to NameServer.
1006 */
1007 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1008 if (!ndlp) {
1009 lpfc_disc_start(phba);
1010 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1011 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001012 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001013 return;
1014 } else {
1015 lpfc_nlp_init(phba, ndlp, NameServer_DID);
1016 ndlp->nlp_type |= NLP_FABRIC;
1017 }
1018 }
James Smartde0c5b32007-04-25 09:52:27 -04001019 lpfc_nlp_set_state(phba, ndlp, NLP_STE_PLOGI_ISSUE);
James Smart488d1462006-03-07 15:02:37 -05001020 lpfc_issue_els_plogi(phba, NameServer_DID, 0);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001021 if (phba->cfg_fdmi_on) {
1022 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
1023 GFP_KERNEL);
1024 if (ndlp_fdmi) {
1025 lpfc_nlp_init(phba, ndlp_fdmi, FDMI_DID);
1026 ndlp_fdmi->nlp_type |= NLP_FABRIC;
1027 ndlp_fdmi->nlp_state = NLP_STE_PLOGI_ISSUE;
James Smart488d1462006-03-07 15:02:37 -05001028 lpfc_issue_els_plogi(phba, FDMI_DID, 0);
dea31012005-04-17 16:05:31 -05001029 }
1030 }
1031 }
1032
1033 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1034 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001035 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001036 return;
1037}
1038
1039/*
1040 * This routine handles processing a NameServer REG_LOGIN mailbox
1041 * command upon completion. It is setup in the LPFC_MBOXQ
1042 * as the completion routine when the command is
1043 * handed off to the SLI layer.
1044 */
1045void
1046lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1047{
1048 struct lpfc_sli *psli;
1049 MAILBOX_t *mb;
1050 struct lpfc_dmabuf *mp;
1051 struct lpfc_nodelist *ndlp;
1052
1053 psli = &phba->sli;
1054 mb = &pmb->mb;
1055
1056 ndlp = (struct lpfc_nodelist *) pmb->context2;
1057 mp = (struct lpfc_dmabuf *) (pmb->context1);
1058
1059 if (mb->mbxStatus) {
James Smart329f9bc2007-04-25 09:53:01 -04001060 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001061 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1062 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001063 mempool_free(pmb, phba->mbox_mem_pool);
1064 lpfc_drop_node(phba, ndlp);
dea31012005-04-17 16:05:31 -05001065
1066 /* RegLogin failed, so just use loop map to make discovery
1067 list */
1068 lpfc_disc_list_loopmap(phba);
1069
1070 /* Start discovery */
1071 lpfc_disc_start(phba);
1072 return;
1073 }
1074
1075 pmb->context1 = NULL;
1076
dea31012005-04-17 16:05:31 -05001077 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001078 ndlp->nlp_type |= NLP_FABRIC;
James Smartde0c5b32007-04-25 09:52:27 -04001079 lpfc_nlp_set_state(phba, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001080
1081 if (phba->hba_state < LPFC_HBA_READY) {
1082 /* Link up discovery requires Fabrib registration. */
1083 lpfc_ns_cmd(phba, ndlp, SLI_CTNS_RNN_ID);
1084 lpfc_ns_cmd(phba, ndlp, SLI_CTNS_RSNN_NN);
1085 lpfc_ns_cmd(phba, ndlp, SLI_CTNS_RFT_ID);
James Smart2fb9bd82006-12-02 13:33:57 -05001086 lpfc_ns_cmd(phba, ndlp, SLI_CTNS_RFF_ID);
dea31012005-04-17 16:05:31 -05001087 }
1088
1089 phba->fc_ns_retry = 0;
1090 /* Good status, issue CT Request to NameServer */
1091 if (lpfc_ns_cmd(phba, ndlp, SLI_CTNS_GID_FT)) {
1092 /* Cannot issue NameServer Query, so finish up discovery */
1093 lpfc_disc_start(phba);
1094 }
1095
James Smart329f9bc2007-04-25 09:53:01 -04001096 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001097 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1098 kfree(mp);
1099 mempool_free( pmb, phba->mbox_mem_pool);
1100
1101 return;
1102}
1103
1104static void
James Smart329f9bc2007-04-25 09:53:01 -04001105lpfc_register_remote_port(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001106{
1107 struct fc_rport *rport;
1108 struct lpfc_rport_data *rdata;
1109 struct fc_rport_identifiers rport_ids;
dea31012005-04-17 16:05:31 -05001110
1111 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001112 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1113 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001114 rport_ids.port_id = ndlp->nlp_DID;
1115 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001116
James Smart329f9bc2007-04-25 09:53:01 -04001117 /*
1118 * We leave our node pointer in rport->dd_data when we unregister a
1119 * FCP target port. But fc_remote_port_add zeros the space to which
1120 * rport->dd_data points. So, if we're reusing a previously
1121 * registered port, drop the reference that we took the last time we
1122 * registered the port.
1123 */
1124 if (ndlp->rport && ndlp->rport->dd_data &&
1125 *(struct lpfc_rport_data **) ndlp->rport->dd_data) {
1126 lpfc_nlp_put(ndlp);
1127 }
dea31012005-04-17 16:05:31 -05001128 ndlp->rport = rport = fc_remote_port_add(phba->host, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001129 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001130 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1131 "Warning: fc_remote_port_add failed\n");
1132 return;
1133 }
1134
1135 /* initialize static port data */
1136 rport->maxframe_size = ndlp->nlp_maxframe;
1137 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001138 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001139 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001140
1141 if (ndlp->nlp_type & NLP_FCP_TARGET)
1142 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1143 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1144 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1145
1146
1147 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1148 fc_remote_port_rolechg(rport, rport_ids.roles);
1149
James Smart071fbd3d2006-04-15 11:53:20 -04001150 if ((rport->scsi_target_id != -1) &&
James Smarte17da182006-07-06 15:49:25 -04001151 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001152 ndlp->nlp_sid = rport->scsi_target_id;
1153 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001154
1155 return;
1156}
1157
1158static void
James Smart329f9bc2007-04-25 09:53:01 -04001159lpfc_unregister_remote_port(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001160{
1161 struct fc_rport *rport = ndlp->rport;
1162 struct lpfc_rport_data *rdata = rport->dd_data;
1163
James Smartc01f3202006-08-18 17:47:08 -04001164 if (rport->scsi_target_id == -1) {
1165 ndlp->rport = NULL;
1166 rdata->pnode = NULL;
James Smart329f9bc2007-04-25 09:53:01 -04001167 lpfc_nlp_put(ndlp);
1168 put_device(&rport->dev);
James Smartc01f3202006-08-18 17:47:08 -04001169 }
1170
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001171 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001172
1173 return;
1174}
1175
James Smartde0c5b32007-04-25 09:52:27 -04001176static void
1177lpfc_nlp_counters(struct lpfc_hba *phba, int state, int count)
1178{
James Smart685f0bf2007-04-25 09:53:08 -04001179 spin_lock_irq(phba->host->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001180 switch (state) {
1181 case NLP_STE_UNUSED_NODE:
1182 phba->fc_unused_cnt += count;
1183 break;
1184 case NLP_STE_PLOGI_ISSUE:
1185 phba->fc_plogi_cnt += count;
1186 break;
1187 case NLP_STE_ADISC_ISSUE:
1188 phba->fc_adisc_cnt += count;
1189 break;
1190 case NLP_STE_REG_LOGIN_ISSUE:
1191 phba->fc_reglogin_cnt += count;
1192 break;
1193 case NLP_STE_PRLI_ISSUE:
1194 phba->fc_prli_cnt += count;
1195 break;
1196 case NLP_STE_UNMAPPED_NODE:
1197 phba->fc_unmap_cnt += count;
1198 break;
1199 case NLP_STE_MAPPED_NODE:
1200 phba->fc_map_cnt += count;
1201 break;
1202 case NLP_STE_NPR_NODE:
1203 phba->fc_npr_cnt += count;
1204 break;
1205 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001206 spin_unlock_irq(phba->host->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001207}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001208
James Smartde0c5b32007-04-25 09:52:27 -04001209static void
1210lpfc_nlp_state_cleanup(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1211 int old_state, int new_state)
1212{
1213 if (new_state == NLP_STE_UNMAPPED_NODE) {
1214 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1215 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1216 ndlp->nlp_type |= NLP_FC_NODE;
1217 }
1218 if (new_state == NLP_STE_MAPPED_NODE)
1219 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1220 if (new_state == NLP_STE_NPR_NODE)
1221 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001222
James Smartde0c5b32007-04-25 09:52:27 -04001223 /* Transport interface */
1224 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1225 old_state == NLP_STE_UNMAPPED_NODE)) {
1226 phba->nport_event_cnt++;
1227 lpfc_unregister_remote_port(phba, ndlp);
1228 }
dea31012005-04-17 16:05:31 -05001229
James Smartde0c5b32007-04-25 09:52:27 -04001230 if (new_state == NLP_STE_MAPPED_NODE ||
1231 new_state == NLP_STE_UNMAPPED_NODE) {
1232 phba->nport_event_cnt++;
dea31012005-04-17 16:05:31 -05001233 /*
1234 * Tell the fc transport about the port, if we haven't
1235 * already. If we have, and it's a scsi entity, be
1236 * sure to unblock any attached scsi devices
1237 */
James Smartde0c5b32007-04-25 09:52:27 -04001238 lpfc_register_remote_port(phba, ndlp);
1239 }
dea31012005-04-17 16:05:31 -05001240
1241 /*
1242 * if we added to Mapped list, but the remote port
1243 * registration failed or assigned a target id outside
1244 * our presentable range - move the node to the
1245 * Unmapped List
1246 */
James Smartde0c5b32007-04-25 09:52:27 -04001247 if (new_state == NLP_STE_MAPPED_NODE &&
1248 (!ndlp->rport ||
1249 ndlp->rport->scsi_target_id == -1 ||
1250 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
1251 spin_lock_irq(phba->host->host_lock);
1252 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
1253 spin_unlock_irq(phba->host->host_lock);
1254 lpfc_nlp_set_state(phba, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001255 }
James Smartde0c5b32007-04-25 09:52:27 -04001256}
1257
James Smart685f0bf2007-04-25 09:53:08 -04001258static char *
1259lpfc_nlp_state_name(char *buffer, size_t size, int state)
1260{
1261 static char *states[] = {
1262 [NLP_STE_UNUSED_NODE] = "UNUSED",
1263 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1264 [NLP_STE_ADISC_ISSUE] = "ADISC",
1265 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1266 [NLP_STE_PRLI_ISSUE] = "PRLI",
1267 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1268 [NLP_STE_MAPPED_NODE] = "MAPPED",
1269 [NLP_STE_NPR_NODE] = "NPR",
1270 };
1271
1272 if (state < ARRAY_SIZE(states) && states[state])
1273 strlcpy(buffer, states[state], size);
1274 else
1275 snprintf(buffer, size, "unknown (%d)", state);
1276 return buffer;
1277}
1278
James Smartde0c5b32007-04-25 09:52:27 -04001279void
1280lpfc_nlp_set_state(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int state)
1281{
1282 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001283 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001284
James Smart685f0bf2007-04-25 09:53:08 -04001285 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1286 "%d:0904 NPort state transition x%06x, %s -> %s\n",
1287 phba->brd_no,
1288 ndlp->nlp_DID,
1289 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1290 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smartde0c5b32007-04-25 09:52:27 -04001291 if (old_state == NLP_STE_NPR_NODE &&
1292 (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1293 state != NLP_STE_NPR_NODE)
1294 lpfc_cancel_retry_delay_tmo(phba, ndlp);
1295 if (old_state == NLP_STE_UNMAPPED_NODE) {
1296 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1297 ndlp->nlp_type &= ~NLP_FC_NODE;
1298 }
1299
James Smart685f0bf2007-04-25 09:53:08 -04001300 if (list_empty(&ndlp->nlp_listp)) {
1301 spin_lock_irq(phba->host->host_lock);
1302 list_add_tail(&ndlp->nlp_listp, &phba->fc_nodes);
1303 spin_unlock_irq(phba->host->host_lock);
1304 } else if (old_state)
James Smartde0c5b32007-04-25 09:52:27 -04001305 lpfc_nlp_counters(phba, old_state, -1);
1306
1307 ndlp->nlp_state = state;
James Smartde0c5b32007-04-25 09:52:27 -04001308 lpfc_nlp_counters(phba, state, 1);
James Smartde0c5b32007-04-25 09:52:27 -04001309 lpfc_nlp_state_cleanup(phba, ndlp, old_state, state);
1310}
1311
1312void
1313lpfc_dequeue_node(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1314{
1315 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
1316 lpfc_cancel_retry_delay_tmo(phba, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001317 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1318 lpfc_nlp_counters(phba, ndlp->nlp_state, -1);
James Smart685f0bf2007-04-25 09:53:08 -04001319 spin_lock_irq(phba->host->host_lock);
1320 list_del_init(&ndlp->nlp_listp);
James Smartde0c5b32007-04-25 09:52:27 -04001321 spin_unlock_irq(phba->host->host_lock);
1322 lpfc_nlp_state_cleanup(phba, ndlp, ndlp->nlp_state, 0);
1323}
1324
1325void
1326lpfc_drop_node(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1327{
1328 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
1329 lpfc_cancel_retry_delay_tmo(phba, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001330 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1331 lpfc_nlp_counters(phba, ndlp->nlp_state, -1);
James Smart685f0bf2007-04-25 09:53:08 -04001332 spin_lock_irq(phba->host->host_lock);
1333 list_del_init(&ndlp->nlp_listp);
James Smartde0c5b32007-04-25 09:52:27 -04001334 spin_unlock_irq(phba->host->host_lock);
James Smart329f9bc2007-04-25 09:53:01 -04001335 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001336}
1337
1338/*
1339 * Start / ReStart rescue timer for Discovery / RSCN handling
1340 */
1341void
1342lpfc_set_disctmo(struct lpfc_hba * phba)
1343{
1344 uint32_t tmo;
1345
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001346 if (phba->hba_state == LPFC_LOCAL_CFG_LINK) {
1347 /* For FAN, timeout should be greater then edtov */
1348 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1349 } else {
1350 /* Normal discovery timeout should be > then ELS/CT timeout
1351 * FC spec states we need 3 * ratov for CT requests
1352 */
1353 tmo = ((phba->fc_ratov * 3) + 3);
1354 }
dea31012005-04-17 16:05:31 -05001355
1356 mod_timer(&phba->fc_disctmo, jiffies + HZ * tmo);
1357 spin_lock_irq(phba->host->host_lock);
1358 phba->fc_flag |= FC_DISC_TMO;
1359 spin_unlock_irq(phba->host->host_lock);
1360
1361 /* Start Discovery Timer state <hba_state> */
1362 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1363 "%d:0247 Start Discovery Timer state x%x "
1364 "Data: x%x x%lx x%x x%x\n",
1365 phba->brd_no,
1366 phba->hba_state, tmo, (unsigned long)&phba->fc_disctmo,
1367 phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1368
1369 return;
1370}
1371
1372/*
1373 * Cancel rescue timer for Discovery / RSCN handling
1374 */
1375int
1376lpfc_can_disctmo(struct lpfc_hba * phba)
1377{
1378 /* Turn off discovery timer if its running */
1379 if (phba->fc_flag & FC_DISC_TMO) {
1380 spin_lock_irq(phba->host->host_lock);
1381 phba->fc_flag &= ~FC_DISC_TMO;
1382 spin_unlock_irq(phba->host->host_lock);
1383 del_timer_sync(&phba->fc_disctmo);
1384 phba->work_hba_events &= ~WORKER_DISC_TMO;
1385 }
1386
1387 /* Cancel Discovery Timer state <hba_state> */
1388 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1389 "%d:0248 Cancel Discovery Timer state x%x "
1390 "Data: x%x x%x x%x\n",
1391 phba->brd_no, phba->hba_state, phba->fc_flag,
1392 phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1393
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001394 return 0;
dea31012005-04-17 16:05:31 -05001395}
1396
1397/*
1398 * Check specified ring for outstanding IOCB on the SLI queue
1399 * Return true if iocb matches the specified nport
1400 */
1401int
1402lpfc_check_sli_ndlp(struct lpfc_hba * phba,
1403 struct lpfc_sli_ring * pring,
1404 struct lpfc_iocbq * iocb, struct lpfc_nodelist * ndlp)
1405{
1406 struct lpfc_sli *psli;
1407 IOCB_t *icmd;
1408
1409 psli = &phba->sli;
1410 icmd = &iocb->iocb;
1411 if (pring->ringno == LPFC_ELS_RING) {
1412 switch (icmd->ulpCommand) {
1413 case CMD_GEN_REQUEST64_CR:
1414 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001415 return 1;
dea31012005-04-17 16:05:31 -05001416 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04001417 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1418 return 1;
dea31012005-04-17 16:05:31 -05001419 case CMD_XMIT_ELS_RSP64_CX:
1420 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001421 return 1;
dea31012005-04-17 16:05:31 -05001422 }
James Smarta4bc3372006-12-02 13:34:16 -05001423 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05001424
1425 } else if (pring->ringno == psli->fcp_ring) {
1426 /* Skip match check if waiting to relogin to FCP target */
1427 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
1428 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001429 return 0;
dea31012005-04-17 16:05:31 -05001430 }
1431 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001432 return 1;
dea31012005-04-17 16:05:31 -05001433 }
1434 } else if (pring->ringno == psli->next_ring) {
1435
1436 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001437 return 0;
dea31012005-04-17 16:05:31 -05001438}
1439
1440/*
1441 * Free resources / clean up outstanding I/Os
1442 * associated with nlp_rpi in the LPFC_NODELIST entry.
1443 */
1444static int
1445lpfc_no_rpi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1446{
James Smart2534ba72007-04-25 09:52:20 -04001447 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001448 struct lpfc_sli *psli;
1449 struct lpfc_sli_ring *pring;
1450 struct lpfc_iocbq *iocb, *next_iocb;
1451 IOCB_t *icmd;
1452 uint32_t rpi, i;
1453
1454 /*
1455 * Everything that matches on txcmplq will be returned
1456 * by firmware with a no rpi error.
1457 */
1458 psli = &phba->sli;
1459 rpi = ndlp->nlp_rpi;
1460 if (rpi) {
1461 /* Now process each ring */
1462 for (i = 0; i < psli->num_rings; i++) {
1463 pring = &psli->ring[i];
1464
1465 spin_lock_irq(phba->host->host_lock);
1466 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
1467 list) {
1468 /*
1469 * Check to see if iocb matches the nport we are
1470 * looking for
1471 */
1472 if ((lpfc_check_sli_ndlp
1473 (phba, pring, iocb, ndlp))) {
1474 /* It matches, so deque and call compl
1475 with an error */
James Smart2534ba72007-04-25 09:52:20 -04001476 list_move_tail(&iocb->list,
1477 &completions);
dea31012005-04-17 16:05:31 -05001478 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001479 }
1480 }
1481 spin_unlock_irq(phba->host->host_lock);
1482
1483 }
1484 }
James Smart2534ba72007-04-25 09:52:20 -04001485
1486 while (!list_empty(&completions)) {
1487 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
1488 list_del(&iocb->list);
1489
1490 if (iocb->iocb_cmpl) {
1491 icmd = &iocb->iocb;
1492 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1493 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1494 (iocb->iocb_cmpl) (phba, iocb, iocb);
1495 } else
1496 lpfc_sli_release_iocbq(phba, iocb);
1497 }
1498
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001499 return 0;
dea31012005-04-17 16:05:31 -05001500}
1501
1502/*
1503 * Free rpi associated with LPFC_NODELIST entry.
1504 * This routine is called from lpfc_freenode(), when we are removing
1505 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1506 * LOGO that completes successfully, and we are waiting to PLOGI back
1507 * to the remote NPort. In addition, it is called after we receive
1508 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1509 * we are waiting to PLOGI back to the remote NPort.
1510 */
1511int
1512lpfc_unreg_rpi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1513{
1514 LPFC_MBOXQ_t *mbox;
1515 int rc;
1516
1517 if (ndlp->nlp_rpi) {
1518 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
1519 lpfc_unreg_login(phba, ndlp->nlp_rpi, mbox);
1520 mbox->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
1521 rc = lpfc_sli_issue_mbox
1522 (phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
1523 if (rc == MBX_NOT_FINISHED)
1524 mempool_free( mbox, phba->mbox_mem_pool);
1525 }
dea31012005-04-17 16:05:31 -05001526 lpfc_no_rpi(phba, ndlp);
1527 ndlp->nlp_rpi = 0;
1528 return 1;
1529 }
1530 return 0;
1531}
1532
1533/*
1534 * Free resources associated with LPFC_NODELIST entry
1535 * so it can be freed.
1536 */
1537static int
James Smart685f0bf2007-04-25 09:53:08 -04001538lpfc_cleanup_node(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
dea31012005-04-17 16:05:31 -05001539{
1540 LPFC_MBOXQ_t *mb;
1541 LPFC_MBOXQ_t *nextmb;
1542 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05001543
1544 /* Cleanup node for NPort <nlp_DID> */
1545 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1546 "%d:0900 Cleanup node for NPort x%x "
1547 "Data: x%x x%x x%x\n",
1548 phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
1549 ndlp->nlp_state, ndlp->nlp_rpi);
1550
James Smartde0c5b32007-04-25 09:52:27 -04001551 lpfc_dequeue_node(phba, ndlp);
dea31012005-04-17 16:05:31 -05001552
dea31012005-04-17 16:05:31 -05001553 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1554 if ((mb = phba->sli.mbox_active)) {
1555 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1556 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1557 mb->context2 = NULL;
1558 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1559 }
1560 }
James Smart33ccf8d2006-08-17 11:57:58 -04001561
1562 spin_lock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -05001563 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1564 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1565 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1566 mp = (struct lpfc_dmabuf *) (mb->context1);
1567 if (mp) {
1568 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1569 kfree(mp);
1570 }
1571 list_del(&mb->list);
1572 mempool_free(mb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001573 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001574 }
1575 }
James Smart33ccf8d2006-08-17 11:57:58 -04001576 spin_unlock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -05001577
James Smart07951072007-04-25 09:51:38 -04001578 lpfc_els_abort(phba,ndlp);
dea31012005-04-17 16:05:31 -05001579 spin_lock_irq(phba->host->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04001580 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
dea31012005-04-17 16:05:31 -05001581 spin_unlock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -05001582
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05001583 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05001584 del_timer_sync(&ndlp->nlp_delayfunc);
1585
dea31012005-04-17 16:05:31 -05001586 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1587 list_del_init(&ndlp->els_retry_evt.evt_listp);
1588
1589 lpfc_unreg_rpi(phba, ndlp);
1590
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001591 return 0;
dea31012005-04-17 16:05:31 -05001592}
1593
1594/*
1595 * Check to see if we can free the nlp back to the freelist.
1596 * If we are in the middle of using the nlp in the discovery state
1597 * machine, defer the free till we reach the end of the state machine.
1598 */
James Smart329f9bc2007-04-25 09:53:01 -04001599static void
1600lpfc_nlp_remove(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001601{
James Smart1dcb58e2007-04-25 09:51:30 -04001602 struct lpfc_rport_data *rdata;
dea31012005-04-17 16:05:31 -05001603
1604 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
James Smartfdcebe22006-03-07 15:04:01 -05001605 lpfc_cancel_retry_delay_tmo(phba, ndlp);
dea31012005-04-17 16:05:31 -05001606 }
1607
James Smart685f0bf2007-04-25 09:53:08 -04001608 lpfc_cleanup_node(phba, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04001609
James Smart329f9bc2007-04-25 09:53:01 -04001610 if ((ndlp->rport) && !(phba->fc_flag & FC_UNLOADING)) {
1611 put_device(&ndlp->rport->dev);
1612 rdata = ndlp->rport->dd_data;
1613 rdata->pnode = NULL;
1614 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05001615 }
dea31012005-04-17 16:05:31 -05001616}
1617
1618static int
James Smart685f0bf2007-04-25 09:53:08 -04001619lpfc_matchdid(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, uint32_t did)
dea31012005-04-17 16:05:31 -05001620{
1621 D_ID mydid;
1622 D_ID ndlpdid;
1623 D_ID matchdid;
1624
1625 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001626 return 0;
dea31012005-04-17 16:05:31 -05001627
1628 if (ndlp->nlp_DID == 0) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001629 return 0;
dea31012005-04-17 16:05:31 -05001630 }
1631
1632 /* First check for Direct match */
1633 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001634 return 1;
dea31012005-04-17 16:05:31 -05001635
1636 /* Next check for area/domain identically equals 0 match */
1637 mydid.un.word = phba->fc_myDID;
1638 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001639 return 0;
dea31012005-04-17 16:05:31 -05001640 }
1641
1642 matchdid.un.word = did;
1643 ndlpdid.un.word = ndlp->nlp_DID;
1644 if (matchdid.un.b.id == ndlpdid.un.b.id) {
1645 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
1646 (mydid.un.b.area == matchdid.un.b.area)) {
1647 if ((ndlpdid.un.b.domain == 0) &&
1648 (ndlpdid.un.b.area == 0)) {
1649 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001650 return 1;
dea31012005-04-17 16:05:31 -05001651 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001652 return 0;
dea31012005-04-17 16:05:31 -05001653 }
1654
1655 matchdid.un.word = ndlp->nlp_DID;
1656 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
1657 (mydid.un.b.area == ndlpdid.un.b.area)) {
1658 if ((matchdid.un.b.domain == 0) &&
1659 (matchdid.un.b.area == 0)) {
1660 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001661 return 1;
dea31012005-04-17 16:05:31 -05001662 }
1663 }
1664 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001665 return 0;
dea31012005-04-17 16:05:31 -05001666}
1667
James Smart685f0bf2007-04-25 09:53:08 -04001668/* Search for a nodelist entry */
dea31012005-04-17 16:05:31 -05001669struct lpfc_nodelist *
James Smart685f0bf2007-04-25 09:53:08 -04001670lpfc_findnode_did(struct lpfc_hba *phba, uint32_t did)
dea31012005-04-17 16:05:31 -05001671{
James Smart2fb9bd82006-12-02 13:33:57 -05001672 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05001673 uint32_t data1;
1674
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001675 spin_lock_irq(phba->host->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001676 list_for_each_entry(ndlp, &phba->fc_nodes, nlp_listp) {
1677 if (lpfc_matchdid(phba, ndlp, did)) {
1678 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1679 ((uint32_t) ndlp->nlp_xri << 16) |
1680 ((uint32_t) ndlp->nlp_type << 8) |
1681 ((uint32_t) ndlp->nlp_rpi & 0xff));
1682 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1683 "%d:0929 FIND node DID "
1684 " Data: x%p x%x x%x x%x\n",
1685 phba->brd_no,
1686 ndlp, ndlp->nlp_DID,
1687 ndlp->nlp_flag, data1);
1688 spin_unlock_irq(phba->host->host_lock);
1689 return ndlp;
dea31012005-04-17 16:05:31 -05001690 }
1691 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001692 spin_unlock_irq(phba->host->host_lock);
1693
dea31012005-04-17 16:05:31 -05001694 /* FIND node did <did> NOT FOUND */
James Smart2fb9bd82006-12-02 13:33:57 -05001695 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
James Smart685f0bf2007-04-25 09:53:08 -04001696 "%d:0932 FIND node did x%x NOT FOUND.\n",
1697 phba->brd_no, did);
dea31012005-04-17 16:05:31 -05001698 return NULL;
1699}
1700
1701struct lpfc_nodelist *
1702lpfc_setup_disc_node(struct lpfc_hba * phba, uint32_t did)
1703{
1704 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05001705
James Smart685f0bf2007-04-25 09:53:08 -04001706 ndlp = lpfc_findnode_did(phba, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001707 if (!ndlp) {
1708 if ((phba->fc_flag & FC_RSCN_MODE) &&
dea31012005-04-17 16:05:31 -05001709 ((lpfc_rscn_payload_check(phba, did) == 0)))
1710 return NULL;
1711 ndlp = (struct lpfc_nodelist *)
1712 mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1713 if (!ndlp)
1714 return NULL;
1715 lpfc_nlp_init(phba, ndlp, did);
James Smartde0c5b32007-04-25 09:52:27 -04001716 lpfc_nlp_set_state(phba, ndlp, NLP_STE_NPR_NODE);
dea31012005-04-17 16:05:31 -05001717 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1718 return ndlp;
1719 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001720 if (phba->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05001721 if (lpfc_rscn_payload_check(phba, did)) {
1722 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001723
1724 /* Since this node is marked for discovery,
1725 * delay timeout is not needed.
1726 */
James Smartfdcebe22006-03-07 15:04:01 -05001727 if (ndlp->nlp_flag & NLP_DELAY_TMO)
1728 lpfc_cancel_retry_delay_tmo(phba, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04001729 } else
dea31012005-04-17 16:05:31 -05001730 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001731 } else {
James Smart685f0bf2007-04-25 09:53:08 -04001732 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
1733 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE)
dea31012005-04-17 16:05:31 -05001734 return NULL;
James Smartde0c5b32007-04-25 09:52:27 -04001735 lpfc_nlp_set_state(phba, ndlp, NLP_STE_NPR_NODE);
dea31012005-04-17 16:05:31 -05001736 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1737 }
1738 return ndlp;
1739}
1740
1741/* Build a list of nodes to discover based on the loopmap */
1742void
1743lpfc_disc_list_loopmap(struct lpfc_hba * phba)
1744{
1745 int j;
1746 uint32_t alpa, index;
1747
1748 if (phba->hba_state <= LPFC_LINK_DOWN) {
1749 return;
1750 }
1751 if (phba->fc_topology != TOPOLOGY_LOOP) {
1752 return;
1753 }
1754
1755 /* Check for loop map present or not */
1756 if (phba->alpa_map[0]) {
1757 for (j = 1; j <= phba->alpa_map[0]; j++) {
1758 alpa = phba->alpa_map[j];
1759
1760 if (((phba->fc_myDID & 0xff) == alpa) || (alpa == 0)) {
1761 continue;
1762 }
1763 lpfc_setup_disc_node(phba, alpa);
1764 }
1765 } else {
1766 /* No alpamap, so try all alpa's */
1767 for (j = 0; j < FC_MAXLOOP; j++) {
1768 /* If cfg_scan_down is set, start from highest
1769 * ALPA (0xef) to lowest (0x1).
1770 */
1771 if (phba->cfg_scan_down)
1772 index = j;
1773 else
1774 index = FC_MAXLOOP - j - 1;
1775 alpa = lpfcAlpaArray[index];
1776 if ((phba->fc_myDID & 0xff) == alpa) {
1777 continue;
1778 }
1779
1780 lpfc_setup_disc_node(phba, alpa);
1781 }
1782 }
1783 return;
1784}
1785
1786/* Start Link up / RSCN discovery on NPR list */
1787void
1788lpfc_disc_start(struct lpfc_hba * phba)
1789{
1790 struct lpfc_sli *psli;
1791 LPFC_MBOXQ_t *mbox;
1792 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart685f0bf2007-04-25 09:53:08 -04001793 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05001794 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04001795 int did_changed;
dea31012005-04-17 16:05:31 -05001796 int rc;
1797
1798 psli = &phba->sli;
1799
1800 if (phba->hba_state <= LPFC_LINK_DOWN) {
1801 return;
1802 }
1803 if (phba->hba_state == LPFC_CLEAR_LA)
1804 clear_la_pending = 1;
1805 else
1806 clear_la_pending = 0;
1807
1808 if (phba->hba_state < LPFC_HBA_READY) {
1809 phba->hba_state = LPFC_DISC_AUTH;
1810 }
1811 lpfc_set_disctmo(phba);
1812
1813 if (phba->fc_prevDID == phba->fc_myDID) {
1814 did_changed = 0;
1815 } else {
1816 did_changed = 1;
1817 }
1818 phba->fc_prevDID = phba->fc_myDID;
1819 phba->num_disc_nodes = 0;
1820
1821 /* Start Discovery state <hba_state> */
1822 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1823 "%d:0202 Start Discovery hba state x%x "
1824 "Data: x%x x%x x%x\n",
1825 phba->brd_no, phba->hba_state, phba->fc_flag,
1826 phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1827
1828 /* If our did changed, we MUST do PLOGI */
James Smart685f0bf2007-04-25 09:53:08 -04001829 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nodes, nlp_listp) {
1830 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
1831 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
1832 did_changed) {
1833 spin_lock_irq(phba->host->host_lock);
1834 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1835 spin_unlock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -05001836 }
1837 }
1838
1839 /* First do ADISCs - if any */
1840 num_sent = lpfc_els_disc_adisc(phba);
1841
1842 if (num_sent)
1843 return;
1844
1845 if ((phba->hba_state < LPFC_HBA_READY) && (!clear_la_pending)) {
1846 /* If we get here, there is nothing to ADISC */
1847 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
1848 phba->hba_state = LPFC_CLEAR_LA;
1849 lpfc_clear_la(phba, mbox);
1850 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
1851 rc = lpfc_sli_issue_mbox(phba, mbox,
1852 (MBX_NOWAIT | MBX_STOP_IOCB));
1853 if (rc == MBX_NOT_FINISHED) {
1854 mempool_free( mbox, phba->mbox_mem_pool);
1855 lpfc_disc_flush_list(phba);
James Smarta4bc3372006-12-02 13:34:16 -05001856 psli->ring[(psli->extra_ring)].flag &=
dea31012005-04-17 16:05:31 -05001857 ~LPFC_STOP_IOCB_EVENT;
1858 psli->ring[(psli->fcp_ring)].flag &=
1859 ~LPFC_STOP_IOCB_EVENT;
1860 psli->ring[(psli->next_ring)].flag &=
1861 ~LPFC_STOP_IOCB_EVENT;
1862 phba->hba_state = LPFC_HBA_READY;
1863 }
1864 }
1865 } else {
1866 /* Next do PLOGIs - if any */
1867 num_sent = lpfc_els_disc_plogi(phba);
1868
1869 if (num_sent)
1870 return;
1871
1872 if (phba->fc_flag & FC_RSCN_MODE) {
1873 /* Check to see if more RSCNs came in while we
1874 * were processing this one.
1875 */
1876 if ((phba->fc_rscn_id_cnt == 0) &&
1877 (!(phba->fc_flag & FC_RSCN_DISCOVERY))) {
1878 spin_lock_irq(phba->host->host_lock);
1879 phba->fc_flag &= ~FC_RSCN_MODE;
1880 spin_unlock_irq(phba->host->host_lock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001881 } else
dea31012005-04-17 16:05:31 -05001882 lpfc_els_handle_rscn(phba);
1883 }
1884 }
1885 return;
1886}
1887
1888/*
1889 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
1890 * ring the match the sppecified nodelist.
1891 */
1892static void
1893lpfc_free_tx(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1894{
James Smart2534ba72007-04-25 09:52:20 -04001895 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001896 struct lpfc_sli *psli;
1897 IOCB_t *icmd;
1898 struct lpfc_iocbq *iocb, *next_iocb;
1899 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05001900
1901 psli = &phba->sli;
1902 pring = &psli->ring[LPFC_ELS_RING];
1903
1904 /* Error matching iocb on txq or txcmplq
1905 * First check the txq.
1906 */
James Smart2534ba72007-04-25 09:52:20 -04001907 spin_lock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -05001908 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
1909 if (iocb->context1 != ndlp) {
1910 continue;
1911 }
1912 icmd = &iocb->iocb;
1913 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
1914 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
1915
James Smart2534ba72007-04-25 09:52:20 -04001916 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05001917 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001918 }
1919 }
1920
1921 /* Next check the txcmplq */
1922 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1923 if (iocb->context1 != ndlp) {
1924 continue;
1925 }
1926 icmd = &iocb->iocb;
1927 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
1928 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
James Smart2534ba72007-04-25 09:52:20 -04001929 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05001930 }
1931 }
James Smart2534ba72007-04-25 09:52:20 -04001932 spin_unlock_irq(phba->host->host_lock);
1933
1934 while (!list_empty(&completions)) {
1935 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
1936 list_del(&iocb->list);
1937
1938 if (iocb->iocb_cmpl) {
1939 icmd = &iocb->iocb;
1940 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1941 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1942 (iocb->iocb_cmpl) (phba, iocb, iocb);
1943 } else
1944 lpfc_sli_release_iocbq(phba, iocb);
1945 }
dea31012005-04-17 16:05:31 -05001946
1947 return;
1948}
1949
1950void
1951lpfc_disc_flush_list(struct lpfc_hba * phba)
1952{
1953 struct lpfc_nodelist *ndlp, *next_ndlp;
1954
James Smart685f0bf2007-04-25 09:53:08 -04001955 if (phba->fc_plogi_cnt || phba->fc_adisc_cnt) {
1956 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nodes,
1957 nlp_listp) {
1958 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
1959 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
1960 lpfc_free_tx(phba, ndlp);
1961 lpfc_nlp_put(ndlp);
1962 }
dea31012005-04-17 16:05:31 -05001963 }
1964 }
dea31012005-04-17 16:05:31 -05001965}
1966
1967/*****************************************************************************/
1968/*
1969 * NAME: lpfc_disc_timeout
1970 *
1971 * FUNCTION: Fibre Channel driver discovery timeout routine.
1972 *
1973 * EXECUTION ENVIRONMENT: interrupt only
1974 *
1975 * CALLED FROM:
1976 * Timer function
1977 *
1978 * RETURNS:
1979 * none
1980 */
1981/*****************************************************************************/
1982void
1983lpfc_disc_timeout(unsigned long ptr)
1984{
1985 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
1986 unsigned long flags = 0;
1987
1988 if (unlikely(!phba))
1989 return;
1990
1991 spin_lock_irqsave(phba->host->host_lock, flags);
1992 if (!(phba->work_hba_events & WORKER_DISC_TMO)) {
1993 phba->work_hba_events |= WORKER_DISC_TMO;
1994 if (phba->work_wait)
1995 wake_up(phba->work_wait);
1996 }
1997 spin_unlock_irqrestore(phba->host->host_lock, flags);
1998 return;
1999}
2000
2001static void
2002lpfc_disc_timeout_handler(struct lpfc_hba *phba)
2003{
2004 struct lpfc_sli *psli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002005 struct lpfc_nodelist *ndlp, *next_ndlp;
dea31012005-04-17 16:05:31 -05002006 LPFC_MBOXQ_t *clearlambox, *initlinkmbox;
2007 int rc, clrlaerr = 0;
2008
2009 if (unlikely(!phba))
2010 return;
2011
2012 if (!(phba->fc_flag & FC_DISC_TMO))
2013 return;
2014
2015 psli = &phba->sli;
2016
2017 spin_lock_irq(phba->host->host_lock);
2018 phba->fc_flag &= ~FC_DISC_TMO;
2019 spin_unlock_irq(phba->host->host_lock);
2020
2021 switch (phba->hba_state) {
2022
2023 case LPFC_LOCAL_CFG_LINK:
2024 /* hba_state is identically LPFC_LOCAL_CFG_LINK while waiting for FAN */
2025 /* FAN timeout */
2026 lpfc_printf_log(phba,
2027 KERN_WARNING,
2028 LOG_DISCOVERY,
2029 "%d:0221 FAN timeout\n",
2030 phba->brd_no);
2031
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002032 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart685f0bf2007-04-25 09:53:08 -04002033 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nodes,
2034 nlp_listp) {
2035 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2036 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002037 if (ndlp->nlp_type & NLP_FABRIC) {
2038 /* Clean up the ndlp on Fabric connections */
James Smartde0c5b32007-04-25 09:52:27 -04002039 lpfc_drop_node(phba, ndlp);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002040 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002041 /* Fail outstanding IO now since device
2042 * is marked for PLOGI.
2043 */
2044 lpfc_unreg_rpi(phba, ndlp);
2045 }
2046 }
dea31012005-04-17 16:05:31 -05002047 phba->hba_state = LPFC_FLOGI;
2048 lpfc_set_disctmo(phba);
2049 lpfc_initial_flogi(phba);
2050 break;
2051
2052 case LPFC_FLOGI:
2053 /* hba_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
2054 /* Initial FLOGI timeout */
2055 lpfc_printf_log(phba,
2056 KERN_ERR,
2057 LOG_DISCOVERY,
2058 "%d:0222 Initial FLOGI timeout\n",
2059 phba->brd_no);
2060
2061 /* Assume no Fabric and go on with discovery.
2062 * Check for outstanding ELS FLOGI to abort.
2063 */
2064
2065 /* FLOGI failed, so just use loop map to make discovery list */
2066 lpfc_disc_list_loopmap(phba);
2067
2068 /* Start discovery */
2069 lpfc_disc_start(phba);
2070 break;
2071
2072 case LPFC_FABRIC_CFG_LINK:
2073 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2074 NameServer login */
2075 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2076 "%d:0223 Timeout while waiting for NameServer "
2077 "login\n", phba->brd_no);
2078
2079 /* Next look for NameServer ndlp */
James Smart685f0bf2007-04-25 09:53:08 -04002080 ndlp = lpfc_findnode_did(phba, NameServer_DID);
dea31012005-04-17 16:05:31 -05002081 if (ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04002082 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002083 /* Start discovery */
2084 lpfc_disc_start(phba);
2085 break;
2086
2087 case LPFC_NS_QRY:
2088 /* Check for wait for NameServer Rsp timeout */
2089 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2090 "%d:0224 NameServer Query timeout "
2091 "Data: x%x x%x\n",
2092 phba->brd_no,
2093 phba->fc_ns_retry, LPFC_MAX_NS_RETRY);
2094
James Smart685f0bf2007-04-25 09:53:08 -04002095 ndlp = lpfc_findnode_did(phba, NameServer_DID);
2096 if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
dea31012005-04-17 16:05:31 -05002097 if (phba->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2098 /* Try it one more time */
2099 rc = lpfc_ns_cmd(phba, ndlp, SLI_CTNS_GID_FT);
2100 if (rc == 0)
2101 break;
2102 }
2103 phba->fc_ns_retry = 0;
2104 }
2105
2106 /* Nothing to authenticate, so CLEAR_LA right now */
2107 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2108 if (!clearlambox) {
2109 clrlaerr = 1;
2110 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2111 "%d:0226 Device Discovery "
2112 "completion error\n",
2113 phba->brd_no);
2114 phba->hba_state = LPFC_HBA_ERROR;
2115 break;
2116 }
2117
2118 phba->hba_state = LPFC_CLEAR_LA;
2119 lpfc_clear_la(phba, clearlambox);
2120 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2121 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2122 (MBX_NOWAIT | MBX_STOP_IOCB));
2123 if (rc == MBX_NOT_FINISHED) {
2124 mempool_free(clearlambox, phba->mbox_mem_pool);
2125 clrlaerr = 1;
2126 break;
2127 }
2128
2129 /* Setup and issue mailbox INITIALIZE LINK command */
2130 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2131 if (!initlinkmbox) {
2132 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smartdca94792006-08-01 07:34:08 -04002133 "%d:0206 Device Discovery "
dea31012005-04-17 16:05:31 -05002134 "completion error\n",
2135 phba->brd_no);
2136 phba->hba_state = LPFC_HBA_ERROR;
2137 break;
2138 }
2139
2140 lpfc_linkdown(phba);
2141 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2142 phba->cfg_link_speed);
2143 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
2144 rc = lpfc_sli_issue_mbox(phba, initlinkmbox,
2145 (MBX_NOWAIT | MBX_STOP_IOCB));
James Smart5b8bd0c2007-04-25 09:52:49 -04002146 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002147 if (rc == MBX_NOT_FINISHED)
2148 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2149
2150 break;
2151
2152 case LPFC_DISC_AUTH:
2153 /* Node Authentication timeout */
2154 lpfc_printf_log(phba,
2155 KERN_ERR,
2156 LOG_DISCOVERY,
2157 "%d:0227 Node Authentication timeout\n",
2158 phba->brd_no);
2159 lpfc_disc_flush_list(phba);
2160 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2161 if (!clearlambox) {
2162 clrlaerr = 1;
2163 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smartdca94792006-08-01 07:34:08 -04002164 "%d:0207 Device Discovery "
dea31012005-04-17 16:05:31 -05002165 "completion error\n",
2166 phba->brd_no);
2167 phba->hba_state = LPFC_HBA_ERROR;
2168 break;
2169 }
2170 phba->hba_state = LPFC_CLEAR_LA;
2171 lpfc_clear_la(phba, clearlambox);
2172 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2173 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2174 (MBX_NOWAIT | MBX_STOP_IOCB));
2175 if (rc == MBX_NOT_FINISHED) {
2176 mempool_free(clearlambox, phba->mbox_mem_pool);
2177 clrlaerr = 1;
2178 }
2179 break;
2180
2181 case LPFC_CLEAR_LA:
2182 /* CLEAR LA timeout */
2183 lpfc_printf_log(phba,
2184 KERN_ERR,
2185 LOG_DISCOVERY,
2186 "%d:0228 CLEAR LA timeout\n",
2187 phba->brd_no);
2188 clrlaerr = 1;
2189 break;
2190
2191 case LPFC_HBA_READY:
2192 if (phba->fc_flag & FC_RSCN_MODE) {
2193 lpfc_printf_log(phba,
2194 KERN_ERR,
2195 LOG_DISCOVERY,
2196 "%d:0231 RSCN timeout Data: x%x x%x\n",
2197 phba->brd_no,
2198 phba->fc_ns_retry, LPFC_MAX_NS_RETRY);
2199
2200 /* Cleanup any outstanding ELS commands */
2201 lpfc_els_flush_cmd(phba);
2202
2203 lpfc_els_flush_rscn(phba);
2204 lpfc_disc_flush_list(phba);
2205 }
2206 break;
2207 }
2208
2209 if (clrlaerr) {
2210 lpfc_disc_flush_list(phba);
James Smarta4bc3372006-12-02 13:34:16 -05002211 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002212 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2213 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2214 phba->hba_state = LPFC_HBA_READY;
2215 }
2216
2217 return;
2218}
2219
dea31012005-04-17 16:05:31 -05002220/*
2221 * This routine handles processing a NameServer REG_LOGIN mailbox
2222 * command upon completion. It is setup in the LPFC_MBOXQ
2223 * as the completion routine when the command is
2224 * handed off to the SLI layer.
2225 */
2226void
2227lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
2228{
2229 struct lpfc_sli *psli;
2230 MAILBOX_t *mb;
2231 struct lpfc_dmabuf *mp;
2232 struct lpfc_nodelist *ndlp;
2233
2234 psli = &phba->sli;
2235 mb = &pmb->mb;
2236
2237 ndlp = (struct lpfc_nodelist *) pmb->context2;
2238 mp = (struct lpfc_dmabuf *) (pmb->context1);
2239
2240 pmb->context1 = NULL;
2241
dea31012005-04-17 16:05:31 -05002242 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002243 ndlp->nlp_type |= NLP_FABRIC;
James Smartde0c5b32007-04-25 09:52:27 -04002244 lpfc_nlp_set_state(phba, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002245
2246 /* Start issuing Fabric-Device Management Interface (FDMI)
2247 * command to 0xfffffa (FDMI well known port)
2248 */
2249 if (phba->cfg_fdmi_on == 1) {
2250 lpfc_fdmi_cmd(phba, ndlp, SLI_MGMT_DHBA);
2251 } else {
2252 /*
2253 * Delay issuing FDMI command if fdmi-on=2
2254 * (supporting RPA/hostnmae)
2255 */
2256 mod_timer(&phba->fc_fdmitmo, jiffies + HZ * 60);
2257 }
2258
James Smart329f9bc2007-04-25 09:53:01 -04002259 /* Mailbox took a reference to the node */
2260 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002261 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2262 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002263 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002264
2265 return;
2266}
2267
James Smart685f0bf2007-04-25 09:53:08 -04002268static int
2269lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2270{
2271 uint16_t *rpi = param;
2272
2273 return ndlp->nlp_rpi == *rpi;
2274}
2275
2276static int
2277lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2278{
2279 return memcmp(&ndlp->nlp_portname, param,
2280 sizeof(ndlp->nlp_portname)) == 0;
2281}
2282
dea31012005-04-17 16:05:31 -05002283/*
James Smart685f0bf2007-04-25 09:53:08 -04002284 * Search node lists for a remote port matching filter criteria
2285 * Caller needs to hold host_lock before calling this routine.
dea31012005-04-17 16:05:31 -05002286 */
2287struct lpfc_nodelist *
James Smart685f0bf2007-04-25 09:53:08 -04002288__lpfc_find_node(struct lpfc_hba *phba, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05002289{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002290 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002291
James Smart685f0bf2007-04-25 09:53:08 -04002292 list_for_each_entry(ndlp, &phba->fc_nodes, nlp_listp) {
2293 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE &&
2294 filter(ndlp, param))
2295 return ndlp;
2296 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002297 return NULL;
dea31012005-04-17 16:05:31 -05002298}
2299
James Smart685f0bf2007-04-25 09:53:08 -04002300/*
2301 * Search node lists for a remote port matching filter criteria
2302 * This routine is used when the caller does NOT have host_lock.
2303 */
2304struct lpfc_nodelist *
2305lpfc_find_node(struct lpfc_hba *phba, node_filter filter, void *param)
2306{
2307 struct lpfc_nodelist *ndlp;
2308
2309 spin_lock_irq(phba->host->host_lock);
2310 ndlp = __lpfc_find_node(phba, filter, param);
2311 spin_unlock_irq(phba->host->host_lock);
2312 return ndlp;
2313}
2314
2315/*
2316 * This routine looks up the ndlp lists for the given RPI. If rpi found it
2317 * returns the node list pointer else return NULL.
2318 */
2319struct lpfc_nodelist *
2320__lpfc_findnode_rpi(struct lpfc_hba *phba, uint16_t rpi)
2321{
2322 return __lpfc_find_node(phba, lpfc_filter_by_rpi, &rpi);
2323}
2324
James Smart2534ba72007-04-25 09:52:20 -04002325struct lpfc_nodelist *
2326lpfc_findnode_rpi(struct lpfc_hba * phba, uint16_t rpi)
2327{
2328 struct lpfc_nodelist *ndlp;
2329
2330 spin_lock_irq(phba->host->host_lock);
2331 ndlp = __lpfc_findnode_rpi(phba, rpi);
2332 spin_unlock_irq(phba->host->host_lock);
2333 return ndlp;
2334}
2335
James Smart488d1462006-03-07 15:02:37 -05002336/*
James Smart685f0bf2007-04-25 09:53:08 -04002337 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
2338 * returns the node list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05002339 */
2340struct lpfc_nodelist *
James Smart685f0bf2007-04-25 09:53:08 -04002341lpfc_findnode_wwpn(struct lpfc_hba *phba, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05002342{
2343 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05002344
2345 spin_lock_irq(phba->host->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002346 ndlp = __lpfc_find_node(phba, lpfc_filter_by_wwpn, wwpn);
James Smart488d1462006-03-07 15:02:37 -05002347 spin_unlock_irq(phba->host->host_lock);
2348 return NULL;
2349}
2350
dea31012005-04-17 16:05:31 -05002351void
James Smart329f9bc2007-04-25 09:53:01 -04002352lpfc_nlp_init(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, uint32_t did)
dea31012005-04-17 16:05:31 -05002353{
2354 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -05002355 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002356 init_timer(&ndlp->nlp_delayfunc);
2357 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2358 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2359 ndlp->nlp_DID = did;
2360 ndlp->nlp_phba = phba;
2361 ndlp->nlp_sid = NLP_NO_SID;
James Smart685f0bf2007-04-25 09:53:08 -04002362 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart329f9bc2007-04-25 09:53:01 -04002363 kref_init(&ndlp->kref);
dea31012005-04-17 16:05:31 -05002364 return;
2365}
James Smart329f9bc2007-04-25 09:53:01 -04002366
2367void
2368lpfc_nlp_release(struct kref *kref)
2369{
2370 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
2371 kref);
2372 lpfc_nlp_remove(ndlp->nlp_phba, ndlp);
2373 mempool_free(ndlp, ndlp->nlp_phba->nlp_mem_pool);
2374}
2375
2376struct lpfc_nodelist *
2377lpfc_nlp_get(struct lpfc_nodelist *ndlp)
2378{
2379 if (ndlp)
2380 kref_get(&ndlp->kref);
2381 return ndlp;
2382}
2383
2384int
2385lpfc_nlp_put(struct lpfc_nodelist *ndlp)
2386{
2387 return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
2388}