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