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