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