]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/lpfc/lpfc_nportdisc.c
[SCSI] lpfc 8.1.7: Fix memory leak and cleanup code related to per ring lookup array
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / lpfc / lpfc_nportdisc.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
2fe165b6 4 * Copyright (C) 2004-2006 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea3101e
JB
20 *******************************************************************/
21
dea3101e
JB
22#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25
91886523 26#include <scsi/scsi.h>
dea3101e
JB
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_transport_fc.h>
30
31#include "lpfc_hw.h"
32#include "lpfc_sli.h"
33#include "lpfc_disc.h"
34#include "lpfc_scsi.h"
35#include "lpfc.h"
36#include "lpfc_logmsg.h"
37#include "lpfc_crtn.h"
38
39
40/* Called to verify a rcv'ed ADISC was intended for us. */
41static int
42lpfc_check_adisc(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
43 struct lpfc_name * nn, struct lpfc_name * pn)
44{
45 /* Compare the ADISC rsp WWNN / WWPN matches our internal node
46 * table entry for that node.
47 */
48 if (memcmp(nn, &ndlp->nlp_nodename, sizeof (struct lpfc_name)) != 0)
c9f8735b 49 return 0;
dea3101e
JB
50
51 if (memcmp(pn, &ndlp->nlp_portname, sizeof (struct lpfc_name)) != 0)
c9f8735b 52 return 0;
dea3101e
JB
53
54 /* we match, return success */
c9f8735b 55 return 1;
dea3101e
JB
56}
57
dea3101e
JB
58int
59lpfc_check_sparm(struct lpfc_hba * phba,
60 struct lpfc_nodelist * ndlp, struct serv_parm * sp,
61 uint32_t class)
62{
63 volatile struct serv_parm *hsp = &phba->fc_sparam;
2fb70f79
JSEC
64 uint16_t hsp_value, ssp_value = 0;
65
66 /*
67 * The receive data field size and buffer-to-buffer receive data field
68 * size entries are 16 bits but are represented as two 8-bit fields in
69 * the driver data structure to account for rsvd bits and other control
70 * bits. Reconstruct and compare the fields as a 16-bit values before
71 * correcting the byte values.
72 */
dea3101e 73 if (sp->cls1.classValid) {
2fb70f79
JSEC
74 hsp_value = (hsp->cls1.rcvDataSizeMsb << 8) |
75 hsp->cls1.rcvDataSizeLsb;
76 ssp_value = (sp->cls1.rcvDataSizeMsb << 8) |
77 sp->cls1.rcvDataSizeLsb;
78 if (ssp_value > hsp_value) {
dea3101e 79 sp->cls1.rcvDataSizeLsb = hsp->cls1.rcvDataSizeLsb;
2fb70f79
JSEC
80 sp->cls1.rcvDataSizeMsb = hsp->cls1.rcvDataSizeMsb;
81 }
dea3101e 82 } else if (class == CLASS1) {
2fb70f79 83 return 0;
dea3101e
JB
84 }
85
86 if (sp->cls2.classValid) {
2fb70f79
JSEC
87 hsp_value = (hsp->cls2.rcvDataSizeMsb << 8) |
88 hsp->cls2.rcvDataSizeLsb;
89 ssp_value = (sp->cls2.rcvDataSizeMsb << 8) |
90 sp->cls2.rcvDataSizeLsb;
91 if (ssp_value > hsp_value) {
dea3101e 92 sp->cls2.rcvDataSizeLsb = hsp->cls2.rcvDataSizeLsb;
2fb70f79
JSEC
93 sp->cls2.rcvDataSizeMsb = hsp->cls2.rcvDataSizeMsb;
94 }
dea3101e 95 } else if (class == CLASS2) {
2fb70f79 96 return 0;
dea3101e
JB
97 }
98
99 if (sp->cls3.classValid) {
2fb70f79
JSEC
100 hsp_value = (hsp->cls3.rcvDataSizeMsb << 8) |
101 hsp->cls3.rcvDataSizeLsb;
102 ssp_value = (sp->cls3.rcvDataSizeMsb << 8) |
103 sp->cls3.rcvDataSizeLsb;
104 if (ssp_value > hsp_value) {
dea3101e 105 sp->cls3.rcvDataSizeLsb = hsp->cls3.rcvDataSizeLsb;
2fb70f79
JSEC
106 sp->cls3.rcvDataSizeMsb = hsp->cls3.rcvDataSizeMsb;
107 }
dea3101e 108 } else if (class == CLASS3) {
2fb70f79 109 return 0;
dea3101e
JB
110 }
111
2fb70f79
JSEC
112 /*
113 * Preserve the upper four bits of the MSB from the PLOGI response.
114 * These bits contain the Buffer-to-Buffer State Change Number
115 * from the target and need to be passed to the FW.
116 */
117 hsp_value = (hsp->cmn.bbRcvSizeMsb << 8) | hsp->cmn.bbRcvSizeLsb;
118 ssp_value = (sp->cmn.bbRcvSizeMsb << 8) | sp->cmn.bbRcvSizeLsb;
119 if (ssp_value > hsp_value) {
dea3101e 120 sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
2fb70f79
JSEC
121 sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
122 (hsp->cmn.bbRcvSizeMsb & 0x0F);
123 }
dea3101e 124
dea3101e
JB
125 memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
126 memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
2fb70f79 127 return 1;
dea3101e
JB
128}
129
130static void *
131lpfc_check_elscmpl_iocb(struct lpfc_hba * phba,
132 struct lpfc_iocbq *cmdiocb,
133 struct lpfc_iocbq *rspiocb)
134{
135 struct lpfc_dmabuf *pcmd, *prsp;
136 uint32_t *lp;
137 void *ptr = NULL;
138 IOCB_t *irsp;
139
140 irsp = &rspiocb->iocb;
141 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
142
143 /* For lpfc_els_abort, context2 could be zero'ed to delay
144 * freeing associated memory till after ABTS completes.
145 */
146 if (pcmd) {
147 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf,
148 list);
149 if (prsp) {
150 lp = (uint32_t *) prsp->virt;
151 ptr = (void *)((uint8_t *)lp + sizeof(uint32_t));
152 }
2fe165b6 153 } else {
dea3101e
JB
154 /* Force ulpStatus error since we are returning NULL ptr */
155 if (!(irsp->ulpStatus)) {
156 irsp->ulpStatus = IOSTAT_LOCAL_REJECT;
157 irsp->un.ulpWord[4] = IOERR_SLI_ABORTED;
158 }
159 ptr = NULL;
160 }
c9f8735b 161 return ptr;
dea3101e
JB
162}
163
164
165/*
166 * Free resources / clean up outstanding I/Os
167 * associated with a LPFC_NODELIST entry. This
168 * routine effectively results in a "software abort".
169 */
170int
171lpfc_els_abort(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
172 int send_abts)
173{
174 struct lpfc_sli *psli;
175 struct lpfc_sli_ring *pring;
176 struct lpfc_iocbq *iocb, *next_iocb;
177 IOCB_t *icmd;
178 int found = 0;
179
180 /* Abort outstanding I/O on NPort <nlp_DID> */
181 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
182 "%d:0201 Abort outstanding I/O on NPort x%x "
183 "Data: x%x x%x x%x\n",
184 phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
185 ndlp->nlp_state, ndlp->nlp_rpi);
186
187 psli = &phba->sli;
188 pring = &psli->ring[LPFC_ELS_RING];
189
190 /* First check the txq */
191 do {
192 found = 0;
193 spin_lock_irq(phba->host->host_lock);
194 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
195 /* Check to see if iocb matches the nport we are looking
196 for */
197 if ((lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))) {
198 found = 1;
199 /* It matches, so deque and call compl with an
200 error */
201 list_del(&iocb->list);
202 pring->txq_cnt--;
203 if (iocb->iocb_cmpl) {
204 icmd = &iocb->iocb;
205 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
206 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
207 spin_unlock_irq(phba->host->host_lock);
208 (iocb->iocb_cmpl) (phba, iocb, iocb);
209 spin_lock_irq(phba->host->host_lock);
604a3e30
JB
210 } else
211 lpfc_sli_release_iocbq(phba, iocb);
dea3101e
JB
212 break;
213 }
214 }
215 spin_unlock_irq(phba->host->host_lock);
216 } while (found);
217
218 /* Everything on txcmplq will be returned by firmware
219 * with a no rpi / linkdown / abort error. For ring 0,
220 * ELS discovery, we want to get rid of it right here.
221 */
222 /* Next check the txcmplq */
223 do {
224 found = 0;
225 spin_lock_irq(phba->host->host_lock);
226 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
227 list) {
228 /* Check to see if iocb matches the nport we are looking
229 for */
230 if ((lpfc_check_sli_ndlp (phba, pring, iocb, ndlp))) {
231 found = 1;
232 /* It matches, so deque and call compl with an
233 error */
234 list_del(&iocb->list);
235 pring->txcmplq_cnt--;
236
237 icmd = &iocb->iocb;
238 /* If the driver is completing an ELS
239 * command early, flush it out of the firmware.
240 */
241 if (send_abts &&
242 (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) &&
243 (icmd->un.elsreq64.bdl.ulpIoTag32)) {
244 lpfc_sli_issue_abort_iotag32(phba,
245 pring, iocb);
246 }
247 if (iocb->iocb_cmpl) {
248 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
249 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
250 spin_unlock_irq(phba->host->host_lock);
251 (iocb->iocb_cmpl) (phba, iocb, iocb);
252 spin_lock_irq(phba->host->host_lock);
604a3e30
JB
253 } else
254 lpfc_sli_release_iocbq(phba, iocb);
dea3101e
JB
255 break;
256 }
257 }
258 spin_unlock_irq(phba->host->host_lock);
259 } while(found);
260
261 /* If we are delaying issuing an ELS command, cancel it */
fdcebe28
JS
262 if (ndlp->nlp_flag & NLP_DELAY_TMO)
263 lpfc_cancel_retry_delay_tmo(phba, ndlp);
c9f8735b 264 return 0;
dea3101e
JB
265}
266
267static int
268lpfc_rcv_plogi(struct lpfc_hba * phba,
269 struct lpfc_nodelist * ndlp,
270 struct lpfc_iocbq *cmdiocb)
271{
272 struct lpfc_dmabuf *pcmd;
273 uint32_t *lp;
274 IOCB_t *icmd;
275 struct serv_parm *sp;
276 LPFC_MBOXQ_t *mbox;
277 struct ls_rjt stat;
278 int rc;
279
280 memset(&stat, 0, sizeof (struct ls_rjt));
281 if (phba->hba_state <= LPFC_FLOGI) {
282 /* Before responding to PLOGI, check for pt2pt mode.
283 * If we are pt2pt, with an outstanding FLOGI, abort
284 * the FLOGI and resend it first.
285 */
286 if (phba->fc_flag & FC_PT2PT) {
287 lpfc_els_abort_flogi(phba);
288 if (!(phba->fc_flag & FC_PT2PT_PLOGI)) {
289 /* If the other side is supposed to initiate
290 * the PLOGI anyway, just ACC it now and
291 * move on with discovery.
292 */
293 phba->fc_edtov = FF_DEF_EDTOV;
294 phba->fc_ratov = FF_DEF_RATOV;
295 /* Start discovery - this should just do
296 CLEAR_LA */
297 lpfc_disc_start(phba);
2fe165b6 298 } else {
dea3101e
JB
299 lpfc_initial_flogi(phba);
300 }
2fe165b6 301 } else {
dea3101e
JB
302 stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
303 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
304 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb,
305 ndlp);
306 return 0;
307 }
308 }
309 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
310 lp = (uint32_t *) pcmd->virt;
311 sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
312 if ((lpfc_check_sparm(phba, ndlp, sp, CLASS3) == 0)) {
313 /* Reject this request because invalid parameters */
314 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
315 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
316 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
c9f8735b 317 return 0;
dea3101e
JB
318 }
319 icmd = &cmdiocb->iocb;
320
321 /* PLOGI chkparm OK */
322 lpfc_printf_log(phba,
323 KERN_INFO,
324 LOG_ELS,
325 "%d:0114 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
326 phba->brd_no,
327 ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
328 ndlp->nlp_rpi);
329
330 if ((phba->cfg_fcp_class == 2) &&
331 (sp->cls2.classValid)) {
332 ndlp->nlp_fcp_info |= CLASS2;
333 } else {
334 ndlp->nlp_fcp_info |= CLASS3;
335 }
336 ndlp->nlp_class_sup = 0;
337 if (sp->cls1.classValid)
338 ndlp->nlp_class_sup |= FC_COS_CLASS1;
339 if (sp->cls2.classValid)
340 ndlp->nlp_class_sup |= FC_COS_CLASS2;
341 if (sp->cls3.classValid)
342 ndlp->nlp_class_sup |= FC_COS_CLASS3;
343 if (sp->cls4.classValid)
344 ndlp->nlp_class_sup |= FC_COS_CLASS4;
345 ndlp->nlp_maxframe =
346 ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
347
348 /* no need to reg_login if we are already in one of these states */
2fe165b6 349 switch (ndlp->nlp_state) {
dea3101e
JB
350 case NLP_STE_NPR_NODE:
351 if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
352 break;
353 case NLP_STE_REG_LOGIN_ISSUE:
354 case NLP_STE_PRLI_ISSUE:
355 case NLP_STE_UNMAPPED_NODE:
356 case NLP_STE_MAPPED_NODE:
357 lpfc_els_rsp_acc(phba, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL, 0);
c9f8735b 358 return 1;
dea3101e
JB
359 }
360
361 if ((phba->fc_flag & FC_PT2PT)
362 && !(phba->fc_flag & FC_PT2PT_PLOGI)) {
363 /* rcv'ed PLOGI decides what our NPortId will be */
364 phba->fc_myDID = icmd->un.rcvels.parmRo;
365 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
366 if (mbox == NULL)
367 goto out;
368 lpfc_config_link(phba, mbox);
369 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
370 rc = lpfc_sli_issue_mbox
371 (phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
372 if (rc == MBX_NOT_FINISHED) {
373 mempool_free( mbox, phba->mbox_mem_pool);
374 goto out;
375 }
376
377 lpfc_can_disctmo(phba);
378 }
379 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
380 if (mbox == NULL)
381 goto out;
382
383 if (lpfc_reg_login(phba, icmd->un.rcvels.remoteID,
384 (uint8_t *) sp, mbox, 0)) {
385 mempool_free( mbox, phba->mbox_mem_pool);
386 goto out;
387 }
388
389 /* ACC PLOGI rsp command needs to execute first,
390 * queue this mbox command to be processed later.
391 */
392 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
393 mbox->context2 = ndlp;
5024ab17 394 ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
dea3101e 395
dea3101e 396 lpfc_els_rsp_acc(phba, ELS_CMD_PLOGI, cmdiocb, ndlp, mbox, 0);
c9f8735b 397 return 1;
dea3101e
JB
398
399out:
400 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
401 stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
402 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
c9f8735b 403 return 0;
dea3101e
JB
404}
405
406static int
407lpfc_rcv_padisc(struct lpfc_hba * phba,
408 struct lpfc_nodelist * ndlp,
409 struct lpfc_iocbq *cmdiocb)
410{
411 struct lpfc_dmabuf *pcmd;
412 struct serv_parm *sp;
413 struct lpfc_name *pnn, *ppn;
414 struct ls_rjt stat;
415 ADISC *ap;
416 IOCB_t *icmd;
417 uint32_t *lp;
418 uint32_t cmd;
419
420 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
421 lp = (uint32_t *) pcmd->virt;
422
423 cmd = *lp++;
424 if (cmd == ELS_CMD_ADISC) {
425 ap = (ADISC *) lp;
426 pnn = (struct lpfc_name *) & ap->nodeName;
427 ppn = (struct lpfc_name *) & ap->portName;
428 } else {
429 sp = (struct serv_parm *) lp;
430 pnn = (struct lpfc_name *) & sp->nodeName;
431 ppn = (struct lpfc_name *) & sp->portName;
432 }
433
434 icmd = &cmdiocb->iocb;
435 if ((icmd->ulpStatus == 0) &&
436 (lpfc_check_adisc(phba, ndlp, pnn, ppn))) {
437 if (cmd == ELS_CMD_ADISC) {
438 lpfc_els_rsp_adisc_acc(phba, cmdiocb, ndlp);
2fe165b6 439 } else {
dea3101e
JB
440 lpfc_els_rsp_acc(phba, ELS_CMD_PLOGI, cmdiocb, ndlp,
441 NULL, 0);
442 }
c9f8735b 443 return 1;
dea3101e
JB
444 }
445 /* Reject this request because invalid parameters */
446 stat.un.b.lsRjtRsvd0 = 0;
447 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
448 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
449 stat.un.b.vendorUnique = 0;
450 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
451
dea3101e
JB
452 /* 1 sec timeout */
453 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
454
455 spin_lock_irq(phba->host->host_lock);
456 ndlp->nlp_flag |= NLP_DELAY_TMO;
457 spin_unlock_irq(phba->host->host_lock);
5024ab17
JW
458 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
459 ndlp->nlp_prev_state = ndlp->nlp_state;
dea3101e
JB
460 ndlp->nlp_state = NLP_STE_NPR_NODE;
461 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
c9f8735b 462 return 0;
dea3101e
JB
463}
464
465static int
466lpfc_rcv_logo(struct lpfc_hba * phba,
467 struct lpfc_nodelist * ndlp,
82d9a2a2
JS
468 struct lpfc_iocbq *cmdiocb,
469 uint32_t els_cmd)
dea3101e
JB
470{
471 /* Put ndlp on NPR list with 1 sec timeout for plogi, ACC logo */
472 /* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
473 * PLOGIs during LOGO storms from a device.
474 */
475 ndlp->nlp_flag |= NLP_LOGO_ACC;
82d9a2a2
JS
476 if (els_cmd == ELS_CMD_PRLO)
477 lpfc_els_rsp_acc(phba, ELS_CMD_PRLO, cmdiocb, ndlp, NULL, 0);
478 else
479 lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
dea3101e 480
c9f8735b
JW
481 if (!(ndlp->nlp_type & NLP_FABRIC) ||
482 (ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
dea3101e 483 /* Only try to re-login if this is NOT a Fabric Node */
dea3101e
JB
484 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
485 spin_lock_irq(phba->host->host_lock);
486 ndlp->nlp_flag |= NLP_DELAY_TMO;
487 spin_unlock_irq(phba->host->host_lock);
dea3101e 488
5024ab17
JW
489 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
490 ndlp->nlp_prev_state = ndlp->nlp_state;
c9f8735b
JW
491 ndlp->nlp_state = NLP_STE_NPR_NODE;
492 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
493 } else {
5024ab17 494 ndlp->nlp_prev_state = ndlp->nlp_state;
c9f8735b
JW
495 ndlp->nlp_state = NLP_STE_UNUSED_NODE;
496 lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
497 }
dea3101e 498
c9f8735b 499 spin_lock_irq(phba->host->host_lock);
dea3101e 500 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
c9f8735b 501 spin_unlock_irq(phba->host->host_lock);
dea3101e
JB
502 /* The driver has to wait until the ACC completes before it continues
503 * processing the LOGO. The action will resume in
504 * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
505 * unreg_login, the driver waits so the ACC does not get aborted.
506 */
c9f8735b 507 return 0;
dea3101e
JB
508}
509
510static void
511lpfc_rcv_prli(struct lpfc_hba * phba,
512 struct lpfc_nodelist * ndlp,
513 struct lpfc_iocbq *cmdiocb)
514{
515 struct lpfc_dmabuf *pcmd;
516 uint32_t *lp;
517 PRLI *npr;
518 struct fc_rport *rport = ndlp->rport;
519 u32 roles;
520
521 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
522 lp = (uint32_t *) pcmd->virt;
523 npr = (PRLI *) ((uint8_t *) lp + sizeof (uint32_t));
524
525 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
526 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
527 if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
528 (npr->prliType == PRLI_FCP_TYPE)) {
529 if (npr->initiatorFunc)
530 ndlp->nlp_type |= NLP_FCP_INITIATOR;
531 if (npr->targetFunc)
532 ndlp->nlp_type |= NLP_FCP_TARGET;
533 if (npr->Retry)
534 ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
535 }
536 if (rport) {
537 /* We need to update the rport role values */
538 roles = FC_RPORT_ROLE_UNKNOWN;
539 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
540 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
541 if (ndlp->nlp_type & NLP_FCP_TARGET)
542 roles |= FC_RPORT_ROLE_FCP_TARGET;
543 fc_remote_port_rolechg(rport, roles);
544 }
545}
546
547static uint32_t
548lpfc_disc_set_adisc(struct lpfc_hba * phba,
549 struct lpfc_nodelist * ndlp)
550{
551 /* Check config parameter use-adisc or FCP-2 */
552 if ((phba->cfg_use_adisc == 0) &&
553 !(phba->fc_flag & FC_RSCN_MODE)) {
554 if (!(ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
c9f8735b 555 return 0;
dea3101e
JB
556 }
557 spin_lock_irq(phba->host->host_lock);
558 ndlp->nlp_flag |= NLP_NPR_ADISC;
559 spin_unlock_irq(phba->host->host_lock);
c9f8735b 560 return 1;
dea3101e
JB
561}
562
563static uint32_t
564lpfc_disc_illegal(struct lpfc_hba * phba,
565 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
566{
567 lpfc_printf_log(phba,
568 KERN_ERR,
569 LOG_DISCOVERY,
570 "%d:0253 Illegal State Transition: node x%x event x%x, "
571 "state x%x Data: x%x x%x\n",
572 phba->brd_no,
573 ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
574 ndlp->nlp_flag);
c9f8735b 575 return ndlp->nlp_state;
dea3101e
JB
576}
577
578/* Start of Discovery State Machine routines */
579
580static uint32_t
581lpfc_rcv_plogi_unused_node(struct lpfc_hba * phba,
582 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
583{
584 struct lpfc_iocbq *cmdiocb;
585
586 cmdiocb = (struct lpfc_iocbq *) arg;
587
588 if (lpfc_rcv_plogi(phba, ndlp, cmdiocb)) {
5024ab17 589 ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
dea3101e
JB
590 ndlp->nlp_state = NLP_STE_UNUSED_NODE;
591 lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
c9f8735b 592 return ndlp->nlp_state;
dea3101e
JB
593 }
594 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
c9f8735b 595 return NLP_STE_FREED_NODE;
dea3101e
JB
596}
597
598static uint32_t
599lpfc_rcv_els_unused_node(struct lpfc_hba * phba,
600 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
601{
602 lpfc_issue_els_logo(phba, ndlp, 0);
603 lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
c9f8735b 604 return ndlp->nlp_state;
dea3101e
JB
605}
606
607static uint32_t
608lpfc_rcv_logo_unused_node(struct lpfc_hba * phba,
609 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
610{
611 struct lpfc_iocbq *cmdiocb;
612
613 cmdiocb = (struct lpfc_iocbq *) arg;
614
615 spin_lock_irq(phba->host->host_lock);
616 ndlp->nlp_flag |= NLP_LOGO_ACC;
617 spin_unlock_irq(phba->host->host_lock);
618 lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
619 lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
620
c9f8735b 621 return ndlp->nlp_state;
dea3101e
JB
622}
623
624static uint32_t
625lpfc_cmpl_logo_unused_node(struct lpfc_hba * phba,
626 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
627{
628 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
c9f8735b 629 return NLP_STE_FREED_NODE;
dea3101e
JB
630}
631
632static uint32_t
633lpfc_device_rm_unused_node(struct lpfc_hba * phba,
634 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
635{
636 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
c9f8735b 637 return NLP_STE_FREED_NODE;
dea3101e
JB
638}
639
640static uint32_t
641lpfc_rcv_plogi_plogi_issue(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
642 void *arg, uint32_t evt)
643{
644 struct lpfc_iocbq *cmdiocb = arg;
645 struct lpfc_dmabuf *pcmd;
646 struct serv_parm *sp;
647 uint32_t *lp;
648 struct ls_rjt stat;
649 int port_cmp;
650
651 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
652 lp = (uint32_t *) pcmd->virt;
653 sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
654
655 memset(&stat, 0, sizeof (struct ls_rjt));
656
657 /* For a PLOGI, we only accept if our portname is less
658 * than the remote portname.
659 */
660 phba->fc_stat.elsLogiCol++;
661 port_cmp = memcmp(&phba->fc_portname, &sp->portName,
662 sizeof (struct lpfc_name));
663
664 if (port_cmp >= 0) {
665 /* Reject this request because the remote node will accept
666 ours */
667 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
668 stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
669 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
2fe165b6 670 } else {
dea3101e
JB
671 lpfc_rcv_plogi(phba, ndlp, cmdiocb);
672 } /* if our portname was less */
673
c9f8735b
JW
674 return ndlp->nlp_state;
675}
676
677static uint32_t
678lpfc_rcv_logo_plogi_issue(struct lpfc_hba * phba,
679 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
680{
681 struct lpfc_iocbq *cmdiocb;
682
683 cmdiocb = (struct lpfc_iocbq *) arg;
684
685 /* software abort outstanding PLOGI */
686 lpfc_els_abort(phba, ndlp, 1);
687
82d9a2a2 688 lpfc_rcv_logo(phba, ndlp, cmdiocb, ELS_CMD_LOGO);
c9f8735b 689 return ndlp->nlp_state;
dea3101e
JB
690}
691
692static uint32_t
693lpfc_rcv_els_plogi_issue(struct lpfc_hba * phba,
694 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
695{
696 struct lpfc_iocbq *cmdiocb;
697
698 cmdiocb = (struct lpfc_iocbq *) arg;
699
700 /* software abort outstanding PLOGI */
701 lpfc_els_abort(phba, ndlp, 1);
dea3101e
JB
702
703 if (evt == NLP_EVT_RCV_LOGO) {
704 lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
2fe165b6 705 } else {
dea3101e
JB
706 lpfc_issue_els_logo(phba, ndlp, 0);
707 }
708
709 /* Put ndlp in npr list set plogi timer for 1 sec */
5024ab17
JW
710 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
711 spin_lock_irq(phba->host->host_lock);
712 ndlp->nlp_flag |= NLP_DELAY_TMO;
713 spin_unlock_irq(phba->host->host_lock);
714 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
715 ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
dea3101e
JB
716 ndlp->nlp_state = NLP_STE_NPR_NODE;
717 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
718
c9f8735b 719 return ndlp->nlp_state;
dea3101e
JB
720}
721
722static uint32_t
723lpfc_cmpl_plogi_plogi_issue(struct lpfc_hba * phba,
724 struct lpfc_nodelist * ndlp, void *arg,
725 uint32_t evt)
726{
727 struct lpfc_iocbq *cmdiocb, *rspiocb;
728 struct lpfc_dmabuf *pcmd, *prsp;
729 uint32_t *lp;
730 IOCB_t *irsp;
731 struct serv_parm *sp;
732 LPFC_MBOXQ_t *mbox;
733
734 cmdiocb = (struct lpfc_iocbq *) arg;
735 rspiocb = cmdiocb->context_un.rsp_iocb;
736
737 if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
5024ab17 738 /* Recovery from PLOGI collision logic */
c9f8735b 739 return ndlp->nlp_state;
dea3101e
JB
740 }
741
742 irsp = &rspiocb->iocb;
743
744 if (irsp->ulpStatus)
745 goto out;
746
747 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
748
749 prsp = list_get_first(&pcmd->list,
750 struct lpfc_dmabuf,
751 list);
752 lp = (uint32_t *) prsp->virt;
753
754 sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
755 if (!lpfc_check_sparm(phba, ndlp, sp, CLASS3))
756 goto out;
757
758 /* PLOGI chkparm OK */
759 lpfc_printf_log(phba,
760 KERN_INFO,
761 LOG_ELS,
762 "%d:0121 PLOGI chkparm OK "
763 "Data: x%x x%x x%x x%x\n",
764 phba->brd_no,
765 ndlp->nlp_DID, ndlp->nlp_state,
766 ndlp->nlp_flag, ndlp->nlp_rpi);
767
768 if ((phba->cfg_fcp_class == 2) &&
769 (sp->cls2.classValid)) {
770 ndlp->nlp_fcp_info |= CLASS2;
771 } else {
772 ndlp->nlp_fcp_info |= CLASS3;
773 }
774 ndlp->nlp_class_sup = 0;
775 if (sp->cls1.classValid)
776 ndlp->nlp_class_sup |= FC_COS_CLASS1;
777 if (sp->cls2.classValid)
778 ndlp->nlp_class_sup |= FC_COS_CLASS2;
779 if (sp->cls3.classValid)
780 ndlp->nlp_class_sup |= FC_COS_CLASS3;
781 if (sp->cls4.classValid)
782 ndlp->nlp_class_sup |= FC_COS_CLASS4;
783 ndlp->nlp_maxframe =
784 ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
785 sp->cmn.bbRcvSizeLsb;
786
787 if (!(mbox = mempool_alloc(phba->mbox_mem_pool,
788 GFP_KERNEL)))
789 goto out;
790
791 lpfc_unreg_rpi(phba, ndlp);
792 if (lpfc_reg_login
793 (phba, irsp->un.elsreq64.remoteID,
794 (uint8_t *) sp, mbox, 0) == 0) {
2fe165b6 795 switch (ndlp->nlp_DID) {
dea3101e
JB
796 case NameServer_DID:
797 mbox->mbox_cmpl =
798 lpfc_mbx_cmpl_ns_reg_login;
799 break;
800 case FDMI_DID:
801 mbox->mbox_cmpl =
802 lpfc_mbx_cmpl_fdmi_reg_login;
803 break;
804 default:
805 mbox->mbox_cmpl =
806 lpfc_mbx_cmpl_reg_login;
807 }
808 mbox->context2 = ndlp;
809 if (lpfc_sli_issue_mbox(phba, mbox,
810 (MBX_NOWAIT | MBX_STOP_IOCB))
811 != MBX_NOT_FINISHED) {
812 ndlp->nlp_state =
813 NLP_STE_REG_LOGIN_ISSUE;
814 lpfc_nlp_list(phba, ndlp,
815 NLP_REGLOGIN_LIST);
c9f8735b 816 return ndlp->nlp_state;
dea3101e
JB
817 }
818 mempool_free(mbox, phba->mbox_mem_pool);
819 } else {
820 mempool_free(mbox, phba->mbox_mem_pool);
821 }
822
823
824 out:
825 /* Free this node since the driver cannot login or has the wrong
826 sparm */
827 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
c9f8735b 828 return NLP_STE_FREED_NODE;
dea3101e
JB
829}
830
831static uint32_t
832lpfc_device_rm_plogi_issue(struct lpfc_hba * phba,
833 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
834{
a0f9b48d
JS
835 if(ndlp->nlp_flag & NLP_NPR_2B_DISC) {
836 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
837 return ndlp->nlp_state;
838 }
839 else {
840 /* software abort outstanding PLOGI */
841 lpfc_els_abort(phba, ndlp, 1);
dea3101e 842
a0f9b48d
JS
843 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
844 return NLP_STE_FREED_NODE;
845 }
dea3101e
JB
846}
847
848static uint32_t
849lpfc_device_recov_plogi_issue(struct lpfc_hba * phba,
850 struct lpfc_nodelist * ndlp, void *arg,
851 uint32_t evt)
852{
853 /* software abort outstanding PLOGI */
854 lpfc_els_abort(phba, ndlp, 1);
855
5024ab17 856 ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
dea3101e
JB
857 ndlp->nlp_state = NLP_STE_NPR_NODE;
858 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
859 spin_lock_irq(phba->host->host_lock);
a0f9b48d 860 ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
dea3101e
JB
861 spin_unlock_irq(phba->host->host_lock);
862
c9f8735b 863 return ndlp->nlp_state;
dea3101e
JB
864}
865
866static uint32_t
867lpfc_rcv_plogi_adisc_issue(struct lpfc_hba * phba,
868 struct lpfc_nodelist * ndlp, void *arg,
869 uint32_t evt)
870{
871 struct lpfc_iocbq *cmdiocb;
872
873 /* software abort outstanding ADISC */
874 lpfc_els_abort(phba, ndlp, 1);
875
876 cmdiocb = (struct lpfc_iocbq *) arg;
877
878 if (lpfc_rcv_plogi(phba, ndlp, cmdiocb)) {
c9f8735b 879 return ndlp->nlp_state;
dea3101e 880 }
5024ab17 881 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
dea3101e
JB
882 ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
883 lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
488d1469 884 lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
dea3101e 885
c9f8735b 886 return ndlp->nlp_state;
dea3101e
JB
887}
888
889static uint32_t
890lpfc_rcv_prli_adisc_issue(struct lpfc_hba * phba,
891 struct lpfc_nodelist * ndlp, void *arg,
892 uint32_t evt)
893{
894 struct lpfc_iocbq *cmdiocb;
895
896 cmdiocb = (struct lpfc_iocbq *) arg;
897
898 lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
c9f8735b 899 return ndlp->nlp_state;
dea3101e
JB
900}
901
902static uint32_t
903lpfc_rcv_logo_adisc_issue(struct lpfc_hba * phba,
904 struct lpfc_nodelist * ndlp, void *arg,
905 uint32_t evt)
906{
907 struct lpfc_iocbq *cmdiocb;
908
909 cmdiocb = (struct lpfc_iocbq *) arg;
910
911 /* software abort outstanding ADISC */
912 lpfc_els_abort(phba, ndlp, 0);
913
82d9a2a2 914 lpfc_rcv_logo(phba, ndlp, cmdiocb, ELS_CMD_LOGO);
c9f8735b 915 return ndlp->nlp_state;
dea3101e
JB
916}
917
918static uint32_t
919lpfc_rcv_padisc_adisc_issue(struct lpfc_hba * phba,
920 struct lpfc_nodelist * ndlp, void *arg,
921 uint32_t evt)
922{
923 struct lpfc_iocbq *cmdiocb;
924
925 cmdiocb = (struct lpfc_iocbq *) arg;
926
927 lpfc_rcv_padisc(phba, ndlp, cmdiocb);
c9f8735b 928 return ndlp->nlp_state;
dea3101e
JB
929}
930
931static uint32_t
932lpfc_rcv_prlo_adisc_issue(struct lpfc_hba * phba,
933 struct lpfc_nodelist * ndlp, void *arg,
934 uint32_t evt)
935{
936 struct lpfc_iocbq *cmdiocb;
937
938 cmdiocb = (struct lpfc_iocbq *) arg;
939
940 /* Treat like rcv logo */
82d9a2a2 941 lpfc_rcv_logo(phba, ndlp, cmdiocb, ELS_CMD_PRLO);
c9f8735b 942 return ndlp->nlp_state;
dea3101e
JB
943}
944
945static uint32_t
946lpfc_cmpl_adisc_adisc_issue(struct lpfc_hba * phba,
947 struct lpfc_nodelist * ndlp, void *arg,
948 uint32_t evt)
949{
950 struct lpfc_iocbq *cmdiocb, *rspiocb;
951 IOCB_t *irsp;
952 ADISC *ap;
953
954 cmdiocb = (struct lpfc_iocbq *) arg;
955 rspiocb = cmdiocb->context_un.rsp_iocb;
956
957 ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
958 irsp = &rspiocb->iocb;
959
960 if ((irsp->ulpStatus) ||
961 (!lpfc_check_adisc(phba, ndlp, &ap->nodeName, &ap->portName))) {
dea3101e
JB
962 /* 1 sec timeout */
963 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
964 spin_lock_irq(phba->host->host_lock);
965 ndlp->nlp_flag |= NLP_DELAY_TMO;
966 spin_unlock_irq(phba->host->host_lock);
5024ab17 967 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
dea3101e
JB
968
969 memset(&ndlp->nlp_nodename, 0, sizeof (struct lpfc_name));
970 memset(&ndlp->nlp_portname, 0, sizeof (struct lpfc_name));
971
5024ab17 972 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
dea3101e
JB
973 ndlp->nlp_state = NLP_STE_NPR_NODE;
974 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
975 lpfc_unreg_rpi(phba, ndlp);
c9f8735b 976 return ndlp->nlp_state;
dea3101e 977 }
5024ab17 978
2501322e 979 if (ndlp->nlp_type & NLP_FCP_TARGET) {
5024ab17 980 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
2501322e
JSEC
981 ndlp->nlp_state = NLP_STE_MAPPED_NODE;
982 lpfc_nlp_list(phba, ndlp, NLP_MAPPED_LIST);
983 } else {
5024ab17 984 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
2501322e
JSEC
985 ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
986 lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
987 }
c9f8735b 988 return ndlp->nlp_state;
dea3101e
JB
989}
990
991static uint32_t
992lpfc_device_rm_adisc_issue(struct lpfc_hba * phba,
993 struct lpfc_nodelist * ndlp, void *arg,
994 uint32_t evt)
995{
a0f9b48d
JS
996 if(ndlp->nlp_flag & NLP_NPR_2B_DISC) {
997 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
998 return ndlp->nlp_state;
999 }
1000 else {
1001 /* software abort outstanding ADISC */
1002 lpfc_els_abort(phba, ndlp, 1);
dea3101e 1003
a0f9b48d
JS
1004 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1005 return NLP_STE_FREED_NODE;
1006 }
dea3101e
JB
1007}
1008
1009static uint32_t
1010lpfc_device_recov_adisc_issue(struct lpfc_hba * phba,
1011 struct lpfc_nodelist * ndlp, void *arg,
1012 uint32_t evt)
1013{
1014 /* software abort outstanding ADISC */
1015 lpfc_els_abort(phba, ndlp, 1);
1016
5024ab17 1017 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
dea3101e
JB
1018 ndlp->nlp_state = NLP_STE_NPR_NODE;
1019 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1020 spin_lock_irq(phba->host->host_lock);
a0f9b48d 1021 ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
c9f8735b 1022 ndlp->nlp_flag |= NLP_NPR_ADISC;
dea3101e
JB
1023 spin_unlock_irq(phba->host->host_lock);
1024
c9f8735b 1025 return ndlp->nlp_state;
dea3101e
JB
1026}
1027
1028static uint32_t
1029lpfc_rcv_plogi_reglogin_issue(struct lpfc_hba * phba,
1030 struct lpfc_nodelist * ndlp, void *arg,
1031 uint32_t evt)
1032{
1033 struct lpfc_iocbq *cmdiocb;
1034
1035 cmdiocb = (struct lpfc_iocbq *) arg;
1036
1037 lpfc_rcv_plogi(phba, ndlp, cmdiocb);
c9f8735b 1038 return ndlp->nlp_state;
dea3101e
JB
1039}
1040
1041static uint32_t
1042lpfc_rcv_prli_reglogin_issue(struct lpfc_hba * phba,
1043 struct lpfc_nodelist * ndlp, void *arg,
1044 uint32_t evt)
1045{
1046 struct lpfc_iocbq *cmdiocb;
1047
1048 cmdiocb = (struct lpfc_iocbq *) arg;
1049
1050 lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
c9f8735b 1051 return ndlp->nlp_state;
dea3101e
JB
1052}
1053
1054static uint32_t
1055lpfc_rcv_logo_reglogin_issue(struct lpfc_hba * phba,
1056 struct lpfc_nodelist * ndlp, void *arg,
1057 uint32_t evt)
1058{
1059 struct lpfc_iocbq *cmdiocb;
1060
1061 cmdiocb = (struct lpfc_iocbq *) arg;
1062
82d9a2a2 1063 lpfc_rcv_logo(phba, ndlp, cmdiocb, ELS_CMD_LOGO);
c9f8735b 1064 return ndlp->nlp_state;
dea3101e
JB
1065}
1066
1067static uint32_t
1068lpfc_rcv_padisc_reglogin_issue(struct lpfc_hba * phba,
1069 struct lpfc_nodelist * ndlp, void *arg,
1070 uint32_t evt)
1071{
1072 struct lpfc_iocbq *cmdiocb;
1073
1074 cmdiocb = (struct lpfc_iocbq *) arg;
1075
1076 lpfc_rcv_padisc(phba, ndlp, cmdiocb);
c9f8735b 1077 return ndlp->nlp_state;
dea3101e
JB
1078}
1079
1080static uint32_t
1081lpfc_rcv_prlo_reglogin_issue(struct lpfc_hba * phba,
1082 struct lpfc_nodelist * ndlp, void *arg,
1083 uint32_t evt)
1084{
1085 struct lpfc_iocbq *cmdiocb;
1086
1087 cmdiocb = (struct lpfc_iocbq *) arg;
82d9a2a2 1088 lpfc_els_rsp_acc(phba, ELS_CMD_PRLO, cmdiocb, ndlp, NULL, 0);
c9f8735b 1089 return ndlp->nlp_state;
dea3101e
JB
1090}
1091
1092static uint32_t
1093lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_hba * phba,
1094 struct lpfc_nodelist * ndlp,
1095 void *arg, uint32_t evt)
1096{
1097 LPFC_MBOXQ_t *pmb;
1098 MAILBOX_t *mb;
1099 uint32_t did;
1100
1101 pmb = (LPFC_MBOXQ_t *) arg;
1102 mb = &pmb->mb;
1103 did = mb->un.varWords[1];
1104 if (mb->mbxStatus) {
1105 /* RegLogin failed */
1106 lpfc_printf_log(phba,
1107 KERN_ERR,
1108 LOG_DISCOVERY,
1109 "%d:0246 RegLogin failed Data: x%x x%x x%x\n",
1110 phba->brd_no,
1111 did, mb->mbxStatus, phba->hba_state);
1112
5024ab17 1113 /* Put ndlp in npr list set plogi timer for 1 sec */
dea3101e
JB
1114 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
1115 spin_lock_irq(phba->host->host_lock);
1116 ndlp->nlp_flag |= NLP_DELAY_TMO;
1117 spin_unlock_irq(phba->host->host_lock);
5024ab17 1118 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
dea3101e
JB
1119
1120 lpfc_issue_els_logo(phba, ndlp, 0);
5024ab17 1121 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
dea3101e
JB
1122 ndlp->nlp_state = NLP_STE_NPR_NODE;
1123 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
c9f8735b 1124 return ndlp->nlp_state;
dea3101e
JB
1125 }
1126
dea3101e 1127 ndlp->nlp_rpi = mb->un.varWords[0];
dea3101e
JB
1128
1129 /* Only if we are not a fabric nport do we issue PRLI */
1130 if (!(ndlp->nlp_type & NLP_FABRIC)) {
5024ab17 1131 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
dea3101e
JB
1132 ndlp->nlp_state = NLP_STE_PRLI_ISSUE;
1133 lpfc_nlp_list(phba, ndlp, NLP_PRLI_LIST);
1134 lpfc_issue_els_prli(phba, ndlp, 0);
1135 } else {
5024ab17 1136 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
dea3101e
JB
1137 ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
1138 lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
1139 }
c9f8735b 1140 return ndlp->nlp_state;
dea3101e
JB
1141}
1142
1143static uint32_t
1144lpfc_device_rm_reglogin_issue(struct lpfc_hba * phba,
1145 struct lpfc_nodelist * ndlp, void *arg,
1146 uint32_t evt)
1147{
a0f9b48d
JS
1148 if(ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1149 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1150 return ndlp->nlp_state;
1151 }
1152 else {
1153 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1154 return NLP_STE_FREED_NODE;
1155 }
dea3101e
JB
1156}
1157
1158static uint32_t
1159lpfc_device_recov_reglogin_issue(struct lpfc_hba * phba,
1160 struct lpfc_nodelist * ndlp, void *arg,
1161 uint32_t evt)
1162{
5024ab17 1163 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
dea3101e
JB
1164 ndlp->nlp_state = NLP_STE_NPR_NODE;
1165 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1166 spin_lock_irq(phba->host->host_lock);
a0f9b48d 1167 ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
dea3101e 1168 spin_unlock_irq(phba->host->host_lock);
c9f8735b 1169 return ndlp->nlp_state;
dea3101e
JB
1170}
1171
1172static uint32_t
1173lpfc_rcv_plogi_prli_issue(struct lpfc_hba * phba,
1174 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1175{
1176 struct lpfc_iocbq *cmdiocb;
1177
1178 cmdiocb = (struct lpfc_iocbq *) arg;
1179
1180 lpfc_rcv_plogi(phba, ndlp, cmdiocb);
c9f8735b 1181 return ndlp->nlp_state;
dea3101e
JB
1182}
1183
1184static uint32_t
1185lpfc_rcv_prli_prli_issue(struct lpfc_hba * phba,
1186 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1187{
1188 struct lpfc_iocbq *cmdiocb;
1189
1190 cmdiocb = (struct lpfc_iocbq *) arg;
1191
1192 lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
c9f8735b 1193 return ndlp->nlp_state;
dea3101e
JB
1194}
1195
1196static uint32_t
1197lpfc_rcv_logo_prli_issue(struct lpfc_hba * phba,
1198 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1199{
1200 struct lpfc_iocbq *cmdiocb;
1201
1202 cmdiocb = (struct lpfc_iocbq *) arg;
1203
1204 /* Software abort outstanding PRLI before sending acc */
1205 lpfc_els_abort(phba, ndlp, 1);
1206
82d9a2a2 1207 lpfc_rcv_logo(phba, ndlp, cmdiocb, ELS_CMD_LOGO);
c9f8735b 1208 return ndlp->nlp_state;
dea3101e
JB
1209}
1210
1211static uint32_t
1212lpfc_rcv_padisc_prli_issue(struct lpfc_hba * phba,
1213 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1214{
1215 struct lpfc_iocbq *cmdiocb;
1216
1217 cmdiocb = (struct lpfc_iocbq *) arg;
1218
1219 lpfc_rcv_padisc(phba, ndlp, cmdiocb);
c9f8735b 1220 return ndlp->nlp_state;
dea3101e
JB
1221}
1222
1223/* This routine is envoked when we rcv a PRLO request from a nport
1224 * we are logged into. We should send back a PRLO rsp setting the
1225 * appropriate bits.
1226 * NEXT STATE = PRLI_ISSUE
1227 */
1228static uint32_t
1229lpfc_rcv_prlo_prli_issue(struct lpfc_hba * phba,
1230 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1231{
1232 struct lpfc_iocbq *cmdiocb;
1233
1234 cmdiocb = (struct lpfc_iocbq *) arg;
82d9a2a2 1235 lpfc_els_rsp_acc(phba, ELS_CMD_PRLO, cmdiocb, ndlp, NULL, 0);
c9f8735b 1236 return ndlp->nlp_state;
dea3101e
JB
1237}
1238
1239static uint32_t
1240lpfc_cmpl_prli_prli_issue(struct lpfc_hba * phba,
1241 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1242{
1243 struct lpfc_iocbq *cmdiocb, *rspiocb;
1244 IOCB_t *irsp;
1245 PRLI *npr;
1246
1247 cmdiocb = (struct lpfc_iocbq *) arg;
1248 rspiocb = cmdiocb->context_un.rsp_iocb;
1249 npr = (PRLI *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1250
1251 irsp = &rspiocb->iocb;
1252 if (irsp->ulpStatus) {
5024ab17 1253 ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
dea3101e
JB
1254 ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
1255 lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
c9f8735b 1256 return ndlp->nlp_state;
dea3101e
JB
1257 }
1258
1259 /* Check out PRLI rsp */
1260 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1261 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
1262 if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
1263 (npr->prliType == PRLI_FCP_TYPE)) {
1264 if (npr->initiatorFunc)
1265 ndlp->nlp_type |= NLP_FCP_INITIATOR;
1266 if (npr->targetFunc)
1267 ndlp->nlp_type |= NLP_FCP_TARGET;
1268 if (npr->Retry)
1269 ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
1270 }
1271
5024ab17 1272 ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
dea3101e
JB
1273 ndlp->nlp_state = NLP_STE_MAPPED_NODE;
1274 lpfc_nlp_list(phba, ndlp, NLP_MAPPED_LIST);
c9f8735b 1275 return ndlp->nlp_state;
dea3101e
JB
1276}
1277
1278/*! lpfc_device_rm_prli_issue
1279 *
1280 * \pre
1281 * \post
1282 * \param phba
1283 * \param ndlp
1284 * \param arg
1285 * \param evt
1286 * \return uint32_t
1287 *
1288 * \b Description:
1289 * This routine is envoked when we a request to remove a nport we are in the
1290 * process of PRLIing. We should software abort outstanding prli, unreg
1291 * login, send a logout. We will change node state to UNUSED_NODE, put it
1292 * on plogi list so it can be freed when LOGO completes.
1293 *
1294 */
1295static uint32_t
1296lpfc_device_rm_prli_issue(struct lpfc_hba * phba,
1297 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1298{
a0f9b48d
JS
1299 if(ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1300 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1301 return ndlp->nlp_state;
1302 }
1303 else {
1304 /* software abort outstanding PLOGI */
1305 lpfc_els_abort(phba, ndlp, 1);
dea3101e 1306
a0f9b48d
JS
1307 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1308 return NLP_STE_FREED_NODE;
1309 }
dea3101e
JB
1310}
1311
1312
1313/*! lpfc_device_recov_prli_issue
1314 *
1315 * \pre
1316 * \post
1317 * \param phba
1318 * \param ndlp
1319 * \param arg
1320 * \param evt
1321 * \return uint32_t
1322 *
1323 * \b Description:
1324 * The routine is envoked when the state of a device is unknown, like
1325 * during a link down. We should remove the nodelist entry from the
1326 * unmapped list, issue a UNREG_LOGIN, do a software abort of the
1327 * outstanding PRLI command, then free the node entry.
1328 */
1329static uint32_t
1330lpfc_device_recov_prli_issue(struct lpfc_hba * phba,
1331 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1332{
1333 /* software abort outstanding PRLI */
1334 lpfc_els_abort(phba, ndlp, 1);
1335
5024ab17 1336 ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
dea3101e
JB
1337 ndlp->nlp_state = NLP_STE_NPR_NODE;
1338 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1339 spin_lock_irq(phba->host->host_lock);
a0f9b48d 1340 ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
dea3101e 1341 spin_unlock_irq(phba->host->host_lock);
c9f8735b 1342 return ndlp->nlp_state;
dea3101e
JB
1343}
1344
1345static uint32_t
1346lpfc_rcv_plogi_unmap_node(struct lpfc_hba * phba,
1347 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1348{
1349 struct lpfc_iocbq *cmdiocb;
1350
1351 cmdiocb = (struct lpfc_iocbq *) arg;
1352
1353 lpfc_rcv_plogi(phba, ndlp, cmdiocb);
c9f8735b 1354 return ndlp->nlp_state;
dea3101e
JB
1355}
1356
1357static uint32_t
1358lpfc_rcv_prli_unmap_node(struct lpfc_hba * phba,
1359 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1360{
1361 struct lpfc_iocbq *cmdiocb;
1362
1363 cmdiocb = (struct lpfc_iocbq *) arg;
1364
1365 lpfc_rcv_prli(phba, ndlp, cmdiocb);
1366 lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
c9f8735b 1367 return ndlp->nlp_state;
dea3101e
JB
1368}
1369
1370static uint32_t
1371lpfc_rcv_logo_unmap_node(struct lpfc_hba * phba,
1372 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1373{
1374 struct lpfc_iocbq *cmdiocb;
1375
1376 cmdiocb = (struct lpfc_iocbq *) arg;
1377
82d9a2a2 1378 lpfc_rcv_logo(phba, ndlp, cmdiocb, ELS_CMD_LOGO);
c9f8735b 1379 return ndlp->nlp_state;
dea3101e
JB
1380}
1381
1382static uint32_t
1383lpfc_rcv_padisc_unmap_node(struct lpfc_hba * phba,
1384 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1385{
1386 struct lpfc_iocbq *cmdiocb;
1387
1388 cmdiocb = (struct lpfc_iocbq *) arg;
1389
1390 lpfc_rcv_padisc(phba, ndlp, cmdiocb);
c9f8735b 1391 return ndlp->nlp_state;
dea3101e
JB
1392}
1393
1394static uint32_t
1395lpfc_rcv_prlo_unmap_node(struct lpfc_hba * phba,
1396 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1397{
1398 struct lpfc_iocbq *cmdiocb;
1399
1400 cmdiocb = (struct lpfc_iocbq *) arg;
1401
82d9a2a2 1402 lpfc_els_rsp_acc(phba, ELS_CMD_PRLO, cmdiocb, ndlp, NULL, 0);
c9f8735b 1403 return ndlp->nlp_state;
dea3101e
JB
1404}
1405
1406static uint32_t
1407lpfc_device_recov_unmap_node(struct lpfc_hba * phba,
1408 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1409{
5024ab17 1410 ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
dea3101e
JB
1411 ndlp->nlp_state = NLP_STE_NPR_NODE;
1412 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
a0f9b48d 1413 ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
dea3101e
JB
1414 lpfc_disc_set_adisc(phba, ndlp);
1415
c9f8735b 1416 return ndlp->nlp_state;
dea3101e
JB
1417}
1418
1419static uint32_t
1420lpfc_rcv_plogi_mapped_node(struct lpfc_hba * phba,
1421 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1422{
1423 struct lpfc_iocbq *cmdiocb;
1424
1425 cmdiocb = (struct lpfc_iocbq *) arg;
1426
1427 lpfc_rcv_plogi(phba, ndlp, cmdiocb);
c9f8735b 1428 return ndlp->nlp_state;
dea3101e
JB
1429}
1430
1431static uint32_t
1432lpfc_rcv_prli_mapped_node(struct lpfc_hba * phba,
1433 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1434{
1435 struct lpfc_iocbq *cmdiocb;
1436
1437 cmdiocb = (struct lpfc_iocbq *) arg;
1438
1439 lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
c9f8735b 1440 return ndlp->nlp_state;
dea3101e
JB
1441}
1442
1443static uint32_t
1444lpfc_rcv_logo_mapped_node(struct lpfc_hba * phba,
1445 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1446{
1447 struct lpfc_iocbq *cmdiocb;
1448
1449 cmdiocb = (struct lpfc_iocbq *) arg;
1450
82d9a2a2 1451 lpfc_rcv_logo(phba, ndlp, cmdiocb, ELS_CMD_LOGO);
c9f8735b 1452 return ndlp->nlp_state;
dea3101e
JB
1453}
1454
1455static uint32_t
1456lpfc_rcv_padisc_mapped_node(struct lpfc_hba * phba,
1457 struct lpfc_nodelist * ndlp, void *arg,
1458 uint32_t evt)
1459{
1460 struct lpfc_iocbq *cmdiocb;
1461
1462 cmdiocb = (struct lpfc_iocbq *) arg;
1463
1464 lpfc_rcv_padisc(phba, ndlp, cmdiocb);
c9f8735b 1465 return ndlp->nlp_state;
dea3101e
JB
1466}
1467
1468static uint32_t
1469lpfc_rcv_prlo_mapped_node(struct lpfc_hba * phba,
1470 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1471{
1472 struct lpfc_iocbq *cmdiocb;
1473
1474 cmdiocb = (struct lpfc_iocbq *) arg;
1475
1476 /* flush the target */
1477 spin_lock_irq(phba->host->host_lock);
1478 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1479 ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
1480 spin_unlock_irq(phba->host->host_lock);
1481
1482 /* Treat like rcv logo */
82d9a2a2 1483 lpfc_rcv_logo(phba, ndlp, cmdiocb, ELS_CMD_PRLO);
c9f8735b 1484 return ndlp->nlp_state;
dea3101e
JB
1485}
1486
1487static uint32_t
1488lpfc_device_recov_mapped_node(struct lpfc_hba * phba,
1489 struct lpfc_nodelist * ndlp, void *arg,
1490 uint32_t evt)
1491{
5024ab17 1492 ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
dea3101e
JB
1493 ndlp->nlp_state = NLP_STE_NPR_NODE;
1494 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1495 spin_lock_irq(phba->host->host_lock);
a0f9b48d 1496 ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
dea3101e
JB
1497 spin_unlock_irq(phba->host->host_lock);
1498 lpfc_disc_set_adisc(phba, ndlp);
c9f8735b 1499 return ndlp->nlp_state;
dea3101e
JB
1500}
1501
1502static uint32_t
1503lpfc_rcv_plogi_npr_node(struct lpfc_hba * phba,
1504 struct lpfc_nodelist * ndlp, void *arg,
1505 uint32_t evt)
1506{
1507 struct lpfc_iocbq *cmdiocb;
1508
1509 cmdiocb = (struct lpfc_iocbq *) arg;
1510
1511 /* Ignore PLOGI if we have an outstanding LOGO */
1512 if (ndlp->nlp_flag & NLP_LOGO_SND) {
c9f8735b 1513 return ndlp->nlp_state;
dea3101e
JB
1514 }
1515
1516 if (lpfc_rcv_plogi(phba, ndlp, cmdiocb)) {
1517 spin_lock_irq(phba->host->host_lock);
fdcebe28 1518 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea3101e 1519 spin_unlock_irq(phba->host->host_lock);
c9f8735b 1520 return ndlp->nlp_state;
dea3101e
JB
1521 }
1522
1523 /* send PLOGI immediately, move to PLOGI issue state */
1524 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
488d1469
JS
1525 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1526 ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1527 lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1528 lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
dea3101e 1529 }
488d1469 1530
c9f8735b 1531 return ndlp->nlp_state;
dea3101e
JB
1532}
1533
1534static uint32_t
1535lpfc_rcv_prli_npr_node(struct lpfc_hba * phba,
1536 struct lpfc_nodelist * ndlp, void *arg,
1537 uint32_t evt)
1538{
1539 struct lpfc_iocbq *cmdiocb;
1540 struct ls_rjt stat;
1541
1542 cmdiocb = (struct lpfc_iocbq *) arg;
1543
1544 memset(&stat, 0, sizeof (struct ls_rjt));
1545 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1546 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1547 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
1548
1549 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1550 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
c9f8735b
JW
1551 spin_lock_irq(phba->host->host_lock);
1552 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1553 spin_unlock_irq(phba->host->host_lock);
5024ab17 1554 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
dea3101e
JB
1555 ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
1556 lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
1557 lpfc_issue_els_adisc(phba, ndlp, 0);
1558 } else {
5024ab17 1559 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
dea3101e
JB
1560 ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1561 lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
488d1469 1562 lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
dea3101e 1563 }
488d1469 1564
dea3101e 1565 }
c9f8735b 1566 return ndlp->nlp_state;
dea3101e
JB
1567}
1568
1569static uint32_t
1570lpfc_rcv_logo_npr_node(struct lpfc_hba * phba,
1571 struct lpfc_nodelist * ndlp, void *arg,
1572 uint32_t evt)
1573{
1574 struct lpfc_iocbq *cmdiocb;
1575
1576 cmdiocb = (struct lpfc_iocbq *) arg;
1577
82d9a2a2 1578 lpfc_rcv_logo(phba, ndlp, cmdiocb, ELS_CMD_LOGO);
c9f8735b 1579 return ndlp->nlp_state;
dea3101e
JB
1580}
1581
1582static uint32_t
1583lpfc_rcv_padisc_npr_node(struct lpfc_hba * phba,
1584 struct lpfc_nodelist * ndlp, void *arg,
1585 uint32_t evt)
1586{
1587 struct lpfc_iocbq *cmdiocb;
1588
1589 cmdiocb = (struct lpfc_iocbq *) arg;
1590
1591 lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1592
1593 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1594 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
5024ab17 1595 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
dea3101e
JB
1596 ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
1597 lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
1598 lpfc_issue_els_adisc(phba, ndlp, 0);
1599 } else {
5024ab17 1600 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
dea3101e
JB
1601 ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1602 lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
488d1469 1603 lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
dea3101e
JB
1604 }
1605 }
c9f8735b 1606 return ndlp->nlp_state;
dea3101e
JB
1607}
1608
1609static uint32_t
1610lpfc_rcv_prlo_npr_node(struct lpfc_hba * phba,
1611 struct lpfc_nodelist * ndlp, void *arg,
1612 uint32_t evt)
1613{
1614 struct lpfc_iocbq *cmdiocb;
1615
1616 cmdiocb = (struct lpfc_iocbq *) arg;
1617
c9f8735b
JW
1618 spin_lock_irq(phba->host->host_lock);
1619 ndlp->nlp_flag |= NLP_LOGO_ACC;
1620 spin_unlock_irq(phba->host->host_lock);
1621
dea3101e
JB
1622 lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
1623
c9f8735b
JW
1624 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1625 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
1626 spin_lock_irq(phba->host->host_lock);
1627 ndlp->nlp_flag |= NLP_DELAY_TMO;
1628 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1629 spin_unlock_irq(phba->host->host_lock);
5024ab17 1630 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
c9f8735b
JW
1631 } else {
1632 spin_lock_irq(phba->host->host_lock);
1633 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1634 spin_unlock_irq(phba->host->host_lock);
dea3101e 1635 }
c9f8735b
JW
1636 return ndlp->nlp_state;
1637}
dea3101e 1638
c9f8735b
JW
1639static uint32_t
1640lpfc_cmpl_plogi_npr_node(struct lpfc_hba * phba,
1641 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1642{
1643 struct lpfc_iocbq *cmdiocb, *rspiocb;
a0f9b48d 1644 IOCB_t *irsp;
c9f8735b
JW
1645
1646 cmdiocb = (struct lpfc_iocbq *) arg;
1647 rspiocb = cmdiocb->context_un.rsp_iocb;
a0f9b48d
JS
1648
1649 irsp = &rspiocb->iocb;
1650 if (irsp->ulpStatus) {
1651 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1652 return NLP_STE_FREED_NODE;
1653 }
c9f8735b
JW
1654 return ndlp->nlp_state;
1655}
1656
1657static uint32_t
1658lpfc_cmpl_prli_npr_node(struct lpfc_hba * phba,
1659 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1660{
1661 struct lpfc_iocbq *cmdiocb, *rspiocb;
a0f9b48d 1662 IOCB_t *irsp;
c9f8735b
JW
1663
1664 cmdiocb = (struct lpfc_iocbq *) arg;
1665 rspiocb = cmdiocb->context_un.rsp_iocb;
a0f9b48d
JS
1666
1667 irsp = &rspiocb->iocb;
1668 if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
1669 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1670 return NLP_STE_FREED_NODE;
1671 }
c9f8735b 1672 return ndlp->nlp_state;
dea3101e
JB
1673}
1674
1675static uint32_t
1676lpfc_cmpl_logo_npr_node(struct lpfc_hba * phba,
1677 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1678{
1679 lpfc_unreg_rpi(phba, ndlp);
1680 /* This routine does nothing, just return the current state */
c9f8735b
JW
1681 return ndlp->nlp_state;
1682}
1683
1684static uint32_t
1685lpfc_cmpl_adisc_npr_node(struct lpfc_hba * phba,
1686 struct lpfc_nodelist * ndlp, void *arg,
1687 uint32_t evt)
1688{
1689 struct lpfc_iocbq *cmdiocb, *rspiocb;
a0f9b48d 1690 IOCB_t *irsp;
c9f8735b
JW
1691
1692 cmdiocb = (struct lpfc_iocbq *) arg;
1693 rspiocb = cmdiocb->context_un.rsp_iocb;
a0f9b48d
JS
1694
1695 irsp = &rspiocb->iocb;
1696 if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
1697 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1698 return NLP_STE_FREED_NODE;
1699 }
c9f8735b 1700 return ndlp->nlp_state;
dea3101e
JB
1701}
1702
1703static uint32_t
1704lpfc_cmpl_reglogin_npr_node(struct lpfc_hba * phba,
1705 struct lpfc_nodelist * ndlp, void *arg,
1706 uint32_t evt)
1707{
1708 LPFC_MBOXQ_t *pmb;
1709 MAILBOX_t *mb;
1710
1711 pmb = (LPFC_MBOXQ_t *) arg;
1712 mb = &pmb->mb;
1713
c9f8735b
JW
1714 if (!mb->mbxStatus)
1715 ndlp->nlp_rpi = mb->un.varWords[0];
a0f9b48d
JS
1716 else {
1717 if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
1718 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1719 return NLP_STE_FREED_NODE;
1720 }
1721 }
c9f8735b 1722 return ndlp->nlp_state;
dea3101e
JB
1723}
1724
1725static uint32_t
1726lpfc_device_rm_npr_node(struct lpfc_hba * phba,
1727 struct lpfc_nodelist * ndlp, void *arg,
1728 uint32_t evt)
1729{
a0f9b48d
JS
1730 if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1731 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1732 return ndlp->nlp_state;
1733 }
dea3101e 1734 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
c9f8735b 1735 return NLP_STE_FREED_NODE;
dea3101e
JB
1736}
1737
1738static uint32_t
1739lpfc_device_recov_npr_node(struct lpfc_hba * phba,
1740 struct lpfc_nodelist * ndlp, void *arg,
1741 uint32_t evt)
1742{
1743 spin_lock_irq(phba->host->host_lock);
a0f9b48d 1744 ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
fdcebe28 1745 spin_unlock_irq(phba->host->host_lock);
6ad42535 1746 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
fdcebe28 1747 lpfc_cancel_retry_delay_tmo(phba, ndlp);
6ad42535 1748 }
c9f8735b 1749 return ndlp->nlp_state;
dea3101e
JB
1750}
1751
1752
1753/* This next section defines the NPort Discovery State Machine */
1754
1755/* There are 4 different double linked lists nodelist entries can reside on.
1756 * The plogi list and adisc list are used when Link Up discovery or RSCN
1757 * processing is needed. Each list holds the nodes that we will send PLOGI
1758 * or ADISC on. These lists will keep track of what nodes will be effected
1759 * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
1760 * The unmapped_list will contain all nodes that we have successfully logged
1761 * into at the Fibre Channel level. The mapped_list will contain all nodes
1762 * that are mapped FCP targets.
1763 */
1764/*
1765 * The bind list is a list of undiscovered (potentially non-existent) nodes
1766 * that we have saved binding information on. This information is used when
1767 * nodes transition from the unmapped to the mapped list.
1768 */
1769/* For UNUSED_NODE state, the node has just been allocated .
1770 * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
1771 * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
1772 * and put on the unmapped list. For ADISC processing, the node is taken off
1773 * the ADISC list and placed on either the mapped or unmapped list (depending
1774 * on its previous state). Once on the unmapped list, a PRLI is issued and the
1775 * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
1776 * changed to UNMAPPED_NODE. If the completion indicates a mapped
1777 * node, the node is taken off the unmapped list. The binding list is checked
1778 * for a valid binding, or a binding is automatically assigned. If binding
1779 * assignment is unsuccessful, the node is left on the unmapped list. If
1780 * binding assignment is successful, the associated binding list entry (if
1781 * any) is removed, and the node is placed on the mapped list.
1782 */
1783/*
1784 * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
1785 * lists will receive a DEVICE_RECOVERY event. If the linkdown or nodev timers
1786 * expire, all effected nodes will receive a DEVICE_RM event.
1787 */
1788/*
1789 * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
1790 * to either the ADISC or PLOGI list. After a Nameserver query or ALPA loopmap
1791 * check, additional nodes may be added or removed (via DEVICE_RM) to / from
1792 * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
1793 * we will first process the ADISC list. 32 entries are processed initially and
1794 * ADISC is initited for each one. Completions / Events for each node are
1795 * funnelled thru the state machine. As each node finishes ADISC processing, it
1796 * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
1797 * waiting, and the ADISC list count is identically 0, then we are done. For
1798 * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
1799 * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
1800 * list. 32 entries are processed initially and PLOGI is initited for each one.
1801 * Completions / Events for each node are funnelled thru the state machine. As
1802 * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
1803 * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
1804 * indentically 0, then we are done. We have now completed discovery / RSCN
1805 * handling. Upon completion, ALL nodes should be on either the mapped or
1806 * unmapped lists.
1807 */
1808
1809static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
1810 (struct lpfc_hba *, struct lpfc_nodelist *, void *, uint32_t) = {
1811 /* Action routine Event Current State */
1812 lpfc_rcv_plogi_unused_node, /* RCV_PLOGI UNUSED_NODE */
1813 lpfc_rcv_els_unused_node, /* RCV_PRLI */
1814 lpfc_rcv_logo_unused_node, /* RCV_LOGO */
1815 lpfc_rcv_els_unused_node, /* RCV_ADISC */
1816 lpfc_rcv_els_unused_node, /* RCV_PDISC */
1817 lpfc_rcv_els_unused_node, /* RCV_PRLO */
1818 lpfc_disc_illegal, /* CMPL_PLOGI */
1819 lpfc_disc_illegal, /* CMPL_PRLI */
1820 lpfc_cmpl_logo_unused_node, /* CMPL_LOGO */
1821 lpfc_disc_illegal, /* CMPL_ADISC */
1822 lpfc_disc_illegal, /* CMPL_REG_LOGIN */
1823 lpfc_device_rm_unused_node, /* DEVICE_RM */
1824 lpfc_disc_illegal, /* DEVICE_RECOVERY */
1825
1826 lpfc_rcv_plogi_plogi_issue, /* RCV_PLOGI PLOGI_ISSUE */
1827 lpfc_rcv_els_plogi_issue, /* RCV_PRLI */
c9f8735b 1828 lpfc_rcv_logo_plogi_issue, /* RCV_LOGO */
dea3101e
JB
1829 lpfc_rcv_els_plogi_issue, /* RCV_ADISC */
1830 lpfc_rcv_els_plogi_issue, /* RCV_PDISC */
1831 lpfc_rcv_els_plogi_issue, /* RCV_PRLO */
1832 lpfc_cmpl_plogi_plogi_issue, /* CMPL_PLOGI */
1833 lpfc_disc_illegal, /* CMPL_PRLI */
1834 lpfc_disc_illegal, /* CMPL_LOGO */
1835 lpfc_disc_illegal, /* CMPL_ADISC */
1836 lpfc_disc_illegal, /* CMPL_REG_LOGIN */
1837 lpfc_device_rm_plogi_issue, /* DEVICE_RM */
1838 lpfc_device_recov_plogi_issue, /* DEVICE_RECOVERY */
1839
1840 lpfc_rcv_plogi_adisc_issue, /* RCV_PLOGI ADISC_ISSUE */
1841 lpfc_rcv_prli_adisc_issue, /* RCV_PRLI */
1842 lpfc_rcv_logo_adisc_issue, /* RCV_LOGO */
1843 lpfc_rcv_padisc_adisc_issue, /* RCV_ADISC */
1844 lpfc_rcv_padisc_adisc_issue, /* RCV_PDISC */
1845 lpfc_rcv_prlo_adisc_issue, /* RCV_PRLO */
1846 lpfc_disc_illegal, /* CMPL_PLOGI */
1847 lpfc_disc_illegal, /* CMPL_PRLI */
1848 lpfc_disc_illegal, /* CMPL_LOGO */
1849 lpfc_cmpl_adisc_adisc_issue, /* CMPL_ADISC */
1850 lpfc_disc_illegal, /* CMPL_REG_LOGIN */
1851 lpfc_device_rm_adisc_issue, /* DEVICE_RM */
1852 lpfc_device_recov_adisc_issue, /* DEVICE_RECOVERY */
1853
1854 lpfc_rcv_plogi_reglogin_issue, /* RCV_PLOGI REG_LOGIN_ISSUE */
1855 lpfc_rcv_prli_reglogin_issue, /* RCV_PLOGI */
1856 lpfc_rcv_logo_reglogin_issue, /* RCV_LOGO */
1857 lpfc_rcv_padisc_reglogin_issue, /* RCV_ADISC */
1858 lpfc_rcv_padisc_reglogin_issue, /* RCV_PDISC */
1859 lpfc_rcv_prlo_reglogin_issue, /* RCV_PRLO */
1860 lpfc_disc_illegal, /* CMPL_PLOGI */
1861 lpfc_disc_illegal, /* CMPL_PRLI */
1862 lpfc_disc_illegal, /* CMPL_LOGO */
1863 lpfc_disc_illegal, /* CMPL_ADISC */
1864 lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN */
1865 lpfc_device_rm_reglogin_issue, /* DEVICE_RM */
1866 lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */
1867
1868 lpfc_rcv_plogi_prli_issue, /* RCV_PLOGI PRLI_ISSUE */
1869 lpfc_rcv_prli_prli_issue, /* RCV_PRLI */
1870 lpfc_rcv_logo_prli_issue, /* RCV_LOGO */
1871 lpfc_rcv_padisc_prli_issue, /* RCV_ADISC */
1872 lpfc_rcv_padisc_prli_issue, /* RCV_PDISC */
1873 lpfc_rcv_prlo_prli_issue, /* RCV_PRLO */
1874 lpfc_disc_illegal, /* CMPL_PLOGI */
1875 lpfc_cmpl_prli_prli_issue, /* CMPL_PRLI */
1876 lpfc_disc_illegal, /* CMPL_LOGO */
1877 lpfc_disc_illegal, /* CMPL_ADISC */
1878 lpfc_disc_illegal, /* CMPL_REG_LOGIN */
1879 lpfc_device_rm_prli_issue, /* DEVICE_RM */
1880 lpfc_device_recov_prli_issue, /* DEVICE_RECOVERY */
1881
1882 lpfc_rcv_plogi_unmap_node, /* RCV_PLOGI UNMAPPED_NODE */
1883 lpfc_rcv_prli_unmap_node, /* RCV_PRLI */
1884 lpfc_rcv_logo_unmap_node, /* RCV_LOGO */
1885 lpfc_rcv_padisc_unmap_node, /* RCV_ADISC */
1886 lpfc_rcv_padisc_unmap_node, /* RCV_PDISC */
1887 lpfc_rcv_prlo_unmap_node, /* RCV_PRLO */
1888 lpfc_disc_illegal, /* CMPL_PLOGI */
1889 lpfc_disc_illegal, /* CMPL_PRLI */
1890 lpfc_disc_illegal, /* CMPL_LOGO */
1891 lpfc_disc_illegal, /* CMPL_ADISC */
1892 lpfc_disc_illegal, /* CMPL_REG_LOGIN */
1893 lpfc_disc_illegal, /* DEVICE_RM */
1894 lpfc_device_recov_unmap_node, /* DEVICE_RECOVERY */
1895
1896 lpfc_rcv_plogi_mapped_node, /* RCV_PLOGI MAPPED_NODE */
1897 lpfc_rcv_prli_mapped_node, /* RCV_PRLI */
1898 lpfc_rcv_logo_mapped_node, /* RCV_LOGO */
1899 lpfc_rcv_padisc_mapped_node, /* RCV_ADISC */
1900 lpfc_rcv_padisc_mapped_node, /* RCV_PDISC */
1901 lpfc_rcv_prlo_mapped_node, /* RCV_PRLO */
1902 lpfc_disc_illegal, /* CMPL_PLOGI */
1903 lpfc_disc_illegal, /* CMPL_PRLI */
1904 lpfc_disc_illegal, /* CMPL_LOGO */
1905 lpfc_disc_illegal, /* CMPL_ADISC */
1906 lpfc_disc_illegal, /* CMPL_REG_LOGIN */
1907 lpfc_disc_illegal, /* DEVICE_RM */
1908 lpfc_device_recov_mapped_node, /* DEVICE_RECOVERY */
1909
1910 lpfc_rcv_plogi_npr_node, /* RCV_PLOGI NPR_NODE */
1911 lpfc_rcv_prli_npr_node, /* RCV_PRLI */
1912 lpfc_rcv_logo_npr_node, /* RCV_LOGO */
1913 lpfc_rcv_padisc_npr_node, /* RCV_ADISC */
1914 lpfc_rcv_padisc_npr_node, /* RCV_PDISC */
1915 lpfc_rcv_prlo_npr_node, /* RCV_PRLO */
c9f8735b
JW
1916 lpfc_cmpl_plogi_npr_node, /* CMPL_PLOGI */
1917 lpfc_cmpl_prli_npr_node, /* CMPL_PRLI */
dea3101e 1918 lpfc_cmpl_logo_npr_node, /* CMPL_LOGO */
c9f8735b 1919 lpfc_cmpl_adisc_npr_node, /* CMPL_ADISC */
dea3101e
JB
1920 lpfc_cmpl_reglogin_npr_node, /* CMPL_REG_LOGIN */
1921 lpfc_device_rm_npr_node, /* DEVICE_RM */
1922 lpfc_device_recov_npr_node, /* DEVICE_RECOVERY */
1923};
1924
1925int
1926lpfc_disc_state_machine(struct lpfc_hba * phba,
1927 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1928{
1929 uint32_t cur_state, rc;
1930 uint32_t(*func) (struct lpfc_hba *, struct lpfc_nodelist *, void *,
1931 uint32_t);
1932
1933 ndlp->nlp_disc_refcnt++;
1934 cur_state = ndlp->nlp_state;
1935
1936 /* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
1937 lpfc_printf_log(phba,
1938 KERN_INFO,
1939 LOG_DISCOVERY,
1940 "%d:0211 DSM in event x%x on NPort x%x in state %d "
1941 "Data: x%x\n",
1942 phba->brd_no,
1943 evt, ndlp->nlp_DID, cur_state, ndlp->nlp_flag);
1944
1945 func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
1946 rc = (func) (phba, ndlp, arg, evt);
1947
1948 /* DSM out state <rc> on NPort <nlp_DID> */
1949 lpfc_printf_log(phba,
1950 KERN_INFO,
1951 LOG_DISCOVERY,
1952 "%d:0212 DSM out state %d on NPort x%x Data: x%x\n",
1953 phba->brd_no,
1954 rc, ndlp->nlp_DID, ndlp->nlp_flag);
1955
1956 ndlp->nlp_disc_refcnt--;
1957
1958 /* Check to see if ndlp removal is deferred */
1959 if ((ndlp->nlp_disc_refcnt == 0)
1960 && (ndlp->nlp_flag & NLP_DELAY_REMOVE)) {
1961 spin_lock_irq(phba->host->host_lock);
1962 ndlp->nlp_flag &= ~NLP_DELAY_REMOVE;
1963 spin_unlock_irq(phba->host->host_lock);
1964 lpfc_nlp_remove(phba, ndlp);
c9f8735b 1965 return NLP_STE_FREED_NODE;
dea3101e
JB
1966 }
1967 if (rc == NLP_STE_FREED_NODE)
c9f8735b 1968 return NLP_STE_FREED_NODE;
c9f8735b 1969 return rc;
dea3101e 1970}