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