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