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