[SCSI] lpfc: NPIV: add SLI-3 interface
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
9413afff 4 * Copyright (C) 2004-2007 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * 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. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/kthread.h>
25#include <linux/interrupt.h>
26
91886523 27#include <scsi/scsi.h>
dea3101e 28#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
2e0fef85 57static void lpfc_disc_timeout_handler(struct lpfc_vport *);
dea3101e 58
c01f3208
JS
59void
60lpfc_terminate_rport_io(struct fc_rport *rport)
dea3101e 61{
c01f3208
JS
62 struct lpfc_rport_data *rdata;
63 struct lpfc_nodelist * ndlp;
64 struct lpfc_hba *phba;
dea3101e 65
c01f3208
JS
66 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);
dea3101e 74 return;
75 }
76
2e0fef85 77 phba = ndlp->vport->phba;
c01f3208 78
c01f3208
JS
79 if (ndlp->nlp_sid != NLP_NO_SID) {
80 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
81 ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
82 }
c01f3208
JS
83
84 return;
85}
86
87/*
88 * This function will be called when dev_loss_tmo fire.
89 */
90void
91lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
92{
93 struct lpfc_rport_data *rdata;
94 struct lpfc_nodelist * ndlp;
95 uint8_t *name;
96 int warn_on = 0;
97 struct lpfc_hba *phba;
2e0fef85 98 struct lpfc_vport *vport;
c01f3208
JS
99
100 rdata = rport->dd_data;
101 ndlp = rdata->pnode;
1a169689 102
c01f3208
JS
103 if (!ndlp) {
104 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
105 printk(KERN_ERR "Cannot find remote node"
106 " for rport in dev_loss_tmo_callbk x%x\n",
107 rport->port_id);
108 return;
109 }
110
82085718
JS
111 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
112 return;
113
c01f3208 114 name = (uint8_t *)&ndlp->nlp_portname;
2e0fef85
JS
115 vport = ndlp->vport;
116 phba = vport->phba;
dea3101e 117
118 if (ndlp->nlp_sid != NLP_NO_SID) {
6e8215e4 119 warn_on = 1;
dea3101e 120 /* flush the target */
121 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
122 ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
123 }
2e0fef85 124 if (vport->load_flag & FC_UNLOADING)
c01f3208
JS
125 warn_on = 0;
126
6e8215e4
JSEC
127 if (warn_on) {
128 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
c01f3208 129 "%d:0203 Devloss timeout on "
488d1469
JS
130 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
131 "NPort x%x Data: x%x x%x x%x\n",
132 phba->brd_no,
133 *name, *(name+1), *(name+2), *(name+3),
134 *(name+4), *(name+5), *(name+6), *(name+7),
135 ndlp->nlp_DID, ndlp->nlp_flag,
6e8215e4
JSEC
136 ndlp->nlp_state, ndlp->nlp_rpi);
137 } else {
138 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
c01f3208 139 "%d:0204 Devloss timeout on "
488d1469
JS
140 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
141 "NPort x%x Data: x%x x%x x%x\n",
142 phba->brd_no,
143 *name, *(name+1), *(name+2), *(name+3),
144 *(name+4), *(name+5), *(name+6), *(name+7),
145 ndlp->nlp_DID, ndlp->nlp_flag,
6e8215e4
JSEC
146 ndlp->nlp_state, ndlp->nlp_rpi);
147 }
148
2e0fef85 149 if (!(vport->load_flag & FC_UNLOADING) &&
1dcb58e5 150 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
82085718
JS
151 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
152 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
2e0fef85 153 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
1dcb58e5
JS
154 else {
155 rdata->pnode = NULL;
156 ndlp->rport = NULL;
329f9bc7
JS
157 lpfc_nlp_put(ndlp);
158 put_device(&rport->dev);
1dcb58e5 159 }
c01f3208 160
dea3101e 161 return;
162}
163
164static void
2e0fef85 165lpfc_work_list_done(struct lpfc_hba *phba)
dea3101e 166{
167 struct lpfc_work_evt *evtp = NULL;
168 struct lpfc_nodelist *ndlp;
169 int free_evt;
170
2e0fef85
JS
171 spin_lock_irq(&phba->hbalock);
172 while (!list_empty(&phba->work_list)) {
dea3101e 173 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
174 evt_listp);
2e0fef85 175 spin_unlock_irq(&phba->hbalock);
dea3101e 176 free_evt = 1;
2fe165b6 177 switch (evtp->evt) {
dea3101e 178 case LPFC_EVT_ELS_RETRY:
2e0fef85 179 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea3101e 180 lpfc_els_retry_delay_handler(ndlp);
181 free_evt = 0;
182 break;
183 case LPFC_EVT_ONLINE:
2e0fef85
JS
184 if (phba->link_state < LPFC_LINK_DOWN)
185 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
41415862 186 else
2e0fef85 187 *(int *) (evtp->evt_arg1) = 0;
dea3101e 188 complete((struct completion *)(evtp->evt_arg2));
189 break;
46fa311e 190 case LPFC_EVT_OFFLINE_PREP:
2e0fef85 191 if (phba->link_state >= LPFC_LINK_DOWN)
46fa311e
JS
192 lpfc_offline_prep(phba);
193 *(int *)(evtp->evt_arg1) = 0;
194 complete((struct completion *)(evtp->evt_arg2));
195 break;
196 case LPFC_EVT_OFFLINE:
197 lpfc_offline(phba);
41415862
JW
198 lpfc_sli_brdrestart(phba);
199 *(int *)(evtp->evt_arg1) =
46fa311e
JS
200 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
201 lpfc_unblock_mgmt_io(phba);
41415862
JW
202 complete((struct completion *)(evtp->evt_arg2));
203 break;
204 case LPFC_EVT_WARM_START:
46fa311e 205 lpfc_offline(phba);
9290831f 206 lpfc_reset_barrier(phba);
41415862
JW
207 lpfc_sli_brdreset(phba);
208 lpfc_hba_down_post(phba);
209 *(int *)(evtp->evt_arg1) =
210 lpfc_sli_brdready(phba, HS_MBRDY);
46fa311e 211 lpfc_unblock_mgmt_io(phba);
41415862
JW
212 complete((struct completion *)(evtp->evt_arg2));
213 break;
214 case LPFC_EVT_KILL:
46fa311e 215 lpfc_offline(phba);
9290831f 216 *(int *)(evtp->evt_arg1)
2e0fef85
JS
217 = (phba->pport->stopped)
218 ? 0 : lpfc_sli_brdkill(phba);
46fa311e 219 lpfc_unblock_mgmt_io(phba);
dea3101e 220 complete((struct completion *)(evtp->evt_arg2));
221 break;
222 }
223 if (free_evt)
224 kfree(evtp);
2e0fef85 225 spin_lock_irq(&phba->hbalock);
dea3101e 226 }
2e0fef85 227 spin_unlock_irq(&phba->hbalock);
dea3101e 228
229}
230
231static void
2e0fef85 232lpfc_work_done(struct lpfc_hba *phba)
dea3101e 233{
234 struct lpfc_sli_ring *pring;
2e0fef85
JS
235 uint32_t ha_copy, control, work_port_events;
236 struct lpfc_vport *vport;
ed957684 237 int i;
dea3101e 238
2e0fef85 239 spin_lock_irq(&phba->hbalock);
dea3101e 240 ha_copy = phba->work_ha;
241 phba->work_ha = 0;
2e0fef85 242 spin_unlock_irq(&phba->hbalock);
dea3101e 243
2fe165b6 244 if (ha_copy & HA_ERATT)
dea3101e 245 lpfc_handle_eratt(phba);
246
2fe165b6 247 if (ha_copy & HA_MBATT)
dea3101e 248 lpfc_sli_handle_mb_event(phba);
249
2fe165b6 250 if (ha_copy & HA_LATT)
dea3101e 251 lpfc_handle_latt(phba);
252
2e0fef85 253 vport = phba->pport;
dea3101e 254
2e0fef85 255 work_port_events = vport->work_port_events;
dea3101e 256
2e0fef85
JS
257 if (work_port_events & WORKER_DISC_TMO)
258 lpfc_disc_timeout_handler(vport);
259
260 if (work_port_events & WORKER_ELS_TMO)
261 lpfc_els_timeout_handler(vport);
262
263 if (work_port_events & WORKER_MBOX_TMO)
dea3101e 264 lpfc_mbox_timeout_handler(phba);
265
2e0fef85
JS
266 if (work_port_events & WORKER_FDMI_TMO)
267 lpfc_fdmi_timeout_handler(vport);
dea3101e 268
2e0fef85
JS
269 spin_lock_irq(&phba->hbalock);
270 vport->work_port_events &= ~work_port_events;
271 spin_unlock_irq(&phba->hbalock);
dea3101e 272
273 for (i = 0; i < phba->sli.num_rings; i++, ha_copy >>= 4) {
274 pring = &phba->sli.ring[i];
275 if ((ha_copy & HA_RXATT)
276 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
277 if (pring->flag & LPFC_STOP_IOCB_MASK) {
278 pring->flag |= LPFC_DEFERRED_RING_EVENT;
279 } else {
280 lpfc_sli_handle_slow_ring_event(phba, pring,
281 (ha_copy &
282 HA_RXMASK));
283 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
284 }
285 /*
286 * Turn on Ring interrupts
287 */
2e0fef85 288 spin_lock_irq(&phba->hbalock);
dea3101e 289 control = readl(phba->HCregaddr);
290 control |= (HC_R0INT_ENA << i);
291 writel(control, phba->HCregaddr);
292 readl(phba->HCregaddr); /* flush */
2e0fef85 293 spin_unlock_irq(&phba->hbalock);
dea3101e 294 }
295 }
296
2e0fef85 297 lpfc_work_list_done(phba);
dea3101e 298}
299
300static int
2e0fef85
JS
301check_work_wait_done(struct lpfc_hba *phba)
302{
303 struct lpfc_vport *vport = phba->pport;
304 int rc = 0;
305
2e0fef85
JS
306 if (!vport)
307 return 0;
ed957684 308
2e0fef85 309 spin_lock_irq(&phba->hbalock);
dea3101e 310
dea3101e 311 if (phba->work_ha ||
2e0fef85 312 vport->work_port_events ||
dea3101e 313 (!list_empty(&phba->work_list)) ||
2e0fef85
JS
314 kthread_should_stop())
315 rc = 1;
316
317 spin_unlock_irq(&phba->hbalock);
318 return rc;
dea3101e 319}
320
321int
322lpfc_do_work(void *p)
323{
324 struct lpfc_hba *phba = p;
325 int rc;
7259f0d0 326 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
dea3101e 327
328 set_user_nice(current, -20);
329 phba->work_wait = &work_waitq;
330
331 while (1) {
332
333 rc = wait_event_interruptible(work_waitq,
334 check_work_wait_done(phba));
335 BUG_ON(rc);
336
337 if (kthread_should_stop())
338 break;
339
340 lpfc_work_done(phba);
341
342 }
343 phba->work_wait = NULL;
344 return 0;
345}
346
347/*
348 * This is only called to handle FC worker events. Since this a rare
349 * occurance, we allocate a struct lpfc_work_evt structure here instead of
350 * embedding it in the IOCB.
351 */
352int
2e0fef85 353lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea3101e 354 uint32_t evt)
355{
356 struct lpfc_work_evt *evtp;
ed957684 357 unsigned long flags;
dea3101e 358
359 /*
360 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
361 * be queued to worker thread for processing
362 */
363 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_KERNEL);
364 if (!evtp)
365 return 0;
366
367 evtp->evt_arg1 = arg1;
368 evtp->evt_arg2 = arg2;
369 evtp->evt = evt;
370
ed957684 371 spin_lock_irqsave(&phba->hbalock, flags);
071fbd3d 372 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea3101e 373 if (phba->work_wait)
374 wake_up(phba->work_wait);
ed957684 375 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 376
377 return 1;
378}
379
380int
685f0bf7 381lpfc_linkdown(struct lpfc_hba *phba)
dea3101e 382{
2e0fef85
JS
383 struct lpfc_vport *vport = phba->pport;
384 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
385 struct lpfc_sli *psli;
dea3101e 386 struct lpfc_nodelist *ndlp, *next_ndlp;
685f0bf7
JS
387 LPFC_MBOXQ_t *mb;
388 int rc;
dea3101e 389
390 psli = &phba->sli;
2e0fef85
JS
391 if (phba->link_state == LPFC_LINK_DOWN) {
392 return 0;
c9f8735b 393 }
2e0fef85
JS
394 spin_lock_irq(&phba->hbalock);
395 if (phba->link_state > LPFC_LINK_DOWN)
396 phba->link_state = LPFC_LINK_DOWN;
397 spin_unlock_irq(&phba->hbalock);
dea3101e 398
2e0fef85 399 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
d2873e4c 400
dea3101e 401 /* Clean up any firmware default rpi's */
2e0fef85
JS
402 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
403 if (mb) {
dea3101e 404 lpfc_unreg_did(phba, 0xffffffff, mb);
ed957684 405 mb->vport = vport;
2e0fef85 406 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
dea3101e 407 if (lpfc_sli_issue_mbox(phba, mb, (MBX_NOWAIT | MBX_STOP_IOCB))
408 == MBX_NOT_FINISHED) {
2e0fef85 409 mempool_free(mb, phba->mbox_mem_pool);
dea3101e 410 }
411 }
412
413 /* Cleanup any outstanding RSCN activity */
2e0fef85 414 lpfc_els_flush_rscn(vport);
dea3101e 415
416 /* Cleanup any outstanding ELS commands */
2e0fef85 417 lpfc_els_flush_cmd(vport);
dea3101e 418
685f0bf7
JS
419 /*
420 * Issue a LINK DOWN event to all nodes.
421 */
2e0fef85
JS
422 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
423 /* free any ndlp's on unused state */
685f0bf7 424 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2e0fef85 425 lpfc_drop_node(vport, ndlp);
685f0bf7 426 else /* otherwise, force node recovery. */
2e0fef85 427 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
685f0bf7 428 NLP_EVT_DEVICE_RECOVERY);
dea3101e 429 }
430
dea3101e 431 /* Setup myDID for link up if we are in pt2pt mode */
2e0fef85
JS
432 if (vport->fc_flag & FC_PT2PT) {
433 vport->fc_myDID = 0;
434 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
435 if (mb) {
dea3101e 436 lpfc_config_link(phba, mb);
437 mb->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
ed957684 438 mb->vport = vport;
2e0fef85
JS
439 if (lpfc_sli_issue_mbox(phba, mb,
440 (MBX_NOWAIT | MBX_STOP_IOCB))
dea3101e 441 == MBX_NOT_FINISHED) {
2e0fef85 442 mempool_free(mb, phba->mbox_mem_pool);
dea3101e 443 }
444 }
2e0fef85
JS
445 spin_lock_irq(shost->host_lock);
446 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
447 spin_unlock_irq(shost->host_lock);
dea3101e 448 }
2e0fef85
JS
449
450 spin_lock_irq(shost->host_lock);
451 vport->fc_flag &= ~FC_LBIT;
452 spin_unlock_irq(shost->host_lock);
dea3101e 453
454 /* Turn off discovery timer if its running */
2e0fef85 455 lpfc_can_disctmo(vport);
dea3101e 456
457 /* Must process IOCBs on all rings to handle ABORTed I/Os */
2fe165b6 458 return 0;
dea3101e 459}
460
461static int
685f0bf7 462lpfc_linkup(struct lpfc_hba *phba)
dea3101e 463{
2e0fef85
JS
464 struct lpfc_vport *vport = phba->pport;
465 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 466 struct lpfc_nodelist *ndlp, *next_ndlp;
467
2e0fef85 468 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
d2873e4c 469
2e0fef85
JS
470 spin_lock_irq(shost->host_lock);
471 phba->link_state = LPFC_LINK_UP;
472 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
473 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
474 vport->fc_flag |= FC_NDISC_ACTIVE;
475 vport->fc_ns_retry = 0;
476 spin_unlock_irq(shost->host_lock);
dea3101e 477
478
2e0fef85
JS
479 if (vport->fc_flag & FC_LBIT) {
480 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
685f0bf7 481 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE) {
c9f8735b 482 if (ndlp->nlp_type & NLP_FABRIC) {
685f0bf7
JS
483 /*
484 * On Linkup its safe to clean up the
c9f8735b
JW
485 * ndlp from Fabric connections.
486 */
2e0fef85 487 lpfc_nlp_set_state(vport, ndlp,
685f0bf7 488 NLP_STE_UNUSED_NODE);
c9f8735b 489 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
685f0bf7
JS
490 /*
491 * Fail outstanding IO now since
492 * device is marked for PLOGI.
c9f8735b 493 */
2e0fef85 494 lpfc_unreg_rpi(vport, ndlp);
c9f8735b
JW
495 }
496 }
dea3101e 497 }
498 }
499
2e0fef85
JS
500 /* free any ndlp's in unused state */
501 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
685f0bf7
JS
502 nlp_listp) {
503 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2e0fef85 504 lpfc_drop_node(vport, ndlp);
685f0bf7 505 }
dea3101e 506
507 return 0;
508}
509
510/*
511 * This routine handles processing a CLEAR_LA mailbox
512 * command upon completion. It is setup in the LPFC_MBOXQ
513 * as the completion routine when the command is
514 * handed off to the SLI layer.
515 */
516void
2e0fef85 517lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 518{
2e0fef85
JS
519 struct lpfc_vport *vport = pmb->vport;
520 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
521 struct lpfc_sli *psli = &phba->sli;
522 MAILBOX_t *mb = &pmb->mb;
dea3101e 523 uint32_t control;
524
dea3101e 525 /* Since we don't do discovery right now, turn these off here */
a4bc3379 526 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 527 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
528 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
529
530 /* Check for error */
531 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
2e0fef85 532 /* CLEAR_LA mbox error <mbxStatus> state <port_state> */
dea3101e 533 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
534 "%d:0320 CLEAR_LA mbxStatus error x%x hba "
535 "state x%x\n",
2e0fef85 536 phba->brd_no, mb->mbxStatus, vport->port_state);
dea3101e 537
2e0fef85 538 phba->link_state = LPFC_HBA_ERROR;
dea3101e 539 goto out;
540 }
541
2e0fef85 542 if (vport->fc_flag & FC_ABORT_DISCOVERY)
dea3101e 543 goto out;
544
2e0fef85
JS
545 vport->num_disc_nodes = 0;
546 /* go thru NPR nodes and issue ELS PLOGIs */
547 if (vport->fc_npr_cnt)
548 lpfc_els_disc_plogi(vport);
dea3101e 549
2e0fef85
JS
550 if (!vport->num_disc_nodes) {
551 spin_lock_irq(shost->host_lock);
552 vport->fc_flag &= ~FC_NDISC_ACTIVE;
553 spin_unlock_irq(shost->host_lock);
dea3101e 554 }
555
2e0fef85 556 vport->port_state = LPFC_VPORT_READY;
dea3101e 557
558out:
559 /* Device Discovery completes */
ed957684 560 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
dea3101e 561 "%d:0225 Device Discovery completes\n",
562 phba->brd_no);
563
2e0fef85 564 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 565
2e0fef85
JS
566 spin_lock_irq(shost->host_lock);
567 vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
568 spin_unlock_irq(shost->host_lock);
dea3101e 569
570 del_timer_sync(&phba->fc_estabtmo);
571
2e0fef85 572 lpfc_can_disctmo(vport);
dea3101e 573
574 /* turn on Link Attention interrupts */
2e0fef85
JS
575
576 spin_lock_irq(&phba->hbalock);
dea3101e 577 psli->sli_flag |= LPFC_PROCESS_LA;
578 control = readl(phba->HCregaddr);
579 control |= HC_LAINT_ENA;
580 writel(control, phba->HCregaddr);
581 readl(phba->HCregaddr); /* flush */
2e0fef85 582 spin_unlock_irq(&phba->hbalock);
dea3101e 583
584 return;
585}
586
2e0fef85 587
dea3101e 588static void
25594c6b 589lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 590{
2e0fef85 591 struct lpfc_vport *vport = pmb->vport;
25594c6b
JW
592 struct lpfc_sli *psli = &phba->sli;
593 int rc;
dea3101e 594
25594c6b 595 if (pmb->mb.mbxStatus)
dea3101e 596 goto out;
dea3101e 597
25594c6b
JW
598 mempool_free(pmb, phba->mbox_mem_pool);
599
600 if (phba->fc_topology == TOPOLOGY_LOOP &&
2e0fef85
JS
601 vport->fc_flag & FC_PUBLIC_LOOP &&
602 !(vport->fc_flag & FC_LBIT)) {
25594c6b 603 /* Need to wait for FAN - use discovery timer
2e0fef85 604 * for timeout. port_state is identically
25594c6b
JW
605 * LPFC_LOCAL_CFG_LINK while waiting for FAN
606 */
2e0fef85 607 lpfc_set_disctmo(vport);
25594c6b 608 return;
dea3101e 609 }
610
2e0fef85 611 /* Start discovery by sending a FLOGI. port_state is identically
25594c6b
JW
612 * LPFC_FLOGI while waiting for FLOGI cmpl
613 */
2e0fef85
JS
614 vport->port_state = LPFC_FLOGI;
615 lpfc_set_disctmo(vport);
616 lpfc_initial_flogi(vport);
25594c6b 617 return;
dea3101e 618
619out:
25594c6b
JW
620 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
621 "%d:0306 CONFIG_LINK mbxStatus error x%x "
622 "HBA state x%x\n",
2e0fef85 623 phba->brd_no, pmb->mb.mbxStatus, vport->port_state);
25594c6b
JW
624
625 lpfc_linkdown(phba);
626
2e0fef85 627 phba->link_state = LPFC_HBA_ERROR;
25594c6b
JW
628
629 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
dea3101e 630 "%d:0200 CONFIG_LINK bad hba state x%x\n",
2e0fef85 631 phba->brd_no, vport->port_state);
dea3101e 632
25594c6b
JW
633 lpfc_clear_la(phba, pmb);
634 pmb->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2e0fef85 635 pmb->vport = vport;
25594c6b
JW
636 rc = lpfc_sli_issue_mbox(phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
637 if (rc == MBX_NOT_FINISHED) {
638 mempool_free(pmb, phba->mbox_mem_pool);
2e0fef85 639 lpfc_disc_flush_list(vport);
a4bc3379 640 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
25594c6b
JW
641 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
642 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2e0fef85 643 vport->port_state = LPFC_VPORT_READY;
dea3101e 644 }
645 return;
646}
647
648static void
2e0fef85 649lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 650{
651 struct lpfc_sli *psli = &phba->sli;
652 MAILBOX_t *mb = &pmb->mb;
653 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
2e0fef85 654 struct lpfc_vport *vport = pmb->vport;
dea3101e 655
656
657 /* Check for error */
658 if (mb->mbxStatus) {
659 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
660 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
661 "%d:0319 READ_SPARAM mbxStatus error x%x "
662 "hba state x%x>\n",
2e0fef85 663 phba->brd_no, mb->mbxStatus, vport->port_state);
dea3101e 664
665 lpfc_linkdown(phba);
2e0fef85 666 phba->link_state = LPFC_HBA_ERROR;
dea3101e 667 goto out;
668 }
669
2e0fef85 670 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea3101e 671 sizeof (struct serv_parm));
a12e07bc 672 if (phba->cfg_soft_wwnn)
2e0fef85
JS
673 u64_to_wwn(phba->cfg_soft_wwnn,
674 vport->fc_sparam.nodeName.u.wwn);
c3f28afa 675 if (phba->cfg_soft_wwpn)
2e0fef85
JS
676 u64_to_wwn(phba->cfg_soft_wwpn,
677 vport->fc_sparam.portName.u.wwn);
678 memcpy((uint8_t *) &vport->fc_nodename,
679 (uint8_t *) &vport->fc_sparam.nodeName,
dea3101e 680 sizeof (struct lpfc_name));
2e0fef85
JS
681 memcpy((uint8_t *) &vport->fc_portname,
682 (uint8_t *) &vport->fc_sparam.portName,
dea3101e 683 sizeof (struct lpfc_name));
684 lpfc_mbuf_free(phba, mp->virt, mp->phys);
685 kfree(mp);
2e0fef85 686 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 687 return;
688
689out:
690 pmb->context1 = NULL;
691 lpfc_mbuf_free(phba, mp->virt, mp->phys);
692 kfree(mp);
2e0fef85
JS
693 if (phba->link_state != LPFC_CLEAR_LA) {
694 struct lpfc_sli_ring *extra_ring =
695 &psli->ring[psli->extra_ring];
696 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
697 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
698
dea3101e 699 lpfc_clear_la(phba, pmb);
700 pmb->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2e0fef85 701 pmb->vport = vport;
dea3101e 702 if (lpfc_sli_issue_mbox(phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB))
703 == MBX_NOT_FINISHED) {
2e0fef85
JS
704 mempool_free(pmb, phba->mbox_mem_pool);
705 lpfc_disc_flush_list(vport);
706 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
707 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
708 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2e0fef85 709 vport->port_state = LPFC_VPORT_READY;
dea3101e 710 }
711 } else {
2e0fef85 712 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 713 }
714 return;
715}
716
717static void
2e0fef85 718lpfc_mbx_process_link_up(struct lpfc_vport *vport, READ_LA_VAR *la)
dea3101e 719{
2e0fef85
JS
720 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
721 struct lpfc_hba *phba = vport->phba;
dea3101e 722 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
2e0fef85 723 int i;
14691150
JS
724 struct lpfc_dmabuf *mp;
725 int rc;
726
dea3101e 727 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
728 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
729
2e0fef85 730 spin_lock_irq(shost->host_lock);
2fe165b6 731 switch (la->UlnkSpeed) {
dea3101e 732 case LA_1GHZ_LINK:
733 phba->fc_linkspeed = LA_1GHZ_LINK;
734 break;
735 case LA_2GHZ_LINK:
736 phba->fc_linkspeed = LA_2GHZ_LINK;
737 break;
738 case LA_4GHZ_LINK:
739 phba->fc_linkspeed = LA_4GHZ_LINK;
740 break;
b87eab38
JS
741 case LA_8GHZ_LINK:
742 phba->fc_linkspeed = LA_8GHZ_LINK;
743 break;
dea3101e 744 default:
745 phba->fc_linkspeed = LA_UNKNW_LINK;
746 break;
747 }
748
749 phba->fc_topology = la->topology;
750
751 if (phba->fc_topology == TOPOLOGY_LOOP) {
752 /* Get Loop Map information */
753
754 if (la->il)
2e0fef85 755 vport->fc_flag |= FC_LBIT;
dea3101e 756
2e0fef85 757 vport->fc_myDID = la->granted_AL_PA;
dea3101e 758 i = la->un.lilpBde64.tus.f.bdeSize;
759
760 if (i == 0) {
761 phba->alpa_map[0] = 0;
762 } else {
763 if (phba->cfg_log_verbose & LOG_LINK_EVENT) {
764 int numalpa, j, k;
765 union {
766 uint8_t pamap[16];
767 struct {
768 uint32_t wd1;
769 uint32_t wd2;
770 uint32_t wd3;
771 uint32_t wd4;
772 } pa;
773 } un;
774 numalpa = phba->alpa_map[0];
775 j = 0;
776 while (j < numalpa) {
777 memset(un.pamap, 0, 16);
778 for (k = 1; j < numalpa; k++) {
779 un.pamap[k - 1] =
780 phba->alpa_map[j + 1];
781 j++;
782 if (k == 16)
783 break;
784 }
785 /* Link Up Event ALPA map */
786 lpfc_printf_log(phba,
787 KERN_WARNING,
788 LOG_LINK_EVENT,
789 "%d:1304 Link Up Event "
790 "ALPA map Data: x%x "
791 "x%x x%x x%x\n",
792 phba->brd_no,
793 un.pa.wd1, un.pa.wd2,
794 un.pa.wd3, un.pa.wd4);
795 }
796 }
797 }
798 } else {
2e0fef85
JS
799 vport->fc_myDID = phba->fc_pref_DID;
800 vport->fc_flag |= FC_LBIT;
dea3101e 801 }
2e0fef85 802 spin_unlock_irq(shost->host_lock);
dea3101e 803
804 lpfc_linkup(phba);
805 if (sparam_mbox) {
806 lpfc_read_sparam(phba, sparam_mbox);
2e0fef85 807 sparam_mbox->vport = vport;
dea3101e 808 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
14691150 809 rc = lpfc_sli_issue_mbox(phba, sparam_mbox,
dea3101e 810 (MBX_NOWAIT | MBX_STOP_IOCB));
14691150
JS
811 if (rc == MBX_NOT_FINISHED) {
812 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
813 lpfc_mbuf_free(phba, mp->virt, mp->phys);
814 kfree(mp);
815 mempool_free(sparam_mbox, phba->mbox_mem_pool);
816 if (cfglink_mbox)
817 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
818 return;
819 }
dea3101e 820 }
821
822 if (cfglink_mbox) {
2e0fef85 823 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea3101e 824 lpfc_config_link(phba, cfglink_mbox);
2e0fef85 825 cfglink_mbox->vport = vport;
25594c6b 826 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
14691150 827 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox,
dea3101e 828 (MBX_NOWAIT | MBX_STOP_IOCB));
14691150
JS
829 if (rc == MBX_NOT_FINISHED)
830 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea3101e 831 }
832}
833
834static void
2e0fef85
JS
835lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
836{
dea3101e 837 uint32_t control;
838 struct lpfc_sli *psli = &phba->sli;
839
840 lpfc_linkdown(phba);
841
842 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2e0fef85 843 spin_lock_irq(&phba->hbalock);
dea3101e 844 psli->sli_flag |= LPFC_PROCESS_LA;
845 control = readl(phba->HCregaddr);
846 control |= HC_LAINT_ENA;
847 writel(control, phba->HCregaddr);
848 readl(phba->HCregaddr); /* flush */
2e0fef85 849 spin_unlock_irq(&phba->hbalock);
dea3101e 850}
851
852/*
853 * This routine handles processing a READ_LA mailbox
854 * command upon completion. It is setup in the LPFC_MBOXQ
855 * as the completion routine when the command is
856 * handed off to the SLI layer.
857 */
858void
2e0fef85 859lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 860{
2e0fef85
JS
861 struct lpfc_vport *vport = pmb->vport;
862 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 863 READ_LA_VAR *la;
864 MAILBOX_t *mb = &pmb->mb;
865 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
866
867 /* Check for error */
868 if (mb->mbxStatus) {
ed957684 869 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
dea3101e 870 "%d:1307 READ_LA mbox error x%x state x%x\n",
ed957684 871 phba->brd_no, mb->mbxStatus, vport->port_state);
dea3101e 872 lpfc_mbx_issue_link_down(phba);
2e0fef85 873 phba->link_state = LPFC_HBA_ERROR;
dea3101e 874 goto lpfc_mbx_cmpl_read_la_free_mbuf;
875 }
876
877 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
878
879 memcpy(&phba->alpa_map[0], mp->virt, 128);
880
2e0fef85 881 spin_lock_irq(shost->host_lock);
c9f8735b 882 if (la->pb)
2e0fef85 883 vport->fc_flag |= FC_BYPASSED_MODE;
c9f8735b 884 else
2e0fef85
JS
885 vport->fc_flag &= ~FC_BYPASSED_MODE;
886 spin_unlock_irq(shost->host_lock);
c9f8735b 887
dea3101e 888 if (((phba->fc_eventTag + 1) < la->eventTag) ||
889 (phba->fc_eventTag == la->eventTag)) {
890 phba->fc_stat.LinkMultiEvent++;
2e0fef85 891 if (la->attType == AT_LINK_UP)
dea3101e 892 if (phba->fc_eventTag != 0)
893 lpfc_linkdown(phba);
894 }
dea3101e 895
896 phba->fc_eventTag = la->eventTag;
897
898 if (la->attType == AT_LINK_UP) {
899 phba->fc_stat.LinkUp++;
2e0fef85 900 if (phba->link_flag & LS_LOOPBACK_MODE) {
5b8bd0c9
JS
901 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
902 "%d:1306 Link Up Event in loop back mode "
903 "x%x received Data: x%x x%x x%x x%x\n",
904 phba->brd_no, la->eventTag, phba->fc_eventTag,
905 la->granted_AL_PA, la->UlnkSpeed,
906 phba->alpa_map[0]);
907 } else {
908 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
dea3101e 909 "%d:1303 Link Up Event x%x received "
910 "Data: x%x x%x x%x x%x\n",
911 phba->brd_no, la->eventTag, phba->fc_eventTag,
912 la->granted_AL_PA, la->UlnkSpeed,
913 phba->alpa_map[0]);
5b8bd0c9 914 }
2e0fef85 915 lpfc_mbx_process_link_up(vport, la);
dea3101e 916 } else {
917 phba->fc_stat.LinkDown++;
918 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
919 "%d:1305 Link Down Event x%x received "
920 "Data: x%x x%x x%x\n",
921 phba->brd_no, la->eventTag, phba->fc_eventTag,
2e0fef85 922 phba->pport->port_state, vport->fc_flag);
dea3101e 923 lpfc_mbx_issue_link_down(phba);
924 }
925
926lpfc_mbx_cmpl_read_la_free_mbuf:
927 lpfc_mbuf_free(phba, mp->virt, mp->phys);
928 kfree(mp);
929 mempool_free(pmb, phba->mbox_mem_pool);
930 return;
931}
932
933/*
934 * This routine handles processing a REG_LOGIN mailbox
935 * command upon completion. It is setup in the LPFC_MBOXQ
936 * as the completion routine when the command is
937 * handed off to the SLI layer.
938 */
939void
2e0fef85 940lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 941{
2e0fef85
JS
942 struct lpfc_vport *vport = pmb->vport;
943 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
944 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea3101e 945
dea3101e 946 pmb->context1 = NULL;
947
948 /* Good status, call state machine */
2e0fef85 949 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea3101e 950 lpfc_mbuf_free(phba, mp->virt, mp->phys);
951 kfree(mp);
2e0fef85 952 mempool_free(pmb, phba->mbox_mem_pool);
329f9bc7 953 lpfc_nlp_put(ndlp);
dea3101e 954
955 return;
956}
957
958/*
959 * This routine handles processing a Fabric REG_LOGIN mailbox
960 * command upon completion. It is setup in the LPFC_MBOXQ
961 * as the completion routine when the command is
962 * handed off to the SLI layer.
963 */
964void
2e0fef85 965lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 966{
2e0fef85
JS
967 struct lpfc_vport *vport = pmb->vport;
968 MAILBOX_t *mb = &pmb->mb;
969 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
970 struct lpfc_nodelist *ndlp, *ndlp_fdmi;
dea3101e 971 ndlp = (struct lpfc_nodelist *) pmb->context2;
dea3101e 972
329f9bc7
JS
973 pmb->context1 = NULL;
974 pmb->context2 = NULL;
975
dea3101e 976 if (mb->mbxStatus) {
977 lpfc_mbuf_free(phba, mp->virt, mp->phys);
978 kfree(mp);
329f9bc7
JS
979 mempool_free(pmb, phba->mbox_mem_pool);
980 lpfc_nlp_put(ndlp);
dea3101e 981
982 /* FLOGI failed, so just use loop map to make discovery list */
2e0fef85 983 lpfc_disc_list_loopmap(vport);
dea3101e 984
985 /* Start discovery */
2e0fef85 986 lpfc_disc_start(vport);
dea3101e 987 return;
988 }
989
dea3101e 990 ndlp->nlp_rpi = mb->un.varWords[0];
dea3101e 991 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 992 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 993
329f9bc7
JS
994 lpfc_nlp_put(ndlp); /* Drop the reference from the mbox */
995
2e0fef85 996 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
dea3101e 997 /* This NPort has been assigned an NPort_ID by the fabric as a
998 * result of the completed fabric login. Issue a State Change
999 * Registration (SCR) ELS request to the fabric controller
1000 * (SCR_DID) so that this NPort gets RSCN events from the
1001 * fabric.
1002 */
2e0fef85 1003 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea3101e 1004
2e0fef85 1005 ndlp = lpfc_findnode_did(vport, NameServer_DID);
c9f8735b 1006 if (!ndlp) {
2e0fef85 1007 /* Allocate a new node instance. If the pool is empty,
c9f8735b
JW
1008 * start the discovery process and skip the Nameserver
1009 * login process. This is attempted again later on.
2e0fef85
JS
1010 * Otherwise, issue a Port Login (PLOGI) to
1011 * the NameServer
c9f8735b 1012 */
2e0fef85 1013 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
c9f8735b 1014 if (!ndlp) {
2e0fef85 1015 lpfc_disc_start(vport);
c9f8735b
JW
1016 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1017 kfree(mp);
329f9bc7 1018 mempool_free(pmb, phba->mbox_mem_pool);
c9f8735b
JW
1019 return;
1020 } else {
2e0fef85 1021 lpfc_nlp_init(vport, ndlp, NameServer_DID);
c9f8735b
JW
1022 ndlp->nlp_type |= NLP_FABRIC;
1023 }
1024 }
2e0fef85
JS
1025
1026 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1027 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
c9f8735b
JW
1028 if (phba->cfg_fdmi_on) {
1029 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
2e0fef85 1030 GFP_KERNEL);
c9f8735b 1031 if (ndlp_fdmi) {
2e0fef85 1032 lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
c9f8735b
JW
1033 ndlp_fdmi->nlp_type |= NLP_FABRIC;
1034 ndlp_fdmi->nlp_state = NLP_STE_PLOGI_ISSUE;
2e0fef85 1035 lpfc_issue_els_plogi(vport, FDMI_DID, 0);
dea3101e 1036 }
1037 }
1038 }
1039
1040 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1041 kfree(mp);
329f9bc7 1042 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1043 return;
1044}
1045
1046/*
1047 * This routine handles processing a NameServer REG_LOGIN mailbox
1048 * command upon completion. It is setup in the LPFC_MBOXQ
1049 * as the completion routine when the command is
1050 * handed off to the SLI layer.
1051 */
1052void
2e0fef85 1053lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1054{
2e0fef85
JS
1055 MAILBOX_t *mb = &pmb->mb;
1056 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1057 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1058 struct lpfc_vport *vport = pmb->vport;
dea3101e 1059
1060 if (mb->mbxStatus) {
329f9bc7 1061 lpfc_nlp_put(ndlp);
dea3101e 1062 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1063 kfree(mp);
de0c5b32 1064 mempool_free(pmb, phba->mbox_mem_pool);
2e0fef85 1065 lpfc_drop_node(vport, ndlp);
dea3101e 1066
2e0fef85
JS
1067 /*
1068 * RegLogin failed, so just use loop map to make discovery
1069 * list
1070 */
1071 lpfc_disc_list_loopmap(vport);
dea3101e 1072
1073 /* Start discovery */
2e0fef85 1074 lpfc_disc_start(vport);
dea3101e 1075 return;
1076 }
1077
1078 pmb->context1 = NULL;
1079
dea3101e 1080 ndlp->nlp_rpi = mb->un.varWords[0];
dea3101e 1081 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 1082 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 1083
2e0fef85
JS
1084 if (vport->port_state < LPFC_VPORT_READY) {
1085 /* Link up discovery requires Fabric registration. */
1086 lpfc_ns_cmd(vport, ndlp, SLI_CTNS_RNN_ID);
1087 lpfc_ns_cmd(vport, ndlp, SLI_CTNS_RSNN_NN);
1088 lpfc_ns_cmd(vport, ndlp, SLI_CTNS_RFT_ID);
1089 lpfc_ns_cmd(vport, ndlp, SLI_CTNS_RFF_ID);
dea3101e 1090 }
1091
2e0fef85 1092 vport->fc_ns_retry = 0;
dea3101e 1093 /* Good status, issue CT Request to NameServer */
2e0fef85 1094 if (lpfc_ns_cmd(vport, ndlp, SLI_CTNS_GID_FT)) {
dea3101e 1095 /* Cannot issue NameServer Query, so finish up discovery */
2e0fef85 1096 lpfc_disc_start(vport);
dea3101e 1097 }
1098
329f9bc7 1099 lpfc_nlp_put(ndlp);
dea3101e 1100 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1101 kfree(mp);
2e0fef85 1102 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1103
1104 return;
1105}
1106
1107static void
2e0fef85 1108lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 1109{
2e0fef85
JS
1110 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1111 struct fc_rport *rport;
dea3101e 1112 struct lpfc_rport_data *rdata;
1113 struct fc_rport_identifiers rport_ids;
2e0fef85 1114 struct lpfc_hba *phba = vport->phba;
dea3101e 1115
1116 /* Remote port has reappeared. Re-register w/ FC transport */
68ce1eb5
AM
1117 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1118 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea3101e 1119 rport_ids.port_id = ndlp->nlp_DID;
1120 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea3101e 1121
329f9bc7
JS
1122 /*
1123 * We leave our node pointer in rport->dd_data when we unregister a
1124 * FCP target port. But fc_remote_port_add zeros the space to which
1125 * rport->dd_data points. So, if we're reusing a previously
1126 * registered port, drop the reference that we took the last time we
1127 * registered the port.
1128 */
1129 if (ndlp->rport && ndlp->rport->dd_data &&
1130 *(struct lpfc_rport_data **) ndlp->rport->dd_data) {
1131 lpfc_nlp_put(ndlp);
1132 }
2e0fef85 1133 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
329f9bc7 1134 if (!rport || !get_device(&rport->dev)) {
dea3101e 1135 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1136 "Warning: fc_remote_port_add failed\n");
1137 return;
1138 }
1139
1140 /* initialize static port data */
1141 rport->maxframe_size = ndlp->nlp_maxframe;
1142 rport->supported_classes = ndlp->nlp_class_sup;
dea3101e 1143 rdata = rport->dd_data;
329f9bc7 1144 rdata->pnode = lpfc_nlp_get(ndlp);
23dc04f1
JSEC
1145
1146 if (ndlp->nlp_type & NLP_FCP_TARGET)
1147 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1148 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1149 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1150
1151
1152 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1153 fc_remote_port_rolechg(rport, rport_ids.roles);
1154
071fbd3d 1155 if ((rport->scsi_target_id != -1) &&
e17da18e 1156 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
071fbd3d
JS
1157 ndlp->nlp_sid = rport->scsi_target_id;
1158 }
19a7b4ae
JSEC
1159
1160 return;
1161}
1162
1163static void
2e0fef85 1164lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
19a7b4ae
JSEC
1165{
1166 struct fc_rport *rport = ndlp->rport;
1167 struct lpfc_rport_data *rdata = rport->dd_data;
1168
c01f3208
JS
1169 if (rport->scsi_target_id == -1) {
1170 ndlp->rport = NULL;
1171 rdata->pnode = NULL;
329f9bc7
JS
1172 lpfc_nlp_put(ndlp);
1173 put_device(&rport->dev);
c01f3208
JS
1174 }
1175
19a7b4ae 1176 fc_remote_port_delete(rport);
dea3101e 1177
1178 return;
1179}
1180
de0c5b32 1181static void
2e0fef85 1182lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
dea3101e 1183{
2e0fef85
JS
1184 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1185
1186 spin_lock_irq(shost->host_lock);
de0c5b32
JS
1187 switch (state) {
1188 case NLP_STE_UNUSED_NODE:
2e0fef85 1189 vport->fc_unused_cnt += count;
de0c5b32
JS
1190 break;
1191 case NLP_STE_PLOGI_ISSUE:
2e0fef85 1192 vport->fc_plogi_cnt += count;
de0c5b32
JS
1193 break;
1194 case NLP_STE_ADISC_ISSUE:
2e0fef85 1195 vport->fc_adisc_cnt += count;
dea3101e 1196 break;
de0c5b32 1197 case NLP_STE_REG_LOGIN_ISSUE:
2e0fef85 1198 vport->fc_reglogin_cnt += count;
de0c5b32
JS
1199 break;
1200 case NLP_STE_PRLI_ISSUE:
2e0fef85 1201 vport->fc_prli_cnt += count;
de0c5b32
JS
1202 break;
1203 case NLP_STE_UNMAPPED_NODE:
2e0fef85 1204 vport->fc_unmap_cnt += count;
de0c5b32
JS
1205 break;
1206 case NLP_STE_MAPPED_NODE:
2e0fef85 1207 vport->fc_map_cnt += count;
de0c5b32
JS
1208 break;
1209 case NLP_STE_NPR_NODE:
2e0fef85 1210 vport->fc_npr_cnt += count;
de0c5b32
JS
1211 break;
1212 }
2e0fef85 1213 spin_unlock_irq(shost->host_lock);
de0c5b32 1214}
66a9ed66 1215
de0c5b32 1216static void
2e0fef85 1217lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
de0c5b32
JS
1218 int old_state, int new_state)
1219{
2e0fef85
JS
1220 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1221
de0c5b32
JS
1222 if (new_state == NLP_STE_UNMAPPED_NODE) {
1223 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1224 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1225 ndlp->nlp_type |= NLP_FC_NODE;
1226 }
1227 if (new_state == NLP_STE_MAPPED_NODE)
1228 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1229 if (new_state == NLP_STE_NPR_NODE)
1230 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
1231
1232 /* Transport interface */
1233 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1234 old_state == NLP_STE_UNMAPPED_NODE)) {
2e0fef85
JS
1235 vport->phba->nport_event_cnt++;
1236 lpfc_unregister_remote_port(ndlp);
de0c5b32 1237 }
dea3101e 1238
de0c5b32
JS
1239 if (new_state == NLP_STE_MAPPED_NODE ||
1240 new_state == NLP_STE_UNMAPPED_NODE) {
2e0fef85 1241 vport->phba->nport_event_cnt++;
dea3101e 1242 /*
1243 * Tell the fc transport about the port, if we haven't
1244 * already. If we have, and it's a scsi entity, be
1245 * sure to unblock any attached scsi devices
1246 */
2e0fef85 1247 lpfc_register_remote_port(vport, ndlp);
de0c5b32 1248 }
dea3101e 1249
1250 /*
1251 * if we added to Mapped list, but the remote port
1252 * registration failed or assigned a target id outside
1253 * our presentable range - move the node to the
1254 * Unmapped List
1255 */
de0c5b32
JS
1256 if (new_state == NLP_STE_MAPPED_NODE &&
1257 (!ndlp->rport ||
1258 ndlp->rport->scsi_target_id == -1 ||
1259 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
2e0fef85 1260 spin_lock_irq(shost->host_lock);
de0c5b32 1261 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
2e0fef85
JS
1262 spin_unlock_irq(shost->host_lock);
1263 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 1264 }
de0c5b32
JS
1265}
1266
685f0bf7
JS
1267static char *
1268lpfc_nlp_state_name(char *buffer, size_t size, int state)
1269{
1270 static char *states[] = {
1271 [NLP_STE_UNUSED_NODE] = "UNUSED",
1272 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1273 [NLP_STE_ADISC_ISSUE] = "ADISC",
1274 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1275 [NLP_STE_PRLI_ISSUE] = "PRLI",
1276 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1277 [NLP_STE_MAPPED_NODE] = "MAPPED",
1278 [NLP_STE_NPR_NODE] = "NPR",
1279 };
1280
1281 if (state < ARRAY_SIZE(states) && states[state])
1282 strlcpy(buffer, states[state], size);
1283 else
1284 snprintf(buffer, size, "unknown (%d)", state);
1285 return buffer;
1286}
1287
de0c5b32 1288void
2e0fef85
JS
1289lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1290 int state)
de0c5b32 1291{
2e0fef85 1292 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
de0c5b32 1293 int old_state = ndlp->nlp_state;
685f0bf7 1294 char name1[16], name2[16];
de0c5b32 1295
2e0fef85 1296 lpfc_printf_log(vport->phba, KERN_INFO, LOG_NODE,
685f0bf7 1297 "%d:0904 NPort state transition x%06x, %s -> %s\n",
2e0fef85 1298 vport->phba->brd_no,
685f0bf7
JS
1299 ndlp->nlp_DID,
1300 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1301 lpfc_nlp_state_name(name2, sizeof(name2), state));
de0c5b32
JS
1302 if (old_state == NLP_STE_NPR_NODE &&
1303 (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1304 state != NLP_STE_NPR_NODE)
2e0fef85 1305 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32
JS
1306 if (old_state == NLP_STE_UNMAPPED_NODE) {
1307 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1308 ndlp->nlp_type &= ~NLP_FC_NODE;
1309 }
1310
685f0bf7 1311 if (list_empty(&ndlp->nlp_listp)) {
2e0fef85
JS
1312 spin_lock_irq(shost->host_lock);
1313 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1314 spin_unlock_irq(shost->host_lock);
685f0bf7 1315 } else if (old_state)
2e0fef85 1316 lpfc_nlp_counters(vport, old_state, -1);
de0c5b32
JS
1317
1318 ndlp->nlp_state = state;
2e0fef85
JS
1319 lpfc_nlp_counters(vport, state, 1);
1320 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
de0c5b32
JS
1321}
1322
1323void
2e0fef85 1324lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
de0c5b32 1325{
2e0fef85
JS
1326 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1327
de0c5b32 1328 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
2e0fef85 1329 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32 1330 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2e0fef85
JS
1331 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1332 spin_lock_irq(shost->host_lock);
685f0bf7 1333 list_del_init(&ndlp->nlp_listp);
2e0fef85
JS
1334 spin_unlock_irq(shost->host_lock);
1335 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state, 0);
de0c5b32
JS
1336}
1337
1338void
2e0fef85 1339lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
de0c5b32 1340{
2e0fef85
JS
1341 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1342
de0c5b32 1343 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
2e0fef85 1344 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32 1345 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2e0fef85
JS
1346 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1347 spin_lock_irq(shost->host_lock);
685f0bf7 1348 list_del_init(&ndlp->nlp_listp);
2e0fef85 1349 spin_unlock_irq(shost->host_lock);
329f9bc7 1350 lpfc_nlp_put(ndlp);
dea3101e 1351}
1352
1353/*
1354 * Start / ReStart rescue timer for Discovery / RSCN handling
1355 */
1356void
2e0fef85 1357lpfc_set_disctmo(struct lpfc_vport *vport)
dea3101e 1358{
2e0fef85
JS
1359 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1360 struct lpfc_hba *phba = vport->phba;
dea3101e 1361 uint32_t tmo;
1362
2e0fef85 1363 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
c9f8735b
JW
1364 /* For FAN, timeout should be greater then edtov */
1365 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1366 } else {
1367 /* Normal discovery timeout should be > then ELS/CT timeout
1368 * FC spec states we need 3 * ratov for CT requests
1369 */
1370 tmo = ((phba->fc_ratov * 3) + 3);
1371 }
dea3101e 1372
2e0fef85
JS
1373 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1374 spin_lock_irq(shost->host_lock);
1375 vport->fc_flag |= FC_DISC_TMO;
1376 spin_unlock_irq(shost->host_lock);
dea3101e 1377
1378 /* Start Discovery Timer state <hba_state> */
1379 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1380 "%d:0247 Start Discovery Timer state x%x "
1381 "Data: x%x x%lx x%x x%x\n",
2e0fef85
JS
1382 phba->brd_no, vport->port_state, tmo,
1383 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1384 vport->fc_adisc_cnt);
dea3101e 1385
1386 return;
1387}
1388
1389/*
1390 * Cancel rescue timer for Discovery / RSCN handling
1391 */
1392int
2e0fef85 1393lpfc_can_disctmo(struct lpfc_vport *vport)
dea3101e 1394{
2e0fef85
JS
1395 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1396 struct lpfc_hba *phba = vport->phba;
1397 unsigned long iflags;
1398
dea3101e 1399 /* Turn off discovery timer if its running */
2e0fef85
JS
1400 if (vport->fc_flag & FC_DISC_TMO) {
1401 spin_lock_irqsave(shost->host_lock, iflags);
1402 vport->fc_flag &= ~FC_DISC_TMO;
1403 spin_unlock_irqrestore(shost->host_lock, iflags);
1404 del_timer_sync(&vport->fc_disctmo);
1405 spin_lock_irqsave(&vport->work_port_lock, iflags);
1406 vport->work_port_events &= ~WORKER_DISC_TMO;
1407 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea3101e 1408 }
1409
1410 /* Cancel Discovery Timer state <hba_state> */
1411 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1412 "%d:0248 Cancel Discovery Timer state x%x "
1413 "Data: x%x x%x x%x\n",
2e0fef85
JS
1414 phba->brd_no, vport->port_state, vport->fc_flag,
1415 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
dea3101e 1416
2fe165b6 1417 return 0;
dea3101e 1418}
1419
1420/*
1421 * Check specified ring for outstanding IOCB on the SLI queue
1422 * Return true if iocb matches the specified nport
1423 */
1424int
2e0fef85
JS
1425lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1426 struct lpfc_sli_ring *pring,
1427 struct lpfc_iocbq *iocb,
1428 struct lpfc_nodelist *ndlp)
dea3101e 1429{
2e0fef85
JS
1430 struct lpfc_sli *psli = &phba->sli;
1431 IOCB_t *icmd = &iocb->iocb;
dea3101e 1432 if (pring->ringno == LPFC_ELS_RING) {
1433 switch (icmd->ulpCommand) {
1434 case CMD_GEN_REQUEST64_CR:
1435 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
2fe165b6 1436 return 1;
dea3101e 1437 case CMD_ELS_REQUEST64_CR:
10d4e957
JS
1438 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1439 return 1;
dea3101e 1440 case CMD_XMIT_ELS_RSP64_CX:
1441 if (iocb->context1 == (uint8_t *) ndlp)
2fe165b6 1442 return 1;
dea3101e 1443 }
a4bc3379 1444 } else if (pring->ringno == psli->extra_ring) {
dea3101e 1445
1446 } else if (pring->ringno == psli->fcp_ring) {
1447 /* Skip match check if waiting to relogin to FCP target */
1448 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
1449 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
2fe165b6 1450 return 0;
dea3101e 1451 }
1452 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
2fe165b6 1453 return 1;
dea3101e 1454 }
1455 } else if (pring->ringno == psli->next_ring) {
1456
1457 }
2fe165b6 1458 return 0;
dea3101e 1459}
1460
1461/*
1462 * Free resources / clean up outstanding I/Os
1463 * associated with nlp_rpi in the LPFC_NODELIST entry.
1464 */
1465static int
2e0fef85 1466lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 1467{
2534ba75 1468 LIST_HEAD(completions);
dea3101e 1469 struct lpfc_sli *psli;
1470 struct lpfc_sli_ring *pring;
1471 struct lpfc_iocbq *iocb, *next_iocb;
1472 IOCB_t *icmd;
1473 uint32_t rpi, i;
1474
1475 /*
1476 * Everything that matches on txcmplq will be returned
1477 * by firmware with a no rpi error.
1478 */
1479 psli = &phba->sli;
1480 rpi = ndlp->nlp_rpi;
1481 if (rpi) {
1482 /* Now process each ring */
1483 for (i = 0; i < psli->num_rings; i++) {
1484 pring = &psli->ring[i];
1485
2e0fef85 1486 spin_lock_irq(&phba->hbalock);
dea3101e 1487 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
2e0fef85 1488 list) {
dea3101e 1489 /*
1490 * Check to see if iocb matches the nport we are
1491 * looking for
1492 */
1493 if ((lpfc_check_sli_ndlp
1494 (phba, pring, iocb, ndlp))) {
1495 /* It matches, so deque and call compl
1496 with an error */
2534ba75
JS
1497 list_move_tail(&iocb->list,
1498 &completions);
dea3101e 1499 pring->txq_cnt--;
dea3101e 1500 }
1501 }
2e0fef85 1502 spin_unlock_irq(&phba->hbalock);
dea3101e 1503 }
1504 }
2534ba75
JS
1505
1506 while (!list_empty(&completions)) {
1507 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
1508 list_del(&iocb->list);
1509
2e0fef85
JS
1510 if (!iocb->iocb_cmpl)
1511 lpfc_sli_release_iocbq(phba, iocb);
1512 else {
2534ba75
JS
1513 icmd = &iocb->iocb;
1514 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1515 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2e0fef85
JS
1516 (iocb->iocb_cmpl)(phba, iocb, iocb);
1517 }
2534ba75
JS
1518 }
1519
2fe165b6 1520 return 0;
dea3101e 1521}
1522
1523/*
1524 * Free rpi associated with LPFC_NODELIST entry.
1525 * This routine is called from lpfc_freenode(), when we are removing
1526 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1527 * LOGO that completes successfully, and we are waiting to PLOGI back
1528 * to the remote NPort. In addition, it is called after we receive
1529 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1530 * we are waiting to PLOGI back to the remote NPort.
1531 */
1532int
2e0fef85 1533lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 1534{
2e0fef85
JS
1535 struct lpfc_hba *phba = vport->phba;
1536 LPFC_MBOXQ_t *mbox;
dea3101e 1537 int rc;
1538
1539 if (ndlp->nlp_rpi) {
2e0fef85
JS
1540 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1541 if (mbox) {
dea3101e 1542 lpfc_unreg_login(phba, ndlp->nlp_rpi, mbox);
ed957684 1543 mbox->vport = vport;
dea3101e 1544 mbox->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
1545 rc = lpfc_sli_issue_mbox
1546 (phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
1547 if (rc == MBX_NOT_FINISHED)
2e0fef85 1548 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e 1549 }
dea3101e 1550 lpfc_no_rpi(phba, ndlp);
1551 ndlp->nlp_rpi = 0;
1552 return 1;
1553 }
1554 return 0;
1555}
1556
1557/*
1558 * Free resources associated with LPFC_NODELIST entry
1559 * so it can be freed.
1560 */
1561static int
2e0fef85 1562lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 1563{
2e0fef85
JS
1564 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1565 struct lpfc_hba *phba = vport->phba;
1566 LPFC_MBOXQ_t *mb, *nextmb;
dea3101e 1567 struct lpfc_dmabuf *mp;
dea3101e 1568
1569 /* Cleanup node for NPort <nlp_DID> */
1570 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1571 "%d:0900 Cleanup node for NPort x%x "
1572 "Data: x%x x%x x%x\n",
1573 phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
1574 ndlp->nlp_state, ndlp->nlp_rpi);
1575
2e0fef85 1576 lpfc_dequeue_node(vport, ndlp);
dea3101e 1577
dea3101e 1578 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1579 if ((mb = phba->sli.mbox_active)) {
1580 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1581 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1582 mb->context2 = NULL;
1583 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1584 }
1585 }
33ccf8d1 1586
2e0fef85 1587 spin_lock_irq(&phba->hbalock);
dea3101e 1588 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1589 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1590 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1591 mp = (struct lpfc_dmabuf *) (mb->context1);
1592 if (mp) {
2e0fef85 1593 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea3101e 1594 kfree(mp);
1595 }
1596 list_del(&mb->list);
1597 mempool_free(mb, phba->mbox_mem_pool);
329f9bc7 1598 lpfc_nlp_put(ndlp);
dea3101e 1599 }
1600 }
2e0fef85 1601 spin_unlock_irq(&phba->hbalock);
dea3101e 1602
07951076 1603 lpfc_els_abort(phba,ndlp);
2e0fef85 1604 spin_lock_irq(shost->host_lock);
c01f3208 1605 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 1606 spin_unlock_irq(shost->host_lock);
dea3101e 1607
5024ab17 1608 ndlp->nlp_last_elscmd = 0;
dea3101e 1609 del_timer_sync(&ndlp->nlp_delayfunc);
1610
dea3101e 1611 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1612 list_del_init(&ndlp->els_retry_evt.evt_listp);
1613
2e0fef85 1614 lpfc_unreg_rpi(vport, ndlp);
dea3101e 1615
2fe165b6 1616 return 0;
dea3101e 1617}
1618
1619/*
1620 * Check to see if we can free the nlp back to the freelist.
1621 * If we are in the middle of using the nlp in the discovery state
1622 * machine, defer the free till we reach the end of the state machine.
1623 */
329f9bc7 1624static void
2e0fef85 1625lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 1626{
1dcb58e5 1627 struct lpfc_rport_data *rdata;
dea3101e 1628
1629 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
2e0fef85 1630 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea3101e 1631 }
1632
2e0fef85 1633 lpfc_cleanup_node(vport, ndlp);
1dcb58e5 1634
2e0fef85
JS
1635 /*
1636 * We should never get here with a non-NULL ndlp->rport. But
1637 * if we do, drop the reference to the rport. That seems the
1638 * intelligent thing to do.
1639 */
1640 if (ndlp->rport && !(vport->load_flag & FC_UNLOADING)) {
329f9bc7
JS
1641 put_device(&ndlp->rport->dev);
1642 rdata = ndlp->rport->dd_data;
1643 rdata->pnode = NULL;
1644 ndlp->rport = NULL;
dea3101e 1645 }
dea3101e 1646}
1647
1648static int
2e0fef85
JS
1649lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1650 uint32_t did)
dea3101e 1651{
2e0fef85 1652 D_ID mydid, ndlpdid, matchdid;
dea3101e 1653
1654 if (did == Bcast_DID)
2fe165b6 1655 return 0;
dea3101e 1656
1657 if (ndlp->nlp_DID == 0) {
2fe165b6 1658 return 0;
dea3101e 1659 }
1660
1661 /* First check for Direct match */
1662 if (ndlp->nlp_DID == did)
2fe165b6 1663 return 1;
dea3101e 1664
1665 /* Next check for area/domain identically equals 0 match */
2e0fef85 1666 mydid.un.word = vport->fc_myDID;
dea3101e 1667 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
2fe165b6 1668 return 0;
dea3101e 1669 }
1670
1671 matchdid.un.word = did;
1672 ndlpdid.un.word = ndlp->nlp_DID;
1673 if (matchdid.un.b.id == ndlpdid.un.b.id) {
1674 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
1675 (mydid.un.b.area == matchdid.un.b.area)) {
1676 if ((ndlpdid.un.b.domain == 0) &&
1677 (ndlpdid.un.b.area == 0)) {
1678 if (ndlpdid.un.b.id)
2fe165b6 1679 return 1;
dea3101e 1680 }
2fe165b6 1681 return 0;
dea3101e 1682 }
1683
1684 matchdid.un.word = ndlp->nlp_DID;
1685 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
1686 (mydid.un.b.area == ndlpdid.un.b.area)) {
1687 if ((matchdid.un.b.domain == 0) &&
1688 (matchdid.un.b.area == 0)) {
1689 if (matchdid.un.b.id)
2fe165b6 1690 return 1;
dea3101e 1691 }
1692 }
1693 }
2fe165b6 1694 return 0;
dea3101e 1695}
1696
685f0bf7 1697/* Search for a nodelist entry */
2e0fef85
JS
1698static struct lpfc_nodelist *
1699__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea3101e 1700{
2e0fef85 1701 struct lpfc_hba *phba = vport->phba;
2fb9bd8b 1702 struct lpfc_nodelist *ndlp;
dea3101e 1703 uint32_t data1;
1704
2e0fef85
JS
1705 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1706 if (lpfc_matchdid(vport, ndlp, did)) {
685f0bf7
JS
1707 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1708 ((uint32_t) ndlp->nlp_xri << 16) |
1709 ((uint32_t) ndlp->nlp_type << 8) |
1710 ((uint32_t) ndlp->nlp_rpi & 0xff));
1711 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1712 "%d:0929 FIND node DID "
1713 " Data: x%p x%x x%x x%x\n",
1714 phba->brd_no,
1715 ndlp, ndlp->nlp_DID,
1716 ndlp->nlp_flag, data1);
685f0bf7 1717 return ndlp;
dea3101e 1718 }
1719 }
66a9ed66 1720
dea3101e 1721 /* FIND node did <did> NOT FOUND */
2fb9bd8b 1722 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
685f0bf7
JS
1723 "%d:0932 FIND node did x%x NOT FOUND.\n",
1724 phba->brd_no, did);
dea3101e 1725 return NULL;
1726}
1727
1728struct lpfc_nodelist *
2e0fef85
JS
1729lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
1730{
1731 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1732 struct lpfc_nodelist *ndlp;
1733
1734 spin_lock_irq(shost->host_lock);
1735 ndlp = __lpfc_findnode_did(vport, did);
1736 spin_unlock_irq(shost->host_lock);
1737 return ndlp;
1738}
1739
1740struct lpfc_nodelist *
1741lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
dea3101e 1742{
2e0fef85 1743 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 1744 struct lpfc_nodelist *ndlp;
dea3101e 1745
2e0fef85 1746 ndlp = lpfc_findnode_did(vport, did);
c9f8735b 1747 if (!ndlp) {
2e0fef85
JS
1748 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
1749 lpfc_rscn_payload_check(vport, did) == 0)
dea3101e 1750 return NULL;
1751 ndlp = (struct lpfc_nodelist *)
2e0fef85 1752 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea3101e 1753 if (!ndlp)
1754 return NULL;
2e0fef85
JS
1755 lpfc_nlp_init(vport, ndlp, did);
1756 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1757 spin_lock_irq(shost->host_lock);
dea3101e 1758 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 1759 spin_unlock_irq(shost->host_lock);
dea3101e 1760 return ndlp;
1761 }
2e0fef85
JS
1762 if (vport->fc_flag & FC_RSCN_MODE) {
1763 if (lpfc_rscn_payload_check(vport, did)) {
1764 spin_lock_irq(shost->host_lock);
dea3101e 1765 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 1766 spin_unlock_irq(shost->host_lock);
c9f8735b
JW
1767
1768 /* Since this node is marked for discovery,
1769 * delay timeout is not needed.
1770 */
fdcebe28 1771 if (ndlp->nlp_flag & NLP_DELAY_TMO)
2e0fef85 1772 lpfc_cancel_retry_delay_tmo(vport, ndlp);
071fbd3d 1773 } else
dea3101e 1774 ndlp = NULL;
2fe165b6 1775 } else {
685f0bf7
JS
1776 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
1777 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE)
dea3101e 1778 return NULL;
2e0fef85
JS
1779 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1780 spin_lock_irq(shost->host_lock);
dea3101e 1781 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 1782 spin_unlock_irq(shost->host_lock);
dea3101e 1783 }
1784 return ndlp;
1785}
1786
1787/* Build a list of nodes to discover based on the loopmap */
1788void
2e0fef85 1789lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea3101e 1790{
2e0fef85 1791 struct lpfc_hba *phba = vport->phba;
dea3101e 1792 int j;
1793 uint32_t alpa, index;
1794
2e0fef85 1795 if (!lpfc_is_link_up(phba))
dea3101e 1796 return;
2e0fef85
JS
1797
1798 if (phba->fc_topology != TOPOLOGY_LOOP)
dea3101e 1799 return;
dea3101e 1800
1801 /* Check for loop map present or not */
1802 if (phba->alpa_map[0]) {
1803 for (j = 1; j <= phba->alpa_map[0]; j++) {
1804 alpa = phba->alpa_map[j];
2e0fef85 1805 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea3101e 1806 continue;
2e0fef85 1807 lpfc_setup_disc_node(vport, alpa);
dea3101e 1808 }
1809 } else {
1810 /* No alpamap, so try all alpa's */
1811 for (j = 0; j < FC_MAXLOOP; j++) {
1812 /* If cfg_scan_down is set, start from highest
1813 * ALPA (0xef) to lowest (0x1).
1814 */
1815 if (phba->cfg_scan_down)
1816 index = j;
1817 else
1818 index = FC_MAXLOOP - j - 1;
1819 alpa = lpfcAlpaArray[index];
2e0fef85 1820 if ((vport->fc_myDID & 0xff) == alpa)
dea3101e 1821 continue;
2e0fef85 1822 lpfc_setup_disc_node(vport, alpa);
dea3101e 1823 }
1824 }
1825 return;
1826}
1827
dea3101e 1828void
2e0fef85 1829lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea3101e 1830{
dea3101e 1831 LPFC_MBOXQ_t *mbox;
2e0fef85
JS
1832 struct lpfc_sli *psli = &phba->sli;
1833 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
1834 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
1835 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
1836 int rc;
1837
1838 /* Link up discovery */
1839 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
1840 phba->link_state = LPFC_CLEAR_LA;
1841 lpfc_clear_la(phba, mbox);
1842 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
1843 mbox->vport = vport;
1844 rc = lpfc_sli_issue_mbox(phba, mbox, (MBX_NOWAIT |
1845 MBX_STOP_IOCB));
1846 if (rc == MBX_NOT_FINISHED) {
1847 mempool_free(mbox, phba->mbox_mem_pool);
1848 lpfc_disc_flush_list(vport);
1849 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
1850 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
1851 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
1852 vport->port_state = LPFC_VPORT_READY;
1853 }
1854 }
1855}
1856
1857/* Start Link up / RSCN discovery on NPR nodes */
1858void
1859lpfc_disc_start(struct lpfc_vport *vport)
1860{
1861 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1862 struct lpfc_hba *phba = vport->phba;
dea3101e 1863 struct lpfc_nodelist *ndlp, *next_ndlp;
685f0bf7 1864 uint32_t num_sent;
dea3101e 1865 uint32_t clear_la_pending;
685f0bf7 1866 int did_changed;
dea3101e 1867
2e0fef85 1868 if (!lpfc_is_link_up(phba))
dea3101e 1869 return;
2e0fef85
JS
1870
1871 if (phba->link_state == LPFC_CLEAR_LA)
dea3101e 1872 clear_la_pending = 1;
1873 else
1874 clear_la_pending = 0;
1875
2e0fef85
JS
1876 if (vport->port_state < LPFC_VPORT_READY)
1877 vport->port_state = LPFC_DISC_AUTH;
dea3101e 1878
2e0fef85
JS
1879 lpfc_set_disctmo(vport);
1880
1881 if (vport->fc_prevDID == vport->fc_myDID)
dea3101e 1882 did_changed = 0;
2e0fef85 1883 else
dea3101e 1884 did_changed = 1;
2e0fef85
JS
1885
1886 vport->fc_prevDID = vport->fc_myDID;
1887 vport->num_disc_nodes = 0;
dea3101e 1888
1889 /* Start Discovery state <hba_state> */
1890 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1891 "%d:0202 Start Discovery hba state x%x "
1892 "Data: x%x x%x x%x\n",
2e0fef85
JS
1893 phba->brd_no, vport->port_state, vport->fc_flag,
1894 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
dea3101e 1895
1896 /* If our did changed, we MUST do PLOGI */
2e0fef85 1897 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
685f0bf7
JS
1898 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
1899 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
1900 did_changed) {
2e0fef85 1901 spin_lock_irq(shost->host_lock);
685f0bf7 1902 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2e0fef85 1903 spin_unlock_irq(shost->host_lock);
dea3101e 1904 }
1905 }
1906
1907 /* First do ADISCs - if any */
2e0fef85 1908 num_sent = lpfc_els_disc_adisc(vport);
dea3101e 1909
1910 if (num_sent)
1911 return;
1912
2e0fef85
JS
1913 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
1914 if (vport->port_type == LPFC_PHYSICAL_PORT) {
dea3101e 1915 /* If we get here, there is nothing to ADISC */
2e0fef85
JS
1916 lpfc_issue_clear_la(phba, vport);
1917 } else if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1918
1919 vport->num_disc_nodes = 0;
1920 /* go thru NPR nodes and issue ELS PLOGIs */
1921 if (vport->fc_npr_cnt)
1922 lpfc_els_disc_plogi(vport);
1923
1924 if (!vport->num_disc_nodes) {
1925 spin_lock_irq(shost->host_lock);
1926 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1927 spin_unlock_irq(shost->host_lock);
dea3101e 1928 }
2e0fef85 1929 vport->port_state = LPFC_VPORT_READY;
dea3101e 1930 }
1931 } else {
1932 /* Next do PLOGIs - if any */
2e0fef85 1933 num_sent = lpfc_els_disc_plogi(vport);
dea3101e 1934
1935 if (num_sent)
1936 return;
1937
2e0fef85 1938 if (vport->fc_flag & FC_RSCN_MODE) {
dea3101e 1939 /* Check to see if more RSCNs came in while we
1940 * were processing this one.
1941 */
2e0fef85
JS
1942 if ((vport->fc_rscn_id_cnt == 0) &&
1943 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
1944 spin_lock_irq(shost->host_lock);
1945 vport->fc_flag &= ~FC_RSCN_MODE;
1946 spin_unlock_irq(shost->host_lock);
2fe165b6 1947 } else
2e0fef85 1948 lpfc_els_handle_rscn(vport);
dea3101e 1949 }
1950 }
1951 return;
1952}
1953
1954/*
1955 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
1956 * ring the match the sppecified nodelist.
1957 */
1958static void
2e0fef85 1959lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 1960{
2534ba75 1961 LIST_HEAD(completions);
dea3101e 1962 struct lpfc_sli *psli;
1963 IOCB_t *icmd;
1964 struct lpfc_iocbq *iocb, *next_iocb;
1965 struct lpfc_sli_ring *pring;
dea3101e 1966
1967 psli = &phba->sli;
1968 pring = &psli->ring[LPFC_ELS_RING];
1969
1970 /* Error matching iocb on txq or txcmplq
1971 * First check the txq.
1972 */
2e0fef85 1973 spin_lock_irq(&phba->hbalock);
dea3101e 1974 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
1975 if (iocb->context1 != ndlp) {
1976 continue;
1977 }
1978 icmd = &iocb->iocb;
1979 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
1980 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
1981
2534ba75 1982 list_move_tail(&iocb->list, &completions);
dea3101e 1983 pring->txq_cnt--;
dea3101e 1984 }
1985 }
1986
1987 /* Next check the txcmplq */
1988 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1989 if (iocb->context1 != ndlp) {
1990 continue;
1991 }
1992 icmd = &iocb->iocb;
2e0fef85
JS
1993 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
1994 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
2534ba75
JS
1995 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1996 }
1997 }
2e0fef85 1998 spin_unlock_irq(&phba->hbalock);
dea3101e 1999
2534ba75
JS
2000 while (!list_empty(&completions)) {
2001 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
2002 list_del(&iocb->list);
dea3101e 2003
2e0fef85
JS
2004 if (!iocb->iocb_cmpl)
2005 lpfc_sli_release_iocbq(phba, iocb);
2006 else {
2534ba75
JS
2007 icmd = &iocb->iocb;
2008 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2009 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2010 (iocb->iocb_cmpl) (phba, iocb, iocb);
2e0fef85 2011 }
dea3101e 2012 }
dea3101e 2013}
2014
2015void
2e0fef85 2016lpfc_disc_flush_list(struct lpfc_vport *vport)
dea3101e 2017{
2018 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 2019 struct lpfc_hba *phba = vport->phba;
dea3101e 2020
2e0fef85
JS
2021 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2022 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
685f0bf7
JS
2023 nlp_listp) {
2024 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2025 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2026 lpfc_free_tx(phba, ndlp);
2027 lpfc_nlp_put(ndlp);
2028 }
dea3101e 2029 }
2030 }
dea3101e 2031}
2032
2033/*****************************************************************************/
2034/*
2035 * NAME: lpfc_disc_timeout
2036 *
2037 * FUNCTION: Fibre Channel driver discovery timeout routine.
2038 *
2039 * EXECUTION ENVIRONMENT: interrupt only
2040 *
2041 * CALLED FROM:
2042 * Timer function
2043 *
2044 * RETURNS:
2045 * none
2046 */
2047/*****************************************************************************/
2048void
2049lpfc_disc_timeout(unsigned long ptr)
2050{
2e0fef85
JS
2051 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2052 struct lpfc_hba *phba = vport->phba;
dea3101e 2053 unsigned long flags = 0;
2054
2055 if (unlikely(!phba))
2056 return;
2057
2e0fef85
JS
2058 if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2059 spin_lock_irqsave(&vport->work_port_lock, flags);
2060 vport->work_port_events |= WORKER_DISC_TMO;
2061 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2062
dea3101e 2063 if (phba->work_wait)
2064 wake_up(phba->work_wait);
2065 }
dea3101e 2066 return;
2067}
2068
2069static void
2e0fef85 2070lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea3101e 2071{
2e0fef85
JS
2072 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2073 struct lpfc_hba *phba = vport->phba;
2074 struct lpfc_sli *psli = &phba->sli;
c9f8735b 2075 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 2076 LPFC_MBOXQ_t *clearlambox, *initlinkmbox;
dea3101e 2077 int rc, clrlaerr = 0;
2078
2e0fef85 2079 if (!(vport->fc_flag & FC_DISC_TMO))
dea3101e 2080 return;
2081
2e0fef85
JS
2082 spin_lock_irq(shost->host_lock);
2083 vport->fc_flag &= ~FC_DISC_TMO;
2084 spin_unlock_irq(shost->host_lock);
dea3101e 2085
2e0fef85 2086 switch (vport->port_state) {
dea3101e 2087
2088 case LPFC_LOCAL_CFG_LINK:
2e0fef85
JS
2089 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2090 * FAN
2091 */
2092 /* FAN timeout */
ed957684 2093 lpfc_printf_log(phba, KERN_WARNING, LOG_DISCOVERY,
dea3101e 2094 "%d:0221 FAN timeout\n",
2095 phba->brd_no);
2096
c9f8735b 2097 /* Start discovery by sending FLOGI, clean up old rpis */
2e0fef85 2098 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
685f0bf7
JS
2099 nlp_listp) {
2100 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2101 continue;
c9f8735b
JW
2102 if (ndlp->nlp_type & NLP_FABRIC) {
2103 /* Clean up the ndlp on Fabric connections */
2e0fef85 2104 lpfc_drop_node(vport, ndlp);
2fe165b6 2105 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
c9f8735b
JW
2106 /* Fail outstanding IO now since device
2107 * is marked for PLOGI.
2108 */
2e0fef85 2109 lpfc_unreg_rpi(vport, ndlp);
c9f8735b
JW
2110 }
2111 }
2e0fef85
JS
2112 vport->port_state = LPFC_FLOGI;
2113 lpfc_set_disctmo(vport);
2114 lpfc_initial_flogi(vport);
dea3101e 2115 break;
2116
2117 case LPFC_FLOGI:
2e0fef85 2118 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea3101e 2119 /* Initial FLOGI timeout */
ed957684 2120 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
dea3101e 2121 "%d:0222 Initial FLOGI timeout\n",
2122 phba->brd_no);
2123
2124 /* Assume no Fabric and go on with discovery.
2125 * Check for outstanding ELS FLOGI to abort.
2126 */
2127
2128 /* FLOGI failed, so just use loop map to make discovery list */
2e0fef85 2129 lpfc_disc_list_loopmap(vport);
dea3101e 2130
2131 /* Start discovery */
2e0fef85 2132 lpfc_disc_start(vport);
dea3101e 2133 break;
2134
2135 case LPFC_FABRIC_CFG_LINK:
2136 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2137 NameServer login */
2138 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2139 "%d:0223 Timeout while waiting for NameServer "
2140 "login\n", phba->brd_no);
2141
2142 /* Next look for NameServer ndlp */
2e0fef85 2143 ndlp = lpfc_findnode_did(vport, NameServer_DID);
dea3101e 2144 if (ndlp)
329f9bc7 2145 lpfc_nlp_put(ndlp);
dea3101e 2146 /* Start discovery */
2e0fef85 2147 lpfc_disc_start(vport);
dea3101e 2148 break;
2149
2150 case LPFC_NS_QRY:
2151 /* Check for wait for NameServer Rsp timeout */
2152 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2153 "%d:0224 NameServer Query timeout "
2154 "Data: x%x x%x\n",
2155 phba->brd_no,
2e0fef85 2156 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea3101e 2157
2e0fef85 2158 ndlp = lpfc_findnode_did(vport, NameServer_DID);
685f0bf7 2159 if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
2e0fef85 2160 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
dea3101e 2161 /* Try it one more time */
2e0fef85 2162 rc = lpfc_ns_cmd(vport, ndlp, SLI_CTNS_GID_FT);
dea3101e 2163 if (rc == 0)
2164 break;
2165 }
2e0fef85 2166 vport->fc_ns_retry = 0;
dea3101e 2167 }
2168
2169 /* Nothing to authenticate, so CLEAR_LA right now */
2170 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2171 if (!clearlambox) {
2172 clrlaerr = 1;
2173 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2174 "%d:0226 Device Discovery "
2175 "completion error\n",
2176 phba->brd_no);
2e0fef85 2177 phba->link_state = LPFC_HBA_ERROR;
dea3101e 2178 break;
2179 }
2180
2e0fef85 2181 phba->link_state = LPFC_CLEAR_LA;
dea3101e 2182 lpfc_clear_la(phba, clearlambox);
2183 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2e0fef85 2184 clearlambox->vport = vport;
dea3101e 2185 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2186 (MBX_NOWAIT | MBX_STOP_IOCB));
2187 if (rc == MBX_NOT_FINISHED) {
2188 mempool_free(clearlambox, phba->mbox_mem_pool);
2189 clrlaerr = 1;
2190 break;
2191 }
2192
2193 /* Setup and issue mailbox INITIALIZE LINK command */
2194 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2195 if (!initlinkmbox) {
2196 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
dca9479b 2197 "%d:0206 Device Discovery "
dea3101e 2198 "completion error\n",
2199 phba->brd_no);
2e0fef85 2200 phba->link_state = LPFC_HBA_ERROR;
dea3101e 2201 break;
2202 }
2203
2204 lpfc_linkdown(phba);
2205 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2206 phba->cfg_link_speed);
2207 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
ed957684 2208 initlinkmbox->vport = vport;
dea3101e 2209 rc = lpfc_sli_issue_mbox(phba, initlinkmbox,
2210 (MBX_NOWAIT | MBX_STOP_IOCB));
5b8bd0c9 2211 lpfc_set_loopback_flag(phba);
dea3101e 2212 if (rc == MBX_NOT_FINISHED)
2213 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2214
2215 break;
2216
2217 case LPFC_DISC_AUTH:
2218 /* Node Authentication timeout */
ed957684 2219 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
dea3101e 2220 "%d:0227 Node Authentication timeout\n",
2221 phba->brd_no);
2e0fef85
JS
2222 lpfc_disc_flush_list(vport);
2223
dea3101e 2224 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2225 if (!clearlambox) {
2226 clrlaerr = 1;
2227 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
dca9479b 2228 "%d:0207 Device Discovery "
dea3101e 2229 "completion error\n",
2230 phba->brd_no);
2e0fef85 2231 phba->link_state = LPFC_HBA_ERROR;
dea3101e 2232 break;
2233 }
2e0fef85 2234 phba->link_state = LPFC_CLEAR_LA;
dea3101e 2235 lpfc_clear_la(phba, clearlambox);
2236 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2e0fef85 2237 clearlambox->vport = vport;
dea3101e 2238 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2239 (MBX_NOWAIT | MBX_STOP_IOCB));
2240 if (rc == MBX_NOT_FINISHED) {
2241 mempool_free(clearlambox, phba->mbox_mem_pool);
2242 clrlaerr = 1;
2243 }
2244 break;
2245
2e0fef85
JS
2246 case LPFC_VPORT_READY:
2247 if (vport->fc_flag & FC_RSCN_MODE) {
ed957684 2248 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
dea3101e 2249 "%d:0231 RSCN timeout Data: x%x x%x\n",
2250 phba->brd_no,
2e0fef85 2251 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea3101e 2252
2253 /* Cleanup any outstanding ELS commands */
2e0fef85 2254 lpfc_els_flush_cmd(vport);
dea3101e 2255
2e0fef85
JS
2256 lpfc_els_flush_rscn(vport);
2257 lpfc_disc_flush_list(vport);
dea3101e 2258 }
2259 break;
2e0fef85
JS
2260
2261 case LPFC_STATE_UNKNOWN:
2262 case LPFC_NS_REG:
2263 case LPFC_BUILD_DISC_LIST:
ed957684 2264 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2e0fef85
JS
2265 "%d:0229 Unexpected discovery timeout, vport "
2266 "State x%x\n",
ed957684 2267 vport->port_state, phba->brd_no);
2e0fef85
JS
2268
2269 break;
2270 }
2271
2272 switch (phba->link_state) {
2273 case LPFC_CLEAR_LA:
2274 /* CLEAR LA timeout */
ed957684 2275 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2e0fef85
JS
2276 "%d:0228 CLEAR LA timeout\n",
2277 phba->brd_no);
2278 clrlaerr = 1;
2279 break;
2280
2281 case LPFC_LINK_UNKNOWN:
2282 case LPFC_WARM_START:
2283 case LPFC_INIT_START:
2284 case LPFC_INIT_MBX_CMDS:
2285 case LPFC_LINK_DOWN:
2286 case LPFC_LINK_UP:
2287 case LPFC_HBA_ERROR:
ed957684 2288 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2e0fef85
JS
2289 "%d:0230 Unexpected timeout, hba link "
2290 "state x%x\n",
2291 phba->brd_no, phba->link_state);
2292 clrlaerr = 1;
2293 break;
dea3101e 2294 }
2295
2296 if (clrlaerr) {
2e0fef85 2297 lpfc_disc_flush_list(vport);
a4bc3379 2298 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 2299 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2300 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2e0fef85 2301 vport->port_state = LPFC_VPORT_READY;
dea3101e 2302 }
2303
2304 return;
2305}
2306
dea3101e 2307/*
2308 * This routine handles processing a NameServer REG_LOGIN mailbox
2309 * command upon completion. It is setup in the LPFC_MBOXQ
2310 * as the completion routine when the command is
2311 * handed off to the SLI layer.
2312 */
2313void
2e0fef85 2314lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2315{
2e0fef85
JS
2316 MAILBOX_t *mb = &pmb->mb;
2317 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2318 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2319 struct lpfc_vport *vport = pmb->vport;
dea3101e 2320
2321 pmb->context1 = NULL;
2322
dea3101e 2323 ndlp->nlp_rpi = mb->un.varWords[0];
dea3101e 2324 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 2325 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 2326
2e0fef85
JS
2327 /*
2328 * Start issuing Fabric-Device Management Interface (FDMI) command to
2329 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2330 * fdmi-on=2 (supporting RPA/hostnmae)
dea3101e 2331 */
2e0fef85
JS
2332
2333 if (phba->cfg_fdmi_on == 1)
2334 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2335 else
2336 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea3101e 2337
329f9bc7
JS
2338 /* Mailbox took a reference to the node */
2339 lpfc_nlp_put(ndlp);
dea3101e 2340 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2341 kfree(mp);
329f9bc7 2342 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2343
2344 return;
2345}
2346
685f0bf7
JS
2347static int
2348lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2349{
2350 uint16_t *rpi = param;
2351
2352 return ndlp->nlp_rpi == *rpi;
2353}
2354
2355static int
2356lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2357{
2358 return memcmp(&ndlp->nlp_portname, param,
2359 sizeof(ndlp->nlp_portname)) == 0;
2360}
2361
dea3101e 2362struct lpfc_nodelist *
2e0fef85 2363__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea3101e 2364{
21568f53 2365 struct lpfc_nodelist *ndlp;
dea3101e 2366
2e0fef85 2367 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
685f0bf7
JS
2368 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE &&
2369 filter(ndlp, param))
2370 return ndlp;
2371 }
21568f53 2372 return NULL;
dea3101e 2373}
2374
685f0bf7
JS
2375/*
2376 * Search node lists for a remote port matching filter criteria
2377 * This routine is used when the caller does NOT have host_lock.
2378 */
2379struct lpfc_nodelist *
2e0fef85 2380lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
685f0bf7 2381{
2e0fef85 2382 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
685f0bf7
JS
2383 struct lpfc_nodelist *ndlp;
2384
2e0fef85
JS
2385 spin_lock_irq(shost->host_lock);
2386 ndlp = __lpfc_find_node(vport, filter, param);
2387 spin_unlock_irq(shost->host_lock);
685f0bf7
JS
2388 return ndlp;
2389}
2390
2391/*
2392 * This routine looks up the ndlp lists for the given RPI. If rpi found it
2e0fef85 2393 * returns the node list element pointer else return NULL.
685f0bf7
JS
2394 */
2395struct lpfc_nodelist *
2e0fef85 2396__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
685f0bf7 2397{
2e0fef85 2398 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
685f0bf7
JS
2399}
2400
2534ba75 2401struct lpfc_nodelist *
2e0fef85 2402lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
2534ba75 2403{
2e0fef85 2404 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2534ba75
JS
2405 struct lpfc_nodelist *ndlp;
2406
2e0fef85
JS
2407 spin_lock_irq(shost->host_lock);
2408 ndlp = __lpfc_findnode_rpi(vport, rpi);
2409 spin_unlock_irq(shost->host_lock);
2534ba75
JS
2410 return ndlp;
2411}
2412
488d1469 2413/*
685f0bf7 2414 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
2e0fef85 2415 * returns the node element list pointer else return NULL.
488d1469
JS
2416 */
2417struct lpfc_nodelist *
2e0fef85 2418lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
488d1469 2419{
2e0fef85 2420 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
488d1469 2421 struct lpfc_nodelist *ndlp;
488d1469 2422
2e0fef85
JS
2423 spin_lock_irq(shost->host_lock);
2424 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2425 spin_unlock_irq(shost->host_lock);
488d1469
JS
2426 return NULL;
2427}
2428
dea3101e 2429void
2e0fef85
JS
2430lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2431 uint32_t did)
dea3101e 2432{
2433 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea3101e 2434 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
dea3101e 2435 init_timer(&ndlp->nlp_delayfunc);
2436 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2437 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2438 ndlp->nlp_DID = did;
2e0fef85 2439 ndlp->vport = vport;
dea3101e 2440 ndlp->nlp_sid = NLP_NO_SID;
685f0bf7 2441 INIT_LIST_HEAD(&ndlp->nlp_listp);
329f9bc7 2442 kref_init(&ndlp->kref);
dea3101e 2443 return;
2444}
329f9bc7
JS
2445
2446void
2447lpfc_nlp_release(struct kref *kref)
2448{
2449 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
2450 kref);
2e0fef85
JS
2451 lpfc_nlp_remove(ndlp->vport, ndlp);
2452 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
329f9bc7
JS
2453}
2454
2455struct lpfc_nodelist *
2456lpfc_nlp_get(struct lpfc_nodelist *ndlp)
2457{
2458 if (ndlp)
2459 kref_get(&ndlp->kref);
2460 return ndlp;
2461}
2462
2463int
2464lpfc_nlp_put(struct lpfc_nodelist *ndlp)
2465{
2466 return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
2467}
This page took 0.552517 seconds and 5 git commands to generate.