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