[SCSI] lpfc 8.1.2: Fixed a race condition in the PLOGI retry logic.
[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                 /* Only try to re-login if this is NOT a Fabric Node */
494                 ndlp->nlp_last_elscmd = (unsigned long)ELS_CMD_PLOGI;
495                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
496                 spin_lock_irq(phba->host->host_lock);
497                 ndlp->nlp_flag |= NLP_DELAY_TMO;
498                 spin_unlock_irq(phba->host->host_lock);
499         }
500
501         ndlp->nlp_state = NLP_STE_NPR_NODE;
502         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
503
504         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
505         /* The driver has to wait until the ACC completes before it continues
506          * processing the LOGO.  The action will resume in
507          * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
508          * unreg_login, the driver waits so the ACC does not get aborted.
509          */
510         return (0);
511 }
512
513 static void
514 lpfc_rcv_prli(struct lpfc_hba * phba,
515                       struct lpfc_nodelist * ndlp,
516                       struct lpfc_iocbq *cmdiocb)
517 {
518         struct lpfc_dmabuf *pcmd;
519         uint32_t *lp;
520         PRLI *npr;
521         struct fc_rport *rport = ndlp->rport;
522         u32 roles;
523
524         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
525         lp = (uint32_t *) pcmd->virt;
526         npr = (PRLI *) ((uint8_t *) lp + sizeof (uint32_t));
527
528         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
529         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
530         if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
531             (npr->prliType == PRLI_FCP_TYPE)) {
532                 if (npr->initiatorFunc)
533                         ndlp->nlp_type |= NLP_FCP_INITIATOR;
534                 if (npr->targetFunc)
535                         ndlp->nlp_type |= NLP_FCP_TARGET;
536                 if (npr->Retry)
537                         ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
538         }
539         if (rport) {
540                 /* We need to update the rport role values */
541                 roles = FC_RPORT_ROLE_UNKNOWN;
542                 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
543                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
544                 if (ndlp->nlp_type & NLP_FCP_TARGET)
545                         roles |= FC_RPORT_ROLE_FCP_TARGET;
546                 fc_remote_port_rolechg(rport, roles);
547         }
548 }
549
550 static uint32_t
551 lpfc_disc_set_adisc(struct lpfc_hba * phba,
552                       struct lpfc_nodelist * ndlp)
553 {
554         /* Check config parameter use-adisc or FCP-2 */
555         if ((phba->cfg_use_adisc == 0) &&
556                 !(phba->fc_flag & FC_RSCN_MODE)) {
557                 if (!(ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
558                         return (0);
559         }
560         spin_lock_irq(phba->host->host_lock);
561         ndlp->nlp_flag |= NLP_NPR_ADISC;
562         spin_unlock_irq(phba->host->host_lock);
563         return (1);
564 }
565
566 static uint32_t
567 lpfc_disc_noop(struct lpfc_hba * phba,
568                 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
569 {
570         /* This routine does nothing, just return the current state */
571         return (ndlp->nlp_state);
572 }
573
574 static uint32_t
575 lpfc_disc_illegal(struct lpfc_hba * phba,
576                    struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
577 {
578         lpfc_printf_log(phba,
579                         KERN_ERR,
580                         LOG_DISCOVERY,
581                         "%d:0253 Illegal State Transition: node x%x event x%x, "
582                         "state x%x Data: x%x x%x\n",
583                         phba->brd_no,
584                         ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
585                         ndlp->nlp_flag);
586         return (ndlp->nlp_state);
587 }
588
589 /* Start of Discovery State Machine routines */
590
591 static uint32_t
592 lpfc_rcv_plogi_unused_node(struct lpfc_hba * phba,
593                            struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
594 {
595         struct lpfc_iocbq *cmdiocb;
596
597         cmdiocb = (struct lpfc_iocbq *) arg;
598
599         if (lpfc_rcv_plogi(phba, ndlp, cmdiocb)) {
600                 ndlp->nlp_state = NLP_STE_UNUSED_NODE;
601                 lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
602                 return (ndlp->nlp_state);
603         }
604         lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
605         return (NLP_STE_FREED_NODE);
606 }
607
608 static uint32_t
609 lpfc_rcv_els_unused_node(struct lpfc_hba * phba,
610                          struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
611 {
612         lpfc_issue_els_logo(phba, ndlp, 0);
613         lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
614         return (ndlp->nlp_state);
615 }
616
617 static uint32_t
618 lpfc_rcv_logo_unused_node(struct lpfc_hba * phba,
619                           struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
620 {
621         struct lpfc_iocbq     *cmdiocb;
622
623         cmdiocb = (struct lpfc_iocbq *) arg;
624
625         spin_lock_irq(phba->host->host_lock);
626         ndlp->nlp_flag |= NLP_LOGO_ACC;
627         spin_unlock_irq(phba->host->host_lock);
628         lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
629         lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
630
631         return (ndlp->nlp_state);
632 }
633
634 static uint32_t
635 lpfc_cmpl_logo_unused_node(struct lpfc_hba * phba,
636                           struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
637 {
638         lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
639         return (NLP_STE_FREED_NODE);
640 }
641
642 static uint32_t
643 lpfc_device_rm_unused_node(struct lpfc_hba * phba,
644                            struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
645 {
646         lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
647         return (NLP_STE_FREED_NODE);
648 }
649
650 static uint32_t
651 lpfc_rcv_plogi_plogi_issue(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
652                            void *arg, uint32_t evt)
653 {
654         struct lpfc_iocbq *cmdiocb = arg;
655         struct lpfc_dmabuf *pcmd;
656         struct serv_parm *sp;
657         uint32_t *lp;
658         struct ls_rjt stat;
659         int port_cmp;
660
661         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
662         lp = (uint32_t *) pcmd->virt;
663         sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
664
665         memset(&stat, 0, sizeof (struct ls_rjt));
666
667         /* For a PLOGI, we only accept if our portname is less
668          * than the remote portname.
669          */
670         phba->fc_stat.elsLogiCol++;
671         port_cmp = memcmp(&phba->fc_portname, &sp->portName,
672                           sizeof (struct lpfc_name));
673
674         if (port_cmp >= 0) {
675                 /* Reject this request because the remote node will accept
676                    ours */
677                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
678                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
679                 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
680         }
681         else {
682                 lpfc_rcv_plogi(phba, ndlp, cmdiocb);
683         } /* if our portname was less */
684
685         return (ndlp->nlp_state);
686 }
687
688 static uint32_t
689 lpfc_rcv_els_plogi_issue(struct lpfc_hba * phba,
690                           struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
691 {
692         struct lpfc_iocbq     *cmdiocb;
693
694         cmdiocb = (struct lpfc_iocbq *) arg;
695
696         /* software abort outstanding PLOGI */
697         lpfc_els_abort(phba, ndlp, 1);
698         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
699         spin_lock_irq(phba->host->host_lock);
700         ndlp->nlp_flag |= NLP_DELAY_TMO;
701         spin_unlock_irq(phba->host->host_lock);
702
703         if (evt == NLP_EVT_RCV_LOGO) {
704                 lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
705         }
706         else {
707                 lpfc_issue_els_logo(phba, ndlp, 0);
708         }
709
710         /* Put ndlp in npr list set plogi timer for 1 sec */
711         ndlp->nlp_last_elscmd = (unsigned long)ELS_CMD_PLOGI;
712         ndlp->nlp_state = NLP_STE_NPR_NODE;
713         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
714
715         return (ndlp->nlp_state);
716 }
717
718 static uint32_t
719 lpfc_cmpl_plogi_plogi_issue(struct lpfc_hba * phba,
720                             struct lpfc_nodelist * ndlp, void *arg,
721                             uint32_t evt)
722 {
723         struct lpfc_iocbq *cmdiocb, *rspiocb;
724         struct lpfc_dmabuf *pcmd, *prsp;
725         uint32_t *lp;
726         IOCB_t *irsp;
727         struct serv_parm *sp;
728         LPFC_MBOXQ_t *mbox;
729
730         cmdiocb = (struct lpfc_iocbq *) arg;
731         rspiocb = cmdiocb->context_un.rsp_iocb;
732
733         if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
734                 return (ndlp->nlp_state);
735         }
736
737         irsp = &rspiocb->iocb;
738
739         if (irsp->ulpStatus)
740                 goto out;
741
742         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
743
744         prsp = list_get_first(&pcmd->list,
745                               struct lpfc_dmabuf,
746                               list);
747         lp = (uint32_t *) prsp->virt;
748
749         sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
750         if (!lpfc_check_sparm(phba, ndlp, sp, CLASS3))
751                 goto out;
752
753         /* PLOGI chkparm OK */
754         lpfc_printf_log(phba,
755                         KERN_INFO,
756                         LOG_ELS,
757                         "%d:0121 PLOGI chkparm OK "
758                         "Data: x%x x%x x%x x%x\n",
759                         phba->brd_no,
760                         ndlp->nlp_DID, ndlp->nlp_state,
761                         ndlp->nlp_flag, ndlp->nlp_rpi);
762
763         if ((phba->cfg_fcp_class == 2) &&
764             (sp->cls2.classValid)) {
765                 ndlp->nlp_fcp_info |= CLASS2;
766         } else {
767                 ndlp->nlp_fcp_info |= CLASS3;
768         }
769         ndlp->nlp_class_sup = 0;
770         if (sp->cls1.classValid)
771                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
772         if (sp->cls2.classValid)
773                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
774         if (sp->cls3.classValid)
775                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
776         if (sp->cls4.classValid)
777                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
778         ndlp->nlp_maxframe =
779                 ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
780                 sp->cmn.bbRcvSizeLsb;
781
782         if (!(mbox = mempool_alloc(phba->mbox_mem_pool,
783                                    GFP_KERNEL)))
784                 goto out;
785
786         lpfc_unreg_rpi(phba, ndlp);
787         if (lpfc_reg_login
788             (phba, irsp->un.elsreq64.remoteID,
789              (uint8_t *) sp, mbox, 0) == 0) {
790                 /* set_slim mailbox command needs to
791                  * execute first, queue this command to
792                  * be processed later.
793                  */
794                 switch(ndlp->nlp_DID) {
795                 case NameServer_DID:
796                         mbox->mbox_cmpl =
797                                 lpfc_mbx_cmpl_ns_reg_login;
798                         break;
799                 case FDMI_DID:
800                         mbox->mbox_cmpl =
801                                 lpfc_mbx_cmpl_fdmi_reg_login;
802                         break;
803                 default:
804                         mbox->mbox_cmpl =
805                                 lpfc_mbx_cmpl_reg_login;
806                 }
807                 mbox->context2 = ndlp;
808                 if (lpfc_sli_issue_mbox(phba, mbox,
809                                         (MBX_NOWAIT | MBX_STOP_IOCB))
810                     != MBX_NOT_FINISHED) {
811                         ndlp->nlp_state =
812                                 NLP_STE_REG_LOGIN_ISSUE;
813                         lpfc_nlp_list(phba, ndlp,
814                                       NLP_REGLOGIN_LIST);
815                         return (ndlp->nlp_state);
816                 }
817                 mempool_free(mbox, phba->mbox_mem_pool);
818         } else {
819                 mempool_free(mbox, phba->mbox_mem_pool);
820         }
821
822
823  out:
824         /* Free this node since the driver cannot login or has the wrong
825            sparm */
826         lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
827         return (NLP_STE_FREED_NODE);
828 }
829
830 static uint32_t
831 lpfc_device_rm_plogi_issue(struct lpfc_hba * phba,
832                            struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
833 {
834         /* software abort outstanding PLOGI */
835         lpfc_els_abort(phba, ndlp, 1);
836
837         lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
838         return (NLP_STE_FREED_NODE);
839 }
840
841 static uint32_t
842 lpfc_device_recov_plogi_issue(struct lpfc_hba * phba,
843                             struct lpfc_nodelist * ndlp, void *arg,
844                             uint32_t evt)
845 {
846         /* software abort outstanding PLOGI */
847         lpfc_els_abort(phba, ndlp, 1);
848
849         ndlp->nlp_state = NLP_STE_NPR_NODE;
850         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
851         spin_lock_irq(phba->host->host_lock);
852         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
853         spin_unlock_irq(phba->host->host_lock);
854
855         return (ndlp->nlp_state);
856 }
857
858 static uint32_t
859 lpfc_rcv_plogi_adisc_issue(struct lpfc_hba * phba,
860                             struct lpfc_nodelist * ndlp, void *arg,
861                             uint32_t evt)
862 {
863         struct lpfc_iocbq *cmdiocb;
864
865         /* software abort outstanding ADISC */
866         lpfc_els_abort(phba, ndlp, 1);
867
868         cmdiocb = (struct lpfc_iocbq *) arg;
869
870         if (lpfc_rcv_plogi(phba, ndlp, cmdiocb)) {
871                 return (ndlp->nlp_state);
872         }
873         ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
874         lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
875         lpfc_issue_els_plogi(phba, ndlp, 0);
876
877         return (ndlp->nlp_state);
878 }
879
880 static uint32_t
881 lpfc_rcv_prli_adisc_issue(struct lpfc_hba * phba,
882                             struct lpfc_nodelist * ndlp, void *arg,
883                             uint32_t evt)
884 {
885         struct lpfc_iocbq *cmdiocb;
886
887         cmdiocb = (struct lpfc_iocbq *) arg;
888
889         lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
890         return (ndlp->nlp_state);
891 }
892
893 static uint32_t
894 lpfc_rcv_logo_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         /* software abort outstanding ADISC */
903         lpfc_els_abort(phba, ndlp, 0);
904
905         lpfc_rcv_logo(phba, ndlp, cmdiocb);
906         return (ndlp->nlp_state);
907 }
908
909 static uint32_t
910 lpfc_rcv_padisc_adisc_issue(struct lpfc_hba * phba,
911                             struct lpfc_nodelist * ndlp, void *arg,
912                             uint32_t evt)
913 {
914         struct lpfc_iocbq *cmdiocb;
915
916         cmdiocb = (struct lpfc_iocbq *) arg;
917
918         lpfc_rcv_padisc(phba, ndlp, cmdiocb);
919         return (ndlp->nlp_state);
920 }
921
922 static uint32_t
923 lpfc_rcv_prlo_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         /* Treat like rcv logo */
932         lpfc_rcv_logo(phba, ndlp, cmdiocb);
933         return (ndlp->nlp_state);
934 }
935
936 static uint32_t
937 lpfc_cmpl_adisc_adisc_issue(struct lpfc_hba * phba,
938                             struct lpfc_nodelist * ndlp, void *arg,
939                             uint32_t evt)
940 {
941         struct lpfc_iocbq *cmdiocb, *rspiocb;
942         IOCB_t *irsp;
943         ADISC *ap;
944
945         cmdiocb = (struct lpfc_iocbq *) arg;
946         rspiocb = cmdiocb->context_un.rsp_iocb;
947
948         ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
949         irsp = &rspiocb->iocb;
950
951         if ((irsp->ulpStatus) ||
952                 (!lpfc_check_adisc(phba, ndlp, &ap->nodeName, &ap->portName))) {
953                 ndlp->nlp_last_elscmd = (unsigned long)ELS_CMD_PLOGI;
954                 /* 1 sec timeout */
955                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
956                 spin_lock_irq(phba->host->host_lock);
957                 ndlp->nlp_flag |= NLP_DELAY_TMO;
958                 spin_unlock_irq(phba->host->host_lock);
959
960                 memset(&ndlp->nlp_nodename, 0, sizeof (struct lpfc_name));
961                 memset(&ndlp->nlp_portname, 0, sizeof (struct lpfc_name));
962
963                 ndlp->nlp_state = NLP_STE_NPR_NODE;
964                 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
965                 lpfc_unreg_rpi(phba, ndlp);
966                 return (ndlp->nlp_state);
967         }
968         if (ndlp->nlp_type & NLP_FCP_TARGET) {
969                 ndlp->nlp_state = NLP_STE_MAPPED_NODE;
970                 lpfc_nlp_list(phba, ndlp, NLP_MAPPED_LIST);
971         } else {
972                 ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
973                 lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
974         }
975         return (ndlp->nlp_state);
976 }
977
978 static uint32_t
979 lpfc_device_rm_adisc_issue(struct lpfc_hba * phba,
980                             struct lpfc_nodelist * ndlp, void *arg,
981                             uint32_t evt)
982 {
983         /* software abort outstanding ADISC */
984         lpfc_els_abort(phba, ndlp, 1);
985
986         lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
987         return (NLP_STE_FREED_NODE);
988 }
989
990 static uint32_t
991 lpfc_device_recov_adisc_issue(struct lpfc_hba * phba,
992                             struct lpfc_nodelist * ndlp, void *arg,
993                             uint32_t evt)
994 {
995         /* software abort outstanding ADISC */
996         lpfc_els_abort(phba, ndlp, 1);
997
998         ndlp->nlp_state = NLP_STE_NPR_NODE;
999         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1000         spin_lock_irq(phba->host->host_lock);
1001         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1002         spin_unlock_irq(phba->host->host_lock);
1003
1004         lpfc_disc_set_adisc(phba, ndlp);
1005         return (ndlp->nlp_state);
1006 }
1007
1008 static uint32_t
1009 lpfc_rcv_plogi_reglogin_issue(struct lpfc_hba * phba,
1010                               struct lpfc_nodelist * ndlp, void *arg,
1011                               uint32_t evt)
1012 {
1013         struct lpfc_iocbq *cmdiocb;
1014
1015         cmdiocb = (struct lpfc_iocbq *) arg;
1016
1017         lpfc_rcv_plogi(phba, ndlp, cmdiocb);
1018         return (ndlp->nlp_state);
1019 }
1020
1021 static uint32_t
1022 lpfc_rcv_prli_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_els_rsp_prli_acc(phba, cmdiocb, ndlp);
1031         return (ndlp->nlp_state);
1032 }
1033
1034 static uint32_t
1035 lpfc_rcv_logo_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_rcv_logo(phba, ndlp, cmdiocb);
1044         return (ndlp->nlp_state);
1045 }
1046
1047 static uint32_t
1048 lpfc_rcv_padisc_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_padisc(phba, ndlp, cmdiocb);
1057         return (ndlp->nlp_state);
1058 }
1059
1060 static uint32_t
1061 lpfc_rcv_prlo_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         lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
1069         return (ndlp->nlp_state);
1070 }
1071
1072 static uint32_t
1073 lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_hba * phba,
1074                                   struct lpfc_nodelist * ndlp,
1075                                   void *arg, uint32_t evt)
1076 {
1077         LPFC_MBOXQ_t *pmb;
1078         MAILBOX_t *mb;
1079         uint32_t did;
1080
1081         pmb = (LPFC_MBOXQ_t *) arg;
1082         mb = &pmb->mb;
1083         did = mb->un.varWords[1];
1084         if (mb->mbxStatus) {
1085                 /* RegLogin failed */
1086                 lpfc_printf_log(phba,
1087                                 KERN_ERR,
1088                                 LOG_DISCOVERY,
1089                                 "%d:0246 RegLogin failed Data: x%x x%x x%x\n",
1090                                 phba->brd_no,
1091                                 did, mb->mbxStatus, phba->hba_state);
1092
1093                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
1094                 spin_lock_irq(phba->host->host_lock);
1095                 ndlp->nlp_flag |= NLP_DELAY_TMO;
1096                 spin_unlock_irq(phba->host->host_lock);
1097
1098                 lpfc_issue_els_logo(phba, ndlp, 0);
1099                 /* Put ndlp in npr list set plogi timer for 1 sec */
1100                 ndlp->nlp_last_elscmd = (unsigned long)ELS_CMD_PLOGI;
1101                 ndlp->nlp_state = NLP_STE_NPR_NODE;
1102                 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1103                 return (ndlp->nlp_state);
1104         }
1105
1106         ndlp->nlp_rpi = mb->un.varWords[0];
1107
1108         /* Only if we are not a fabric nport do we issue PRLI */
1109         if (!(ndlp->nlp_type & NLP_FABRIC)) {
1110                 ndlp->nlp_state = NLP_STE_PRLI_ISSUE;
1111                 lpfc_nlp_list(phba, ndlp, NLP_PRLI_LIST);
1112                 lpfc_issue_els_prli(phba, ndlp, 0);
1113         } else {
1114                 ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
1115                 lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
1116         }
1117         return (ndlp->nlp_state);
1118 }
1119
1120 static uint32_t
1121 lpfc_device_rm_reglogin_issue(struct lpfc_hba * phba,
1122                               struct lpfc_nodelist * ndlp, void *arg,
1123                               uint32_t evt)
1124 {
1125         lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1126         return (NLP_STE_FREED_NODE);
1127 }
1128
1129 static uint32_t
1130 lpfc_device_recov_reglogin_issue(struct lpfc_hba * phba,
1131                                struct lpfc_nodelist * ndlp, void *arg,
1132                                uint32_t evt)
1133 {
1134         ndlp->nlp_state = NLP_STE_NPR_NODE;
1135         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1136         spin_lock_irq(phba->host->host_lock);
1137         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1138         spin_unlock_irq(phba->host->host_lock);
1139         return (ndlp->nlp_state);
1140 }
1141
1142 static uint32_t
1143 lpfc_rcv_plogi_prli_issue(struct lpfc_hba * phba,
1144                           struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1145 {
1146         struct lpfc_iocbq *cmdiocb;
1147
1148         cmdiocb = (struct lpfc_iocbq *) arg;
1149
1150         lpfc_rcv_plogi(phba, ndlp, cmdiocb);
1151         return (ndlp->nlp_state);
1152 }
1153
1154 static uint32_t
1155 lpfc_rcv_prli_prli_issue(struct lpfc_hba * phba,
1156                          struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1157 {
1158         struct lpfc_iocbq *cmdiocb;
1159
1160         cmdiocb = (struct lpfc_iocbq *) arg;
1161
1162         lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
1163         return (ndlp->nlp_state);
1164 }
1165
1166 static uint32_t
1167 lpfc_rcv_logo_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         /* Software abort outstanding PRLI before sending acc */
1175         lpfc_els_abort(phba, ndlp, 1);
1176
1177         lpfc_rcv_logo(phba, ndlp, cmdiocb);
1178         return (ndlp->nlp_state);
1179 }
1180
1181 static uint32_t
1182 lpfc_rcv_padisc_prli_issue(struct lpfc_hba * phba,
1183                            struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1184 {
1185         struct lpfc_iocbq *cmdiocb;
1186
1187         cmdiocb = (struct lpfc_iocbq *) arg;
1188
1189         lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1190         return (ndlp->nlp_state);
1191 }
1192
1193 /* This routine is envoked when we rcv a PRLO request from a nport
1194  * we are logged into.  We should send back a PRLO rsp setting the
1195  * appropriate bits.
1196  * NEXT STATE = PRLI_ISSUE
1197  */
1198 static uint32_t
1199 lpfc_rcv_prlo_prli_issue(struct lpfc_hba * phba,
1200                          struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1201 {
1202         struct lpfc_iocbq *cmdiocb;
1203
1204         cmdiocb = (struct lpfc_iocbq *) arg;
1205         lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
1206         return (ndlp->nlp_state);
1207 }
1208
1209 static uint32_t
1210 lpfc_cmpl_prli_prli_issue(struct lpfc_hba * phba,
1211                           struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1212 {
1213         struct lpfc_iocbq *cmdiocb, *rspiocb;
1214         IOCB_t *irsp;
1215         PRLI *npr;
1216
1217         cmdiocb = (struct lpfc_iocbq *) arg;
1218         rspiocb = cmdiocb->context_un.rsp_iocb;
1219         npr = (PRLI *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1220
1221         irsp = &rspiocb->iocb;
1222         if (irsp->ulpStatus) {
1223                 ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
1224                 lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
1225                 return (ndlp->nlp_state);
1226         }
1227
1228         /* Check out PRLI rsp */
1229         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1230         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
1231         if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
1232             (npr->prliType == PRLI_FCP_TYPE)) {
1233                 if (npr->initiatorFunc)
1234                         ndlp->nlp_type |= NLP_FCP_INITIATOR;
1235                 if (npr->targetFunc)
1236                         ndlp->nlp_type |= NLP_FCP_TARGET;
1237                 if (npr->Retry)
1238                         ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
1239         }
1240
1241         ndlp->nlp_state = NLP_STE_MAPPED_NODE;
1242         lpfc_nlp_list(phba, ndlp, NLP_MAPPED_LIST);
1243         return (ndlp->nlp_state);
1244 }
1245
1246 /*! lpfc_device_rm_prli_issue
1247   *
1248   * \pre
1249   * \post
1250   * \param   phba
1251   * \param   ndlp
1252   * \param   arg
1253   * \param   evt
1254   * \return  uint32_t
1255   *
1256   * \b Description:
1257   *    This routine is envoked when we a request to remove a nport we are in the
1258   *    process of PRLIing. We should software abort outstanding prli, unreg
1259   *    login, send a logout. We will change node state to UNUSED_NODE, put it
1260   *    on plogi list so it can be freed when LOGO completes.
1261   *
1262   */
1263 static uint32_t
1264 lpfc_device_rm_prli_issue(struct lpfc_hba * phba,
1265                           struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1266 {
1267         /* software abort outstanding PRLI */
1268         lpfc_els_abort(phba, ndlp, 1);
1269
1270         lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1271         return (NLP_STE_FREED_NODE);
1272 }
1273
1274
1275 /*! lpfc_device_recov_prli_issue
1276   *
1277   * \pre
1278   * \post
1279   * \param   phba
1280   * \param   ndlp
1281   * \param   arg
1282   * \param   evt
1283   * \return  uint32_t
1284   *
1285   * \b Description:
1286   *    The routine is envoked when the state of a device is unknown, like
1287   *    during a link down. We should remove the nodelist entry from the
1288   *    unmapped list, issue a UNREG_LOGIN, do a software abort of the
1289   *    outstanding PRLI command, then free the node entry.
1290   */
1291 static uint32_t
1292 lpfc_device_recov_prli_issue(struct lpfc_hba * phba,
1293                            struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1294 {
1295         /* software abort outstanding PRLI */
1296         lpfc_els_abort(phba, ndlp, 1);
1297
1298         ndlp->nlp_state = NLP_STE_NPR_NODE;
1299         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1300         spin_lock_irq(phba->host->host_lock);
1301         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1302         spin_unlock_irq(phba->host->host_lock);
1303         return (ndlp->nlp_state);
1304 }
1305
1306 static uint32_t
1307 lpfc_rcv_plogi_unmap_node(struct lpfc_hba * phba,
1308                           struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1309 {
1310         struct lpfc_iocbq *cmdiocb;
1311
1312         cmdiocb = (struct lpfc_iocbq *) arg;
1313
1314         lpfc_rcv_plogi(phba, ndlp, cmdiocb);
1315         return (ndlp->nlp_state);
1316 }
1317
1318 static uint32_t
1319 lpfc_rcv_prli_unmap_node(struct lpfc_hba * phba,
1320                          struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1321 {
1322         struct lpfc_iocbq *cmdiocb;
1323
1324         cmdiocb = (struct lpfc_iocbq *) arg;
1325
1326         lpfc_rcv_prli(phba, ndlp, cmdiocb);
1327         lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
1328         return (ndlp->nlp_state);
1329 }
1330
1331 static uint32_t
1332 lpfc_rcv_logo_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_logo(phba, ndlp, cmdiocb);
1340         return (ndlp->nlp_state);
1341 }
1342
1343 static uint32_t
1344 lpfc_rcv_padisc_unmap_node(struct lpfc_hba * phba,
1345                            struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1346 {
1347         struct lpfc_iocbq *cmdiocb;
1348
1349         cmdiocb = (struct lpfc_iocbq *) arg;
1350
1351         lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1352         return (ndlp->nlp_state);
1353 }
1354
1355 static uint32_t
1356 lpfc_rcv_prlo_unmap_node(struct lpfc_hba * phba,
1357                          struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1358 {
1359         struct lpfc_iocbq *cmdiocb;
1360
1361         cmdiocb = (struct lpfc_iocbq *) arg;
1362
1363         /* Treat like rcv logo */
1364         lpfc_rcv_logo(phba, ndlp, cmdiocb);
1365         return (ndlp->nlp_state);
1366 }
1367
1368 static uint32_t
1369 lpfc_device_recov_unmap_node(struct lpfc_hba * phba,
1370                            struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1371 {
1372         ndlp->nlp_state = NLP_STE_NPR_NODE;
1373         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1374         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1375         lpfc_disc_set_adisc(phba, ndlp);
1376
1377         return (ndlp->nlp_state);
1378 }
1379
1380 static uint32_t
1381 lpfc_rcv_plogi_mapped_node(struct lpfc_hba * phba,
1382                            struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1383 {
1384         struct lpfc_iocbq *cmdiocb;
1385
1386         cmdiocb = (struct lpfc_iocbq *) arg;
1387
1388         lpfc_rcv_plogi(phba, ndlp, cmdiocb);
1389         return (ndlp->nlp_state);
1390 }
1391
1392 static uint32_t
1393 lpfc_rcv_prli_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_els_rsp_prli_acc(phba, cmdiocb, ndlp);
1401         return (ndlp->nlp_state);
1402 }
1403
1404 static uint32_t
1405 lpfc_rcv_logo_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_rcv_logo(phba, ndlp, cmdiocb);
1413         return (ndlp->nlp_state);
1414 }
1415
1416 static uint32_t
1417 lpfc_rcv_padisc_mapped_node(struct lpfc_hba * phba,
1418                             struct lpfc_nodelist * ndlp, void *arg,
1419                             uint32_t evt)
1420 {
1421         struct lpfc_iocbq *cmdiocb;
1422
1423         cmdiocb = (struct lpfc_iocbq *) arg;
1424
1425         lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1426         return (ndlp->nlp_state);
1427 }
1428
1429 static uint32_t
1430 lpfc_rcv_prlo_mapped_node(struct lpfc_hba * phba,
1431                           struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1432 {
1433         struct lpfc_iocbq *cmdiocb;
1434
1435         cmdiocb = (struct lpfc_iocbq *) arg;
1436
1437         /* flush the target */
1438         spin_lock_irq(phba->host->host_lock);
1439         lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1440                                ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
1441         spin_unlock_irq(phba->host->host_lock);
1442
1443         /* Treat like rcv logo */
1444         lpfc_rcv_logo(phba, ndlp, cmdiocb);
1445         return (ndlp->nlp_state);
1446 }
1447
1448 static uint32_t
1449 lpfc_device_recov_mapped_node(struct lpfc_hba * phba,
1450                             struct lpfc_nodelist * ndlp, void *arg,
1451                             uint32_t evt)
1452 {
1453         ndlp->nlp_state = NLP_STE_NPR_NODE;
1454         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1455         spin_lock_irq(phba->host->host_lock);
1456         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1457         spin_unlock_irq(phba->host->host_lock);
1458         lpfc_disc_set_adisc(phba, ndlp);
1459         return (ndlp->nlp_state);
1460 }
1461
1462 static uint32_t
1463 lpfc_rcv_plogi_npr_node(struct lpfc_hba * phba,
1464                             struct lpfc_nodelist * ndlp, void *arg,
1465                             uint32_t evt)
1466 {
1467         struct lpfc_iocbq *cmdiocb;
1468
1469         cmdiocb = (struct lpfc_iocbq *) arg;
1470
1471         /* Ignore PLOGI if we have an outstanding LOGO */
1472         if (ndlp->nlp_flag & NLP_LOGO_SND) {
1473                 return (ndlp->nlp_state);
1474         }
1475
1476         if (lpfc_rcv_plogi(phba, ndlp, cmdiocb)) {
1477                 spin_lock_irq(phba->host->host_lock);
1478                 ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
1479                 spin_unlock_irq(phba->host->host_lock);
1480                 return (ndlp->nlp_state);
1481         }
1482
1483         /* send PLOGI immediately, move to PLOGI issue state */
1484         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1485                         ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1486                         lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1487                         lpfc_issue_els_plogi(phba, ndlp, 0);
1488         }
1489         return (ndlp->nlp_state);
1490 }
1491
1492 static uint32_t
1493 lpfc_rcv_prli_npr_node(struct lpfc_hba * phba,
1494                             struct lpfc_nodelist * ndlp, void *arg,
1495                             uint32_t evt)
1496 {
1497         struct lpfc_iocbq     *cmdiocb;
1498         struct ls_rjt          stat;
1499
1500         cmdiocb = (struct lpfc_iocbq *) arg;
1501
1502         memset(&stat, 0, sizeof (struct ls_rjt));
1503         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1504         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1505         lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
1506
1507         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1508                 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
1509                         ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
1510                         lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
1511                         lpfc_issue_els_adisc(phba, ndlp, 0);
1512                 } else {
1513                         ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1514                         lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1515                         lpfc_issue_els_plogi(phba, ndlp, 0);
1516                 }
1517         }
1518         return (ndlp->nlp_state);
1519 }
1520
1521 static uint32_t
1522 lpfc_rcv_logo_npr_node(struct lpfc_hba * phba,
1523                             struct lpfc_nodelist * ndlp, void *arg,
1524                             uint32_t evt)
1525 {
1526         struct lpfc_iocbq     *cmdiocb;
1527
1528         cmdiocb = (struct lpfc_iocbq *) arg;
1529
1530         lpfc_rcv_logo(phba, ndlp, cmdiocb);
1531         return (ndlp->nlp_state);
1532 }
1533
1534 static uint32_t
1535 lpfc_rcv_padisc_npr_node(struct lpfc_hba * phba,
1536                             struct lpfc_nodelist * ndlp, void *arg,
1537                             uint32_t evt)
1538 {
1539         struct lpfc_iocbq     *cmdiocb;
1540
1541         cmdiocb = (struct lpfc_iocbq *) arg;
1542
1543         lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1544
1545         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1546                 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
1547                         ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
1548                         lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
1549                         lpfc_issue_els_adisc(phba, ndlp, 0);
1550                 } else {
1551                         ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1552                         lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1553                         lpfc_issue_els_plogi(phba, ndlp, 0);
1554                 }
1555         }
1556         return (ndlp->nlp_state);
1557 }
1558
1559 static uint32_t
1560 lpfc_rcv_prlo_npr_node(struct lpfc_hba * phba,
1561                             struct lpfc_nodelist * ndlp, void *arg,
1562                             uint32_t evt)
1563 {
1564         struct lpfc_iocbq     *cmdiocb;
1565
1566         cmdiocb = (struct lpfc_iocbq *) arg;
1567
1568         lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
1569
1570         if (ndlp->nlp_flag & NLP_DELAY_TMO) {
1571                 if (ndlp->nlp_last_elscmd == (unsigned long)ELS_CMD_PLOGI) {
1572                         return (ndlp->nlp_state);
1573                 } else {
1574                         spin_lock_irq(phba->host->host_lock);
1575                         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1576                         spin_unlock_irq(phba->host->host_lock);
1577                         del_timer_sync(&ndlp->nlp_delayfunc);
1578                         if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1579                                 list_del_init(&ndlp->els_retry_evt.evt_listp);
1580                 }
1581         }
1582
1583         ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1584         lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1585         lpfc_issue_els_plogi(phba, ndlp, 0);
1586         return (ndlp->nlp_state);
1587 }
1588
1589 static uint32_t
1590 lpfc_cmpl_logo_npr_node(struct lpfc_hba * phba,
1591                 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1592 {
1593         lpfc_unreg_rpi(phba, ndlp);
1594         /* This routine does nothing, just return the current state */
1595         return (ndlp->nlp_state);
1596 }
1597
1598 static uint32_t
1599 lpfc_cmpl_reglogin_npr_node(struct lpfc_hba * phba,
1600                             struct lpfc_nodelist * ndlp, void *arg,
1601                             uint32_t evt)
1602 {
1603         LPFC_MBOXQ_t *pmb;
1604         MAILBOX_t *mb;
1605
1606         pmb = (LPFC_MBOXQ_t *) arg;
1607         mb = &pmb->mb;
1608
1609         ndlp->nlp_rpi = mb->un.varWords[0];
1610
1611         return (ndlp->nlp_state);
1612 }
1613
1614 static uint32_t
1615 lpfc_device_rm_npr_node(struct lpfc_hba * phba,
1616                             struct lpfc_nodelist * ndlp, void *arg,
1617                             uint32_t evt)
1618 {
1619         lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1620         return (NLP_STE_FREED_NODE);
1621 }
1622
1623 static uint32_t
1624 lpfc_device_recov_npr_node(struct lpfc_hba * phba,
1625                             struct lpfc_nodelist * ndlp, void *arg,
1626                             uint32_t evt)
1627 {
1628         spin_lock_irq(phba->host->host_lock);
1629         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1630         if (ndlp->nlp_flag & NLP_DELAY_TMO) {
1631                 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1632                 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1633                         list_del_init(&ndlp->els_retry_evt.evt_listp);
1634                 spin_unlock_irq(phba->host->host_lock);
1635                 del_timer_sync(&ndlp->nlp_delayfunc);
1636                 return (ndlp->nlp_state);
1637         }
1638         spin_unlock_irq(phba->host->host_lock);
1639         return (ndlp->nlp_state);
1640 }
1641
1642
1643 /* This next section defines the NPort Discovery State Machine */
1644
1645 /* There are 4 different double linked lists nodelist entries can reside on.
1646  * The plogi list and adisc list are used when Link Up discovery or RSCN
1647  * processing is needed. Each list holds the nodes that we will send PLOGI
1648  * or ADISC on. These lists will keep track of what nodes will be effected
1649  * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
1650  * The unmapped_list will contain all nodes that we have successfully logged
1651  * into at the Fibre Channel level. The mapped_list will contain all nodes
1652  * that are mapped FCP targets.
1653  */
1654 /*
1655  * The bind list is a list of undiscovered (potentially non-existent) nodes
1656  * that we have saved binding information on. This information is used when
1657  * nodes transition from the unmapped to the mapped list.
1658  */
1659 /* For UNUSED_NODE state, the node has just been allocated .
1660  * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
1661  * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
1662  * and put on the unmapped list. For ADISC processing, the node is taken off
1663  * the ADISC list and placed on either the mapped or unmapped list (depending
1664  * on its previous state). Once on the unmapped list, a PRLI is issued and the
1665  * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
1666  * changed to UNMAPPED_NODE. If the completion indicates a mapped
1667  * node, the node is taken off the unmapped list. The binding list is checked
1668  * for a valid binding, or a binding is automatically assigned. If binding
1669  * assignment is unsuccessful, the node is left on the unmapped list. If
1670  * binding assignment is successful, the associated binding list entry (if
1671  * any) is removed, and the node is placed on the mapped list.
1672  */
1673 /*
1674  * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
1675  * lists will receive a DEVICE_RECOVERY event. If the linkdown or nodev timers
1676  * expire, all effected nodes will receive a DEVICE_RM event.
1677  */
1678 /*
1679  * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
1680  * to either the ADISC or PLOGI list.  After a Nameserver query or ALPA loopmap
1681  * check, additional nodes may be added or removed (via DEVICE_RM) to / from
1682  * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
1683  * we will first process the ADISC list.  32 entries are processed initially and
1684  * ADISC is initited for each one.  Completions / Events for each node are
1685  * funnelled thru the state machine.  As each node finishes ADISC processing, it
1686  * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
1687  * waiting, and the ADISC list count is identically 0, then we are done. For
1688  * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
1689  * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
1690  * list.  32 entries are processed initially and PLOGI is initited for each one.
1691  * Completions / Events for each node are funnelled thru the state machine.  As
1692  * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
1693  * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
1694  * indentically 0, then we are done. We have now completed discovery / RSCN
1695  * handling. Upon completion, ALL nodes should be on either the mapped or
1696  * unmapped lists.
1697  */
1698
1699 static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
1700      (struct lpfc_hba *, struct lpfc_nodelist *, void *, uint32_t) = {
1701         /* Action routine                  Event       Current State  */
1702         lpfc_rcv_plogi_unused_node,     /* RCV_PLOGI   UNUSED_NODE    */
1703         lpfc_rcv_els_unused_node,       /* RCV_PRLI        */
1704         lpfc_rcv_logo_unused_node,      /* RCV_LOGO        */
1705         lpfc_rcv_els_unused_node,       /* RCV_ADISC       */
1706         lpfc_rcv_els_unused_node,       /* RCV_PDISC       */
1707         lpfc_rcv_els_unused_node,       /* RCV_PRLO        */
1708         lpfc_disc_illegal,              /* CMPL_PLOGI      */
1709         lpfc_disc_illegal,              /* CMPL_PRLI       */
1710         lpfc_cmpl_logo_unused_node,     /* CMPL_LOGO       */
1711         lpfc_disc_illegal,              /* CMPL_ADISC      */
1712         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
1713         lpfc_device_rm_unused_node,     /* DEVICE_RM       */
1714         lpfc_disc_illegal,              /* DEVICE_RECOVERY */
1715
1716         lpfc_rcv_plogi_plogi_issue,     /* RCV_PLOGI   PLOGI_ISSUE    */
1717         lpfc_rcv_els_plogi_issue,       /* RCV_PRLI        */
1718         lpfc_rcv_els_plogi_issue,       /* RCV_LOGO        */
1719         lpfc_rcv_els_plogi_issue,       /* RCV_ADISC       */
1720         lpfc_rcv_els_plogi_issue,       /* RCV_PDISC       */
1721         lpfc_rcv_els_plogi_issue,       /* RCV_PRLO        */
1722         lpfc_cmpl_plogi_plogi_issue,    /* CMPL_PLOGI      */
1723         lpfc_disc_illegal,              /* CMPL_PRLI       */
1724         lpfc_disc_illegal,              /* CMPL_LOGO       */
1725         lpfc_disc_illegal,              /* CMPL_ADISC      */
1726         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
1727         lpfc_device_rm_plogi_issue,     /* DEVICE_RM       */
1728         lpfc_device_recov_plogi_issue,  /* DEVICE_RECOVERY */
1729
1730         lpfc_rcv_plogi_adisc_issue,     /* RCV_PLOGI   ADISC_ISSUE    */
1731         lpfc_rcv_prli_adisc_issue,      /* RCV_PRLI        */
1732         lpfc_rcv_logo_adisc_issue,      /* RCV_LOGO        */
1733         lpfc_rcv_padisc_adisc_issue,    /* RCV_ADISC       */
1734         lpfc_rcv_padisc_adisc_issue,    /* RCV_PDISC       */
1735         lpfc_rcv_prlo_adisc_issue,      /* RCV_PRLO        */
1736         lpfc_disc_illegal,              /* CMPL_PLOGI      */
1737         lpfc_disc_illegal,              /* CMPL_PRLI       */
1738         lpfc_disc_illegal,              /* CMPL_LOGO       */
1739         lpfc_cmpl_adisc_adisc_issue,    /* CMPL_ADISC      */
1740         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
1741         lpfc_device_rm_adisc_issue,     /* DEVICE_RM       */
1742         lpfc_device_recov_adisc_issue,  /* DEVICE_RECOVERY */
1743
1744         lpfc_rcv_plogi_reglogin_issue,  /* RCV_PLOGI  REG_LOGIN_ISSUE */
1745         lpfc_rcv_prli_reglogin_issue,   /* RCV_PLOGI       */
1746         lpfc_rcv_logo_reglogin_issue,   /* RCV_LOGO        */
1747         lpfc_rcv_padisc_reglogin_issue, /* RCV_ADISC       */
1748         lpfc_rcv_padisc_reglogin_issue, /* RCV_PDISC       */
1749         lpfc_rcv_prlo_reglogin_issue,   /* RCV_PRLO        */
1750         lpfc_disc_illegal,              /* CMPL_PLOGI      */
1751         lpfc_disc_illegal,              /* CMPL_PRLI       */
1752         lpfc_disc_illegal,              /* CMPL_LOGO       */
1753         lpfc_disc_illegal,              /* CMPL_ADISC      */
1754         lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN  */
1755         lpfc_device_rm_reglogin_issue,  /* DEVICE_RM       */
1756         lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */
1757
1758         lpfc_rcv_plogi_prli_issue,      /* RCV_PLOGI   PRLI_ISSUE     */
1759         lpfc_rcv_prli_prli_issue,       /* RCV_PRLI        */
1760         lpfc_rcv_logo_prli_issue,       /* RCV_LOGO        */
1761         lpfc_rcv_padisc_prli_issue,     /* RCV_ADISC       */
1762         lpfc_rcv_padisc_prli_issue,     /* RCV_PDISC       */
1763         lpfc_rcv_prlo_prli_issue,       /* RCV_PRLO        */
1764         lpfc_disc_illegal,              /* CMPL_PLOGI      */
1765         lpfc_cmpl_prli_prli_issue,      /* CMPL_PRLI       */
1766         lpfc_disc_illegal,              /* CMPL_LOGO       */
1767         lpfc_disc_illegal,              /* CMPL_ADISC      */
1768         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
1769         lpfc_device_rm_prli_issue,      /* DEVICE_RM       */
1770         lpfc_device_recov_prli_issue,   /* DEVICE_RECOVERY */
1771
1772         lpfc_rcv_plogi_unmap_node,      /* RCV_PLOGI   UNMAPPED_NODE  */
1773         lpfc_rcv_prli_unmap_node,       /* RCV_PRLI        */
1774         lpfc_rcv_logo_unmap_node,       /* RCV_LOGO        */
1775         lpfc_rcv_padisc_unmap_node,     /* RCV_ADISC       */
1776         lpfc_rcv_padisc_unmap_node,     /* RCV_PDISC       */
1777         lpfc_rcv_prlo_unmap_node,       /* RCV_PRLO        */
1778         lpfc_disc_illegal,              /* CMPL_PLOGI      */
1779         lpfc_disc_illegal,              /* CMPL_PRLI       */
1780         lpfc_disc_illegal,              /* CMPL_LOGO       */
1781         lpfc_disc_illegal,              /* CMPL_ADISC      */
1782         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
1783         lpfc_disc_illegal,              /* DEVICE_RM       */
1784         lpfc_device_recov_unmap_node,   /* DEVICE_RECOVERY */
1785
1786         lpfc_rcv_plogi_mapped_node,     /* RCV_PLOGI   MAPPED_NODE    */
1787         lpfc_rcv_prli_mapped_node,      /* RCV_PRLI        */
1788         lpfc_rcv_logo_mapped_node,      /* RCV_LOGO        */
1789         lpfc_rcv_padisc_mapped_node,    /* RCV_ADISC       */
1790         lpfc_rcv_padisc_mapped_node,    /* RCV_PDISC       */
1791         lpfc_rcv_prlo_mapped_node,      /* RCV_PRLO        */
1792         lpfc_disc_illegal,              /* CMPL_PLOGI      */
1793         lpfc_disc_illegal,              /* CMPL_PRLI       */
1794         lpfc_disc_illegal,              /* CMPL_LOGO       */
1795         lpfc_disc_illegal,              /* CMPL_ADISC      */
1796         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
1797         lpfc_disc_illegal,              /* DEVICE_RM       */
1798         lpfc_device_recov_mapped_node,  /* DEVICE_RECOVERY */
1799
1800         lpfc_rcv_plogi_npr_node,        /* RCV_PLOGI   NPR_NODE    */
1801         lpfc_rcv_prli_npr_node,         /* RCV_PRLI        */
1802         lpfc_rcv_logo_npr_node,         /* RCV_LOGO        */
1803         lpfc_rcv_padisc_npr_node,       /* RCV_ADISC       */
1804         lpfc_rcv_padisc_npr_node,       /* RCV_PDISC       */
1805         lpfc_rcv_prlo_npr_node,         /* RCV_PRLO        */
1806         lpfc_disc_noop,                 /* CMPL_PLOGI      */
1807         lpfc_disc_noop,                 /* CMPL_PRLI       */
1808         lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
1809         lpfc_disc_noop,                 /* CMPL_ADISC      */
1810         lpfc_cmpl_reglogin_npr_node,    /* CMPL_REG_LOGIN  */
1811         lpfc_device_rm_npr_node,        /* DEVICE_RM       */
1812         lpfc_device_recov_npr_node,     /* DEVICE_RECOVERY */
1813 };
1814
1815 int
1816 lpfc_disc_state_machine(struct lpfc_hba * phba,
1817                         struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1818 {
1819         uint32_t cur_state, rc;
1820         uint32_t(*func) (struct lpfc_hba *, struct lpfc_nodelist *, void *,
1821                          uint32_t);
1822
1823         ndlp->nlp_disc_refcnt++;
1824         cur_state = ndlp->nlp_state;
1825
1826         /* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
1827         lpfc_printf_log(phba,
1828                         KERN_INFO,
1829                         LOG_DISCOVERY,
1830                         "%d:0211 DSM in event x%x on NPort x%x in state %d "
1831                         "Data: x%x\n",
1832                         phba->brd_no,
1833                         evt, ndlp->nlp_DID, cur_state, ndlp->nlp_flag);
1834
1835         func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
1836         rc = (func) (phba, ndlp, arg, evt);
1837
1838         /* DSM out state <rc> on NPort <nlp_DID> */
1839         lpfc_printf_log(phba,
1840                        KERN_INFO,
1841                        LOG_DISCOVERY,
1842                        "%d:0212 DSM out state %d on NPort x%x Data: x%x\n",
1843                        phba->brd_no,
1844                        rc, ndlp->nlp_DID, ndlp->nlp_flag);
1845
1846         ndlp->nlp_disc_refcnt--;
1847
1848         /* Check to see if ndlp removal is deferred */
1849         if ((ndlp->nlp_disc_refcnt == 0)
1850             && (ndlp->nlp_flag & NLP_DELAY_REMOVE)) {
1851                 spin_lock_irq(phba->host->host_lock);
1852                 ndlp->nlp_flag &= ~NLP_DELAY_REMOVE;
1853                 spin_unlock_irq(phba->host->host_lock);
1854                 lpfc_nlp_remove(phba, ndlp);
1855                 return (NLP_STE_FREED_NODE);
1856         }
1857         if (rc == NLP_STE_FREED_NODE)
1858                 return (NLP_STE_FREED_NODE);
1859         ndlp->nlp_state = rc;
1860         return (rc);
1861 }