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