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