]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/lpfc/lpfc_els.c
d0730e79c1a70da113cf4abb0c1cfd6d42127b32
[mv-sheeva.git] / drivers / scsi / lpfc / lpfc_els.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2008 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 /* See Fibre Channel protocol T11 FC-LS for details */
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 #include "lpfc_vport.h"
39 #include "lpfc_debugfs.h"
40
41 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
42                           struct lpfc_iocbq *);
43 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
44                         struct lpfc_iocbq *);
45 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
46 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
47                                 struct lpfc_nodelist *ndlp, uint8_t retry);
48 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
49                                   struct lpfc_iocbq *iocb);
50 static void lpfc_register_new_vport(struct lpfc_hba *phba,
51                                     struct lpfc_vport *vport,
52                                     struct lpfc_nodelist *ndlp);
53
54 static int lpfc_max_els_tries = 3;
55
56 /**
57  * lpfc_els_chk_latt: Check host link attention event for a vport.
58  * @vport: pointer to a host virtual N_Port data structure.
59  *
60  * This routine checks whether there is an outstanding host link
61  * attention event during the discovery process with the @vport. It is done
62  * by reading the HBA's Host Attention (HA) register. If there is any host
63  * link attention events during this @vport's discovery process, the @vport
64  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
65  * be issued if the link state is not already in host link cleared state,
66  * and a return code shall indicate whether the host link attention event
67  * had happened.
68  *
69  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
70  * state in LPFC_VPORT_READY, the request for checking host link attention
71  * event will be ignored and a return code shall indicate no host link
72  * attention event had happened.
73  *
74  * Return codes
75  *   0 - no host link attention event happened
76  *   1 - host link attention event happened
77  **/
78 int
79 lpfc_els_chk_latt(struct lpfc_vport *vport)
80 {
81         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
82         struct lpfc_hba  *phba = vport->phba;
83         uint32_t ha_copy;
84
85         if (vport->port_state >= LPFC_VPORT_READY ||
86             phba->link_state == LPFC_LINK_DOWN)
87                 return 0;
88
89         /* Read the HBA Host Attention Register */
90         ha_copy = readl(phba->HAregaddr);
91
92         if (!(ha_copy & HA_LATT))
93                 return 0;
94
95         /* Pending Link Event during Discovery */
96         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
97                          "0237 Pending Link Event during "
98                          "Discovery: State x%x\n",
99                          phba->pport->port_state);
100
101         /* CLEAR_LA should re-enable link attention events and
102          * we should then imediately take a LATT event. The
103          * LATT processing should call lpfc_linkdown() which
104          * will cleanup any left over in-progress discovery
105          * events.
106          */
107         spin_lock_irq(shost->host_lock);
108         vport->fc_flag |= FC_ABORT_DISCOVERY;
109         spin_unlock_irq(shost->host_lock);
110
111         if (phba->link_state != LPFC_CLEAR_LA)
112                 lpfc_issue_clear_la(phba, vport);
113
114         return 1;
115 }
116
117 /**
118  * lpfc_prep_els_iocb: Allocate and prepare a lpfc iocb data structure.
119  * @vport: pointer to a host virtual N_Port data structure.
120  * @expectRsp: flag indicating whether response is expected.
121  * @cmdSize: size of the ELS command.
122  * @retry: number of retries to the command IOCB when it fails.
123  * @ndlp: pointer to a node-list data structure.
124  * @did: destination identifier.
125  * @elscmd: the ELS command code.
126  *
127  * This routine is used for allocating a lpfc-IOCB data structure from
128  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
129  * passed into the routine for discovery state machine to issue an Extended
130  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
131  * and preparation routine that is used by all the discovery state machine
132  * routines and the ELS command-specific fields will be later set up by
133  * the individual discovery machine routines after calling this routine
134  * allocating and preparing a generic IOCB data structure. It fills in the
135  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
136  * payload and response payload (if expected). The reference count on the
137  * ndlp is incremented by 1 and the reference to the ndlp is put into
138  * context1 of the IOCB data structure for this IOCB to hold the ndlp
139  * reference for the command's callback function to access later.
140  *
141  * Return code
142  *   Pointer to the newly allocated/prepared els iocb data structure
143  *   NULL - when els iocb data structure allocation/preparation failed
144  **/
145 static struct lpfc_iocbq *
146 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
147                    uint16_t cmdSize, uint8_t retry,
148                    struct lpfc_nodelist *ndlp, uint32_t did,
149                    uint32_t elscmd)
150 {
151         struct lpfc_hba  *phba = vport->phba;
152         struct lpfc_iocbq *elsiocb;
153         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
154         struct ulp_bde64 *bpl;
155         IOCB_t *icmd;
156
157
158         if (!lpfc_is_link_up(phba))
159                 return NULL;
160
161         /* Allocate buffer for  command iocb */
162         elsiocb = lpfc_sli_get_iocbq(phba);
163
164         if (elsiocb == NULL)
165                 return NULL;
166
167         icmd = &elsiocb->iocb;
168
169         /* fill in BDEs for command */
170         /* Allocate buffer for command payload */
171         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
172         if (pcmd)
173                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
174         if (!pcmd || !pcmd->virt)
175                 goto els_iocb_free_pcmb_exit;
176
177         INIT_LIST_HEAD(&pcmd->list);
178
179         /* Allocate buffer for response payload */
180         if (expectRsp) {
181                 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
182                 if (prsp)
183                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
184                                                      &prsp->phys);
185                 if (!prsp || !prsp->virt)
186                         goto els_iocb_free_prsp_exit;
187                 INIT_LIST_HEAD(&prsp->list);
188         } else
189                 prsp = NULL;
190
191         /* Allocate buffer for Buffer ptr list */
192         pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
193         if (pbuflist)
194                 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
195                                                  &pbuflist->phys);
196         if (!pbuflist || !pbuflist->virt)
197                 goto els_iocb_free_pbuf_exit;
198
199         INIT_LIST_HEAD(&pbuflist->list);
200
201         icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
202         icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
203         icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
204         icmd->un.elsreq64.remoteID = did;       /* DID */
205         if (expectRsp) {
206                 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
207                 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
208                 icmd->ulpTimeout = phba->fc_ratov * 2;
209         } else {
210                 icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
211                 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
212         }
213         icmd->ulpBdeCount = 1;
214         icmd->ulpLe = 1;
215         icmd->ulpClass = CLASS3;
216
217         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
218                 icmd->un.elsreq64.myID = vport->fc_myDID;
219
220                 /* For ELS_REQUEST64_CR, use the VPI by default */
221                 icmd->ulpContext = vport->vpi;
222                 icmd->ulpCt_h = 0;
223                 icmd->ulpCt_l = 1;
224         }
225
226         bpl = (struct ulp_bde64 *) pbuflist->virt;
227         bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
228         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
229         bpl->tus.f.bdeSize = cmdSize;
230         bpl->tus.f.bdeFlags = 0;
231         bpl->tus.w = le32_to_cpu(bpl->tus.w);
232
233         if (expectRsp) {
234                 bpl++;
235                 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
236                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
237                 bpl->tus.f.bdeSize = FCELSSIZE;
238                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
239                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
240         }
241
242         /* prevent preparing iocb with NULL ndlp reference */
243         elsiocb->context1 = lpfc_nlp_get(ndlp);
244         if (!elsiocb->context1)
245                 goto els_iocb_free_pbuf_exit;
246         elsiocb->context2 = pcmd;
247         elsiocb->context3 = pbuflist;
248         elsiocb->retry = retry;
249         elsiocb->vport = vport;
250         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
251
252         if (prsp) {
253                 list_add(&prsp->list, &pcmd->list);
254         }
255         if (expectRsp) {
256                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
257                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
258                                  "0116 Xmit ELS command x%x to remote "
259                                  "NPORT x%x I/O tag: x%x, port state: x%x\n",
260                                  elscmd, did, elsiocb->iotag,
261                                  vport->port_state);
262         } else {
263                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
264                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
265                                  "0117 Xmit ELS response x%x to remote "
266                                  "NPORT x%x I/O tag: x%x, size: x%x\n",
267                                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
268                                  cmdSize);
269         }
270         return elsiocb;
271
272 els_iocb_free_pbuf_exit:
273         lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
274         kfree(pbuflist);
275
276 els_iocb_free_prsp_exit:
277         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
278         kfree(prsp);
279
280 els_iocb_free_pcmb_exit:
281         kfree(pcmd);
282         lpfc_sli_release_iocbq(phba, elsiocb);
283         return NULL;
284 }
285
286 /**
287  * lpfc_issue_fabric_reglogin: Issue fabric registration login for a vport.
288  * @vport: pointer to a host virtual N_Port data structure.
289  *
290  * This routine issues a fabric registration login for a @vport. An
291  * active ndlp node with Fabric_DID must already exist for this @vport.
292  * The routine invokes two mailbox commands to carry out fabric registration
293  * login through the HBA firmware: the first mailbox command requests the
294  * HBA to perform link configuration for the @vport; and the second mailbox
295  * command requests the HBA to perform the actual fabric registration login
296  * with the @vport.
297  *
298  * Return code
299  *   0 - successfully issued fabric registration login for @vport
300  *   -ENXIO -- failed to issue fabric registration login for @vport
301  **/
302 static int
303 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
304 {
305         struct lpfc_hba  *phba = vport->phba;
306         LPFC_MBOXQ_t *mbox;
307         struct lpfc_dmabuf *mp;
308         struct lpfc_nodelist *ndlp;
309         struct serv_parm *sp;
310         int rc;
311         int err = 0;
312
313         sp = &phba->fc_fabparam;
314         ndlp = lpfc_findnode_did(vport, Fabric_DID);
315         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
316                 err = 1;
317                 goto fail;
318         }
319
320         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
321         if (!mbox) {
322                 err = 2;
323                 goto fail;
324         }
325
326         vport->port_state = LPFC_FABRIC_CFG_LINK;
327         lpfc_config_link(phba, mbox);
328         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
329         mbox->vport = vport;
330
331         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
332         if (rc == MBX_NOT_FINISHED) {
333                 err = 3;
334                 goto fail_free_mbox;
335         }
336
337         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
338         if (!mbox) {
339                 err = 4;
340                 goto fail;
341         }
342         rc = lpfc_reg_login(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
343                             0);
344         if (rc) {
345                 err = 5;
346                 goto fail_free_mbox;
347         }
348
349         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
350         mbox->vport = vport;
351         /* increment the reference count on ndlp to hold reference
352          * for the callback routine.
353          */
354         mbox->context2 = lpfc_nlp_get(ndlp);
355
356         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
357         if (rc == MBX_NOT_FINISHED) {
358                 err = 6;
359                 goto fail_issue_reg_login;
360         }
361
362         return 0;
363
364 fail_issue_reg_login:
365         /* decrement the reference count on ndlp just incremented
366          * for the failed mbox command.
367          */
368         lpfc_nlp_put(ndlp);
369         mp = (struct lpfc_dmabuf *) mbox->context1;
370         lpfc_mbuf_free(phba, mp->virt, mp->phys);
371         kfree(mp);
372 fail_free_mbox:
373         mempool_free(mbox, phba->mbox_mem_pool);
374
375 fail:
376         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
377         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
378                 "0249 Cannot issue Register Fabric login: Err %d\n", err);
379         return -ENXIO;
380 }
381
382 /**
383  * lpfc_cmpl_els_flogi_fabric: Completion function for flogi to a fabric port.
384  * @vport: pointer to a host virtual N_Port data structure.
385  * @ndlp: pointer to a node-list data structure.
386  * @sp: pointer to service parameter data structure.
387  * @irsp: pointer to the IOCB within the lpfc response IOCB.
388  *
389  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
390  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
391  * port in a fabric topology. It properly sets up the parameters to the @ndlp
392  * from the IOCB response. It also check the newly assigned N_Port ID to the
393  * @vport against the previously assigned N_Port ID. If it is different from
394  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
395  * is invoked on all the remaining nodes with the @vport to unregister the
396  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
397  * is invoked to register login to the fabric.
398  *
399  * Return code
400  *   0 - Success (currently, always return 0)
401  **/
402 static int
403 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
404                            struct serv_parm *sp, IOCB_t *irsp)
405 {
406         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
407         struct lpfc_hba  *phba = vport->phba;
408         struct lpfc_nodelist *np;
409         struct lpfc_nodelist *next_np;
410
411         spin_lock_irq(shost->host_lock);
412         vport->fc_flag |= FC_FABRIC;
413         spin_unlock_irq(shost->host_lock);
414
415         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
416         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
417                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
418
419         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
420
421         if (phba->fc_topology == TOPOLOGY_LOOP) {
422                 spin_lock_irq(shost->host_lock);
423                 vport->fc_flag |= FC_PUBLIC_LOOP;
424                 spin_unlock_irq(shost->host_lock);
425         } else {
426                 /*
427                  * If we are a N-port connected to a Fabric, fixup sparam's so
428                  * logins to devices on remote loops work.
429                  */
430                 vport->fc_sparam.cmn.altBbCredit = 1;
431         }
432
433         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
434         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
435         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
436         ndlp->nlp_class_sup = 0;
437         if (sp->cls1.classValid)
438                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
439         if (sp->cls2.classValid)
440                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
441         if (sp->cls3.classValid)
442                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
443         if (sp->cls4.classValid)
444                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
445         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
446                                 sp->cmn.bbRcvSizeLsb;
447         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
448
449         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
450                 if (sp->cmn.response_multiple_NPort) {
451                         lpfc_printf_vlog(vport, KERN_WARNING,
452                                          LOG_ELS | LOG_VPORT,
453                                          "1816 FLOGI NPIV supported, "
454                                          "response data 0x%x\n",
455                                          sp->cmn.response_multiple_NPort);
456                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
457                 } else {
458                         /* Because we asked f/w for NPIV it still expects us
459                         to call reg_vnpid atleast for the physcial host */
460                         lpfc_printf_vlog(vport, KERN_WARNING,
461                                          LOG_ELS | LOG_VPORT,
462                                          "1817 Fabric does not support NPIV "
463                                          "- configuring single port mode.\n");
464                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
465                 }
466         }
467
468         if ((vport->fc_prevDID != vport->fc_myDID) &&
469                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
470
471                 /* If our NportID changed, we need to ensure all
472                  * remaining NPORTs get unreg_login'ed.
473                  */
474                 list_for_each_entry_safe(np, next_np,
475                                         &vport->fc_nodes, nlp_listp) {
476                         if (!NLP_CHK_NODE_ACT(np))
477                                 continue;
478                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
479                                    !(np->nlp_flag & NLP_NPR_ADISC))
480                                 continue;
481                         spin_lock_irq(shost->host_lock);
482                         np->nlp_flag &= ~NLP_NPR_ADISC;
483                         spin_unlock_irq(shost->host_lock);
484                         lpfc_unreg_rpi(vport, np);
485                 }
486                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
487                         lpfc_mbx_unreg_vpi(vport);
488                         spin_lock_irq(shost->host_lock);
489                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
490                         spin_unlock_irq(shost->host_lock);
491                 }
492         }
493
494         lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
495
496         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
497             vport->fc_flag & FC_VPORT_NEEDS_REG_VPI) {
498                 lpfc_register_new_vport(phba, vport, ndlp);
499                 return 0;
500         }
501         lpfc_issue_fabric_reglogin(vport);
502         return 0;
503 }
504
505 /**
506  * lpfc_cmpl_els_flogi_nport: Completion function for flogi to an N_Port.
507  * @vport: pointer to a host virtual N_Port data structure.
508  * @ndlp: pointer to a node-list data structure.
509  * @sp: pointer to service parameter data structure.
510  *
511  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
512  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
513  * in a point-to-point topology. First, the @vport's N_Port Name is compared
514  * with the received N_Port Name: if the @vport's N_Port Name is greater than
515  * the received N_Port Name lexicographically, this node shall assign local
516  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
517  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
518  * this node shall just wait for the remote node to issue PLOGI and assign
519  * N_Port IDs.
520  *
521  * Return code
522  *   0 - Success
523  *   -ENXIO - Fail
524  **/
525 static int
526 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
527                           struct serv_parm *sp)
528 {
529         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
530         struct lpfc_hba  *phba = vport->phba;
531         LPFC_MBOXQ_t *mbox;
532         int rc;
533
534         spin_lock_irq(shost->host_lock);
535         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
536         spin_unlock_irq(shost->host_lock);
537
538         phba->fc_edtov = FF_DEF_EDTOV;
539         phba->fc_ratov = FF_DEF_RATOV;
540         rc = memcmp(&vport->fc_portname, &sp->portName,
541                     sizeof(vport->fc_portname));
542         if (rc >= 0) {
543                 /* This side will initiate the PLOGI */
544                 spin_lock_irq(shost->host_lock);
545                 vport->fc_flag |= FC_PT2PT_PLOGI;
546                 spin_unlock_irq(shost->host_lock);
547
548                 /*
549                  * N_Port ID cannot be 0, set our to LocalID the other
550                  * side will be RemoteID.
551                  */
552
553                 /* not equal */
554                 if (rc)
555                         vport->fc_myDID = PT2PT_LocalID;
556
557                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
558                 if (!mbox)
559                         goto fail;
560
561                 lpfc_config_link(phba, mbox);
562
563                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
564                 mbox->vport = vport;
565                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
566                 if (rc == MBX_NOT_FINISHED) {
567                         mempool_free(mbox, phba->mbox_mem_pool);
568                         goto fail;
569                 }
570                 /* Decrement ndlp reference count indicating that ndlp can be
571                  * safely released when other references to it are done.
572                  */
573                 lpfc_nlp_put(ndlp);
574
575                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
576                 if (!ndlp) {
577                         /*
578                          * Cannot find existing Fabric ndlp, so allocate a
579                          * new one
580                          */
581                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
582                         if (!ndlp)
583                                 goto fail;
584                         lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
585                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
586                         ndlp = lpfc_enable_node(vport, ndlp,
587                                                 NLP_STE_UNUSED_NODE);
588                         if(!ndlp)
589                                 goto fail;
590                 }
591
592                 memcpy(&ndlp->nlp_portname, &sp->portName,
593                        sizeof(struct lpfc_name));
594                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
595                        sizeof(struct lpfc_name));
596                 /* Set state will put ndlp onto node list if not already done */
597                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
598                 spin_lock_irq(shost->host_lock);
599                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
600                 spin_unlock_irq(shost->host_lock);
601         } else
602                 /* This side will wait for the PLOGI, decrement ndlp reference
603                  * count indicating that ndlp can be released when other
604                  * references to it are done.
605                  */
606                 lpfc_nlp_put(ndlp);
607
608         /* If we are pt2pt with another NPort, force NPIV off! */
609         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
610
611         spin_lock_irq(shost->host_lock);
612         vport->fc_flag |= FC_PT2PT;
613         spin_unlock_irq(shost->host_lock);
614
615         /* Start discovery - this should just do CLEAR_LA */
616         lpfc_disc_start(vport);
617         return 0;
618 fail:
619         return -ENXIO;
620 }
621
622 /**
623  * lpfc_cmpl_els_flogi: Completion callback function for flogi.
624  * @phba: pointer to lpfc hba data structure.
625  * @cmdiocb: pointer to lpfc command iocb data structure.
626  * @rspiocb: pointer to lpfc response iocb data structure.
627  *
628  * This routine is the top-level completion callback function for issuing
629  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
630  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
631  * retry has been made (either immediately or delayed with lpfc_els_retry()
632  * returning 1), the command IOCB will be released and function returned.
633  * If the retry attempt has been given up (possibly reach the maximum
634  * number of retries), one additional decrement of ndlp reference shall be
635  * invoked before going out after releasing the command IOCB. This will
636  * actually release the remote node (Note, lpfc_els_free_iocb() will also
637  * invoke one decrement of ndlp reference count). If no error reported in
638  * the IOCB status, the command Port ID field is used to determine whether
639  * this is a point-to-point topology or a fabric topology: if the Port ID
640  * field is assigned, it is a fabric topology; otherwise, it is a
641  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
642  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
643  * specific topology completion conditions.
644  **/
645 static void
646 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
647                     struct lpfc_iocbq *rspiocb)
648 {
649         struct lpfc_vport *vport = cmdiocb->vport;
650         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
651         IOCB_t *irsp = &rspiocb->iocb;
652         struct lpfc_nodelist *ndlp = cmdiocb->context1;
653         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
654         struct serv_parm *sp;
655         int rc;
656
657         /* Check to see if link went down during discovery */
658         if (lpfc_els_chk_latt(vport)) {
659                 /* One additional decrement on node reference count to
660                  * trigger the release of the node
661                  */
662                 lpfc_nlp_put(ndlp);
663                 goto out;
664         }
665
666         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
667                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
668                 irsp->ulpStatus, irsp->un.ulpWord[4],
669                 vport->port_state);
670
671         if (irsp->ulpStatus) {
672                 /* Check for retry */
673                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
674                         goto out;
675
676                 /* FLOGI failed, so there is no fabric */
677                 spin_lock_irq(shost->host_lock);
678                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
679                 spin_unlock_irq(shost->host_lock);
680
681                 /* If private loop, then allow max outstanding els to be
682                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
683                  * alpa map would take too long otherwise.
684                  */
685                 if (phba->alpa_map[0] == 0) {
686                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
687                 }
688
689                 /* FLOGI failure */
690                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
691                                  "0100 FLOGI failure Data: x%x x%x "
692                                  "x%x\n",
693                                  irsp->ulpStatus, irsp->un.ulpWord[4],
694                                  irsp->ulpTimeout);
695                 goto flogifail;
696         }
697
698         /*
699          * The FLogI succeeded.  Sync the data for the CPU before
700          * accessing it.
701          */
702         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
703
704         sp = prsp->virt + sizeof(uint32_t);
705
706         /* FLOGI completes successfully */
707         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
708                          "0101 FLOGI completes sucessfully "
709                          "Data: x%x x%x x%x x%x\n",
710                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
711                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
712
713         if (vport->port_state == LPFC_FLOGI) {
714                 /*
715                  * If Common Service Parameters indicate Nport
716                  * we are point to point, if Fport we are Fabric.
717                  */
718                 if (sp->cmn.fPort)
719                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
720                 else
721                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
722
723                 if (!rc)
724                         goto out;
725         }
726
727 flogifail:
728         lpfc_nlp_put(ndlp);
729
730         if (!lpfc_error_lost_link(irsp)) {
731                 /* FLOGI failed, so just use loop map to make discovery list */
732                 lpfc_disc_list_loopmap(vport);
733
734                 /* Start discovery */
735                 lpfc_disc_start(vport);
736         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
737                         ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
738                         (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
739                         (phba->link_state != LPFC_CLEAR_LA)) {
740                 /* If FLOGI failed enable link interrupt. */
741                 lpfc_issue_clear_la(phba, vport);
742         }
743 out:
744         lpfc_els_free_iocb(phba, cmdiocb);
745 }
746
747 /**
748  * lpfc_issue_els_flogi: Issue an flogi iocb command for a vport.
749  * @vport: pointer to a host virtual N_Port data structure.
750  * @ndlp: pointer to a node-list data structure.
751  * @retry: number of retries to the command IOCB.
752  *
753  * This routine issues a Fabric Login (FLOGI) Request ELS command
754  * for a @vport. The initiator service parameters are put into the payload
755  * of the FLOGI Request IOCB and the top-level callback function pointer
756  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
757  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
758  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
759  *
760  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
761  * will be incremented by 1 for holding the ndlp and the reference to ndlp
762  * will be stored into the context1 field of the IOCB for the completion
763  * callback function to the FLOGI ELS command.
764  *
765  * Return code
766  *   0 - successfully issued flogi iocb for @vport
767  *   1 - failed to issue flogi iocb for @vport
768  **/
769 static int
770 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
771                      uint8_t retry)
772 {
773         struct lpfc_hba  *phba = vport->phba;
774         struct serv_parm *sp;
775         IOCB_t *icmd;
776         struct lpfc_iocbq *elsiocb;
777         struct lpfc_sli_ring *pring;
778         uint8_t *pcmd;
779         uint16_t cmdsize;
780         uint32_t tmo;
781         int rc;
782
783         pring = &phba->sli.ring[LPFC_ELS_RING];
784
785         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
786         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
787                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
788
789         if (!elsiocb)
790                 return 1;
791
792         icmd = &elsiocb->iocb;
793         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
794
795         /* For FLOGI request, remainder of payload is service parameters */
796         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
797         pcmd += sizeof(uint32_t);
798         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
799         sp = (struct serv_parm *) pcmd;
800
801         /* Setup CSPs accordingly for Fabric */
802         sp->cmn.e_d_tov = 0;
803         sp->cmn.w2.r_a_tov = 0;
804         sp->cls1.classValid = 0;
805         sp->cls2.seqDelivery = 1;
806         sp->cls3.seqDelivery = 1;
807         if (sp->cmn.fcphLow < FC_PH3)
808                 sp->cmn.fcphLow = FC_PH3;
809         if (sp->cmn.fcphHigh < FC_PH3)
810                 sp->cmn.fcphHigh = FC_PH3;
811
812         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
813                 sp->cmn.request_multiple_Nport = 1;
814
815                 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
816                 icmd->ulpCt_h = 1;
817                 icmd->ulpCt_l = 0;
818         }
819
820         if (phba->fc_topology != TOPOLOGY_LOOP) {
821                 icmd->un.elsreq64.myID = 0;
822                 icmd->un.elsreq64.fl = 1;
823         }
824
825         tmo = phba->fc_ratov;
826         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
827         lpfc_set_disctmo(vport);
828         phba->fc_ratov = tmo;
829
830         phba->fc_stat.elsXmitFLOGI++;
831         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
832
833         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
834                 "Issue FLOGI:     opt:x%x",
835                 phba->sli3_options, 0, 0);
836
837         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
838         if (rc == IOCB_ERROR) {
839                 lpfc_els_free_iocb(phba, elsiocb);
840                 return 1;
841         }
842         return 0;
843 }
844
845 /**
846  * lpfc_els_abort_flogi: Abort all outstanding flogi iocbs.
847  * @phba: pointer to lpfc hba data structure.
848  *
849  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
850  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
851  * list and issues an abort IOCB commond on each outstanding IOCB that
852  * contains a active Fabric_DID ndlp. Note that this function is to issue
853  * the abort IOCB command on all the outstanding IOCBs, thus when this
854  * function returns, it does not guarantee all the IOCBs are actually aborted.
855  *
856  * Return code
857  *   0 - Sucessfully issued abort iocb on all outstanding flogis (Always 0)
858  **/
859 int
860 lpfc_els_abort_flogi(struct lpfc_hba *phba)
861 {
862         struct lpfc_sli_ring *pring;
863         struct lpfc_iocbq *iocb, *next_iocb;
864         struct lpfc_nodelist *ndlp;
865         IOCB_t *icmd;
866
867         /* Abort outstanding I/O on NPort <nlp_DID> */
868         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
869                         "0201 Abort outstanding I/O on NPort x%x\n",
870                         Fabric_DID);
871
872         pring = &phba->sli.ring[LPFC_ELS_RING];
873
874         /*
875          * Check the txcmplq for an iocb that matches the nport the driver is
876          * searching for.
877          */
878         spin_lock_irq(&phba->hbalock);
879         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
880                 icmd = &iocb->iocb;
881                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
882                     icmd->un.elsreq64.bdl.ulpIoTag32) {
883                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
884                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
885                             (ndlp->nlp_DID == Fabric_DID))
886                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
887                 }
888         }
889         spin_unlock_irq(&phba->hbalock);
890
891         return 0;
892 }
893
894 /**
895  * lpfc_initial_flogi: Issue an initial fabric login for a vport.
896  * @vport: pointer to a host virtual N_Port data structure.
897  *
898  * This routine issues an initial Fabric Login (FLOGI) for the @vport
899  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
900  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
901  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
902  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
903  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
904  * @vport.
905  *
906  * Return code
907  *   0 - failed to issue initial flogi for @vport
908  *   1 - successfully issued initial flogi for @vport
909  **/
910 int
911 lpfc_initial_flogi(struct lpfc_vport *vport)
912 {
913         struct lpfc_hba *phba = vport->phba;
914         struct lpfc_nodelist *ndlp;
915
916         vport->port_state = LPFC_FLOGI;
917         lpfc_set_disctmo(vport);
918
919         /* First look for the Fabric ndlp */
920         ndlp = lpfc_findnode_did(vport, Fabric_DID);
921         if (!ndlp) {
922                 /* Cannot find existing Fabric ndlp, so allocate a new one */
923                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
924                 if (!ndlp)
925                         return 0;
926                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
927                 /* Put ndlp onto node list */
928                 lpfc_enqueue_node(vport, ndlp);
929         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
930                 /* re-setup ndlp without removing from node list */
931                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
932                 if (!ndlp)
933                         return 0;
934         }
935
936         if (lpfc_issue_els_flogi(vport, ndlp, 0))
937                 /* This decrement of reference count to node shall kick off
938                  * the release of the node.
939                  */
940                 lpfc_nlp_put(ndlp);
941
942         return 1;
943 }
944
945 /**
946  * lpfc_initial_fdisc: Issue an initial fabric discovery for a vport.
947  * @vport: pointer to a host virtual N_Port data structure.
948  *
949  * This routine issues an initial Fabric Discover (FDISC) for the @vport
950  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
951  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
952  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
953  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
954  * is then invoked with the @vport and the ndlp to perform the FDISC for the
955  * @vport.
956  *
957  * Return code
958  *   0 - failed to issue initial fdisc for @vport
959  *   1 - successfully issued initial fdisc for @vport
960  **/
961 int
962 lpfc_initial_fdisc(struct lpfc_vport *vport)
963 {
964         struct lpfc_hba *phba = vport->phba;
965         struct lpfc_nodelist *ndlp;
966
967         /* First look for the Fabric ndlp */
968         ndlp = lpfc_findnode_did(vport, Fabric_DID);
969         if (!ndlp) {
970                 /* Cannot find existing Fabric ndlp, so allocate a new one */
971                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
972                 if (!ndlp)
973                         return 0;
974                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
975                 /* Put ndlp onto node list */
976                 lpfc_enqueue_node(vport, ndlp);
977         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
978                 /* re-setup ndlp without removing from node list */
979                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
980                 if (!ndlp)
981                         return 0;
982         }
983
984         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
985                 /* decrement node reference count to trigger the release of
986                  * the node.
987                  */
988                 lpfc_nlp_put(ndlp);
989                 return 0;
990         }
991         return 1;
992 }
993
994 /**
995  * lpfc_more_plogi: Check and issue remaining plogis for a vport.
996  * @vport: pointer to a host virtual N_Port data structure.
997  *
998  * This routine checks whether there are more remaining Port Logins
999  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1000  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1001  * to issue ELS PLOGIs up to the configured discover threads with the
1002  * @vport (@vport->cfg_discovery_threads). The function also decrement
1003  * the @vport's num_disc_node by 1 if it is not already 0.
1004  **/
1005 void
1006 lpfc_more_plogi(struct lpfc_vport *vport)
1007 {
1008         int sentplogi;
1009
1010         if (vport->num_disc_nodes)
1011                 vport->num_disc_nodes--;
1012
1013         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1014         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1015                          "0232 Continue discovery with %d PLOGIs to go "
1016                          "Data: x%x x%x x%x\n",
1017                          vport->num_disc_nodes, vport->fc_plogi_cnt,
1018                          vport->fc_flag, vport->port_state);
1019         /* Check to see if there are more PLOGIs to be sent */
1020         if (vport->fc_flag & FC_NLP_MORE)
1021                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1022                 sentplogi = lpfc_els_disc_plogi(vport);
1023
1024         return;
1025 }
1026
1027 /**
1028  * lpfc_plogi_confirm_nport: Confirm pologi wwpn matches stored ndlp.
1029  * @phba: pointer to lpfc hba data structure.
1030  * @prsp: pointer to response IOCB payload.
1031  * @ndlp: pointer to a node-list data structure.
1032  *
1033  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1034  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1035  * The following cases are considered N_Port confirmed:
1036  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1037  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1038  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1039  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1040  * 1) if there is a node on vport list other than the @ndlp with the same
1041  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1042  * on that node to release the RPI associated with the node; 2) if there is
1043  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1044  * into, a new node shall be allocated (or activated). In either case, the
1045  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1046  * be released and the new_ndlp shall be put on to the vport node list and
1047  * its pointer returned as the confirmed node.
1048  *
1049  * Note that before the @ndlp got "released", the keepDID from not-matching
1050  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1051  * of the @ndlp. This is because the release of @ndlp is actually to put it
1052  * into an inactive state on the vport node list and the vport node list
1053  * management algorithm does not allow two node with a same DID.
1054  *
1055  * Return code
1056  *   pointer to the PLOGI N_Port @ndlp
1057  **/
1058 static struct lpfc_nodelist *
1059 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1060                          struct lpfc_nodelist *ndlp)
1061 {
1062         struct lpfc_vport    *vport = ndlp->vport;
1063         struct lpfc_nodelist *new_ndlp;
1064         struct lpfc_rport_data *rdata;
1065         struct fc_rport *rport;
1066         struct serv_parm *sp;
1067         uint8_t  name[sizeof(struct lpfc_name)];
1068         uint32_t rc, keepDID = 0;
1069
1070         /* Fabric nodes can have the same WWPN so we don't bother searching
1071          * by WWPN.  Just return the ndlp that was given to us.
1072          */
1073         if (ndlp->nlp_type & NLP_FABRIC)
1074                 return ndlp;
1075
1076         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1077         memset(name, 0, sizeof(struct lpfc_name));
1078
1079         /* Now we find out if the NPort we are logging into, matches the WWPN
1080          * we have for that ndlp. If not, we have some work to do.
1081          */
1082         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1083
1084         if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1085                 return ndlp;
1086
1087         if (!new_ndlp) {
1088                 rc = memcmp(&ndlp->nlp_portname, name,
1089                             sizeof(struct lpfc_name));
1090                 if (!rc)
1091                         return ndlp;
1092                 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1093                 if (!new_ndlp)
1094                         return ndlp;
1095                 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
1096         } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1097                 rc = memcmp(&ndlp->nlp_portname, name,
1098                             sizeof(struct lpfc_name));
1099                 if (!rc)
1100                         return ndlp;
1101                 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1102                                                 NLP_STE_UNUSED_NODE);
1103                 if (!new_ndlp)
1104                         return ndlp;
1105                 keepDID = new_ndlp->nlp_DID;
1106         } else
1107                 keepDID = new_ndlp->nlp_DID;
1108
1109         lpfc_unreg_rpi(vport, new_ndlp);
1110         new_ndlp->nlp_DID = ndlp->nlp_DID;
1111         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1112
1113         if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
1114                 new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1115         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1116
1117         /* Set state will put new_ndlp on to node list if not already done */
1118         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1119
1120         /* Move this back to NPR state */
1121         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1122                 /* The new_ndlp is replacing ndlp totally, so we need
1123                  * to put ndlp on UNUSED list and try to free it.
1124                  */
1125
1126                 /* Fix up the rport accordingly */
1127                 rport =  ndlp->rport;
1128                 if (rport) {
1129                         rdata = rport->dd_data;
1130                         if (rdata->pnode == ndlp) {
1131                                 lpfc_nlp_put(ndlp);
1132                                 ndlp->rport = NULL;
1133                                 rdata->pnode = lpfc_nlp_get(new_ndlp);
1134                                 new_ndlp->rport = rport;
1135                         }
1136                         new_ndlp->nlp_type = ndlp->nlp_type;
1137                 }
1138                 /* We shall actually free the ndlp with both nlp_DID and
1139                  * nlp_portname fields equals 0 to avoid any ndlp on the
1140                  * nodelist never to be used.
1141                  */
1142                 if (ndlp->nlp_DID == 0) {
1143                         spin_lock_irq(&phba->ndlp_lock);
1144                         NLP_SET_FREE_REQ(ndlp);
1145                         spin_unlock_irq(&phba->ndlp_lock);
1146                 }
1147
1148                 /* Two ndlps cannot have the same did on the nodelist */
1149                 ndlp->nlp_DID = keepDID;
1150                 lpfc_drop_node(vport, ndlp);
1151         }
1152         else {
1153                 lpfc_unreg_rpi(vport, ndlp);
1154                 /* Two ndlps cannot have the same did */
1155                 ndlp->nlp_DID = keepDID;
1156                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1157         }
1158         return new_ndlp;
1159 }
1160
1161 /**
1162  * lpfc_end_rscn: Check and handle more rscn for a vport.
1163  * @vport: pointer to a host virtual N_Port data structure.
1164  *
1165  * This routine checks whether more Registration State Change
1166  * Notifications (RSCNs) came in while the discovery state machine was in
1167  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1168  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1169  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1170  * handling the RSCNs.
1171  **/
1172 void
1173 lpfc_end_rscn(struct lpfc_vport *vport)
1174 {
1175         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1176
1177         if (vport->fc_flag & FC_RSCN_MODE) {
1178                 /*
1179                  * Check to see if more RSCNs came in while we were
1180                  * processing this one.
1181                  */
1182                 if (vport->fc_rscn_id_cnt ||
1183                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1184                         lpfc_els_handle_rscn(vport);
1185                 else {
1186                         spin_lock_irq(shost->host_lock);
1187                         vport->fc_flag &= ~FC_RSCN_MODE;
1188                         spin_unlock_irq(shost->host_lock);
1189                 }
1190         }
1191 }
1192
1193 /**
1194  * lpfc_cmpl_els_plogi: Completion callback function for plogi.
1195  * @phba: pointer to lpfc hba data structure.
1196  * @cmdiocb: pointer to lpfc command iocb data structure.
1197  * @rspiocb: pointer to lpfc response iocb data structure.
1198  *
1199  * This routine is the completion callback function for issuing the Port
1200  * Login (PLOGI) command. For PLOGI completion, there must be an active
1201  * ndlp on the vport node list that matches the remote node ID from the
1202  * PLOGI reponse IOCB. If such ndlp does not exist, the PLOGI is simply
1203  * ignored and command IOCB released. The PLOGI response IOCB status is
1204  * checked for error conditons. If there is error status reported, PLOGI
1205  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1206  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1207  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1208  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1209  * there are additional N_Port nodes with the vport that need to perform
1210  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1211  * PLOGIs.
1212  **/
1213 static void
1214 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1215                     struct lpfc_iocbq *rspiocb)
1216 {
1217         struct lpfc_vport *vport = cmdiocb->vport;
1218         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1219         IOCB_t *irsp;
1220         struct lpfc_nodelist *ndlp;
1221         struct lpfc_dmabuf *prsp;
1222         int disc, rc, did, type;
1223
1224         /* we pass cmdiocb to state machine which needs rspiocb as well */
1225         cmdiocb->context_un.rsp_iocb = rspiocb;
1226
1227         irsp = &rspiocb->iocb;
1228         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1229                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
1230                 irsp->ulpStatus, irsp->un.ulpWord[4],
1231                 irsp->un.elsreq64.remoteID);
1232
1233         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1234         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1235                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1236                                  "0136 PLOGI completes to NPort x%x "
1237                                  "with no ndlp. Data: x%x x%x x%x\n",
1238                                  irsp->un.elsreq64.remoteID,
1239                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1240                                  irsp->ulpIoTag);
1241                 goto out;
1242         }
1243
1244         /* Since ndlp can be freed in the disc state machine, note if this node
1245          * is being used during discovery.
1246          */
1247         spin_lock_irq(shost->host_lock);
1248         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1249         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1250         spin_unlock_irq(shost->host_lock);
1251         rc   = 0;
1252
1253         /* PLOGI completes to NPort <nlp_DID> */
1254         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1255                          "0102 PLOGI completes to NPort x%x "
1256                          "Data: x%x x%x x%x x%x x%x\n",
1257                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1258                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1259         /* Check to see if link went down during discovery */
1260         if (lpfc_els_chk_latt(vport)) {
1261                 spin_lock_irq(shost->host_lock);
1262                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1263                 spin_unlock_irq(shost->host_lock);
1264                 goto out;
1265         }
1266
1267         /* ndlp could be freed in DSM, save these values now */
1268         type = ndlp->nlp_type;
1269         did = ndlp->nlp_DID;
1270
1271         if (irsp->ulpStatus) {
1272                 /* Check for retry */
1273                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1274                         /* ELS command is being retried */
1275                         if (disc) {
1276                                 spin_lock_irq(shost->host_lock);
1277                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1278                                 spin_unlock_irq(shost->host_lock);
1279                         }
1280                         goto out;
1281                 }
1282                 /* PLOGI failed */
1283                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1284                 if (lpfc_error_lost_link(irsp))
1285                         rc = NLP_STE_FREED_NODE;
1286                 else
1287                         rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1288                                                      NLP_EVT_CMPL_PLOGI);
1289         } else {
1290                 /* Good status, call state machine */
1291                 prsp = list_entry(((struct lpfc_dmabuf *)
1292                                    cmdiocb->context2)->list.next,
1293                                   struct lpfc_dmabuf, list);
1294                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1295                 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1296                                              NLP_EVT_CMPL_PLOGI);
1297         }
1298
1299         if (disc && vport->num_disc_nodes) {
1300                 /* Check to see if there are more PLOGIs to be sent */
1301                 lpfc_more_plogi(vport);
1302
1303                 if (vport->num_disc_nodes == 0) {
1304                         spin_lock_irq(shost->host_lock);
1305                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
1306                         spin_unlock_irq(shost->host_lock);
1307
1308                         lpfc_can_disctmo(vport);
1309                         lpfc_end_rscn(vport);
1310                 }
1311         }
1312
1313 out:
1314         lpfc_els_free_iocb(phba, cmdiocb);
1315         return;
1316 }
1317
1318 /**
1319  * lpfc_issue_els_plogi: Issue an plogi iocb command for a vport.
1320  * @vport: pointer to a host virtual N_Port data structure.
1321  * @did: destination port identifier.
1322  * @retry: number of retries to the command IOCB.
1323  *
1324  * This routine issues a Port Login (PLOGI) command to a remote N_Port
1325  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1326  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1327  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1328  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1329  *
1330  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1331  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1332  * will be stored into the context1 field of the IOCB for the completion
1333  * callback function to the PLOGI ELS command.
1334  *
1335  * Return code
1336  *   0 - Successfully issued a plogi for @vport
1337  *   1 - failed to issue a plogi for @vport
1338  **/
1339 int
1340 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1341 {
1342         struct lpfc_hba  *phba = vport->phba;
1343         struct serv_parm *sp;
1344         IOCB_t *icmd;
1345         struct lpfc_nodelist *ndlp;
1346         struct lpfc_iocbq *elsiocb;
1347         struct lpfc_sli_ring *pring;
1348         struct lpfc_sli *psli;
1349         uint8_t *pcmd;
1350         uint16_t cmdsize;
1351         int ret;
1352
1353         psli = &phba->sli;
1354         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1355
1356         ndlp = lpfc_findnode_did(vport, did);
1357         if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1358                 ndlp = NULL;
1359
1360         /* If ndlp is not NULL, we will bump the reference count on it */
1361         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1362         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
1363                                      ELS_CMD_PLOGI);
1364         if (!elsiocb)
1365                 return 1;
1366
1367         icmd = &elsiocb->iocb;
1368         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1369
1370         /* For PLOGI request, remainder of payload is service parameters */
1371         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
1372         pcmd += sizeof(uint32_t);
1373         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1374         sp = (struct serv_parm *) pcmd;
1375
1376         if (sp->cmn.fcphLow < FC_PH_4_3)
1377                 sp->cmn.fcphLow = FC_PH_4_3;
1378
1379         if (sp->cmn.fcphHigh < FC_PH3)
1380                 sp->cmn.fcphHigh = FC_PH3;
1381
1382         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1383                 "Issue PLOGI:     did:x%x",
1384                 did, 0, 0);
1385
1386         phba->fc_stat.elsXmitPLOGI++;
1387         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1388         ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1389
1390         if (ret == IOCB_ERROR) {
1391                 lpfc_els_free_iocb(phba, elsiocb);
1392                 return 1;
1393         }
1394         return 0;
1395 }
1396
1397 /**
1398  * lpfc_cmpl_els_prli: Completion callback function for prli.
1399  * @phba: pointer to lpfc hba data structure.
1400  * @cmdiocb: pointer to lpfc command iocb data structure.
1401  * @rspiocb: pointer to lpfc response iocb data structure.
1402  *
1403  * This routine is the completion callback function for a Process Login
1404  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
1405  * status. If there is error status reported, PRLI retry shall be attempted
1406  * by invoking the lpfc_els_retry() routine. Otherwise, the state
1407  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
1408  * ndlp to mark the PRLI completion.
1409  **/
1410 static void
1411 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1412                    struct lpfc_iocbq *rspiocb)
1413 {
1414         struct lpfc_vport *vport = cmdiocb->vport;
1415         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1416         IOCB_t *irsp;
1417         struct lpfc_sli *psli;
1418         struct lpfc_nodelist *ndlp;
1419
1420         psli = &phba->sli;
1421         /* we pass cmdiocb to state machine which needs rspiocb as well */
1422         cmdiocb->context_un.rsp_iocb = rspiocb;
1423
1424         irsp = &(rspiocb->iocb);
1425         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1426         spin_lock_irq(shost->host_lock);
1427         ndlp->nlp_flag &= ~NLP_PRLI_SND;
1428         spin_unlock_irq(shost->host_lock);
1429
1430         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1431                 "PRLI cmpl:       status:x%x/x%x did:x%x",
1432                 irsp->ulpStatus, irsp->un.ulpWord[4],
1433                 ndlp->nlp_DID);
1434         /* PRLI completes to NPort <nlp_DID> */
1435         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1436                          "0103 PRLI completes to NPort x%x "
1437                          "Data: x%x x%x x%x x%x\n",
1438                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1439                          irsp->ulpTimeout, vport->num_disc_nodes);
1440
1441         vport->fc_prli_sent--;
1442         /* Check to see if link went down during discovery */
1443         if (lpfc_els_chk_latt(vport))
1444                 goto out;
1445
1446         if (irsp->ulpStatus) {
1447                 /* Check for retry */
1448                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1449                         /* ELS command is being retried */
1450                         goto out;
1451                 }
1452                 /* PRLI failed */
1453                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1454                 if (lpfc_error_lost_link(irsp))
1455                         goto out;
1456                 else
1457                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1458                                                 NLP_EVT_CMPL_PRLI);
1459         } else
1460                 /* Good status, call state machine */
1461                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1462                                         NLP_EVT_CMPL_PRLI);
1463 out:
1464         lpfc_els_free_iocb(phba, cmdiocb);
1465         return;
1466 }
1467
1468 /**
1469  * lpfc_issue_els_prli: Issue a prli iocb command for a vport.
1470  * @vport: pointer to a host virtual N_Port data structure.
1471  * @ndlp: pointer to a node-list data structure.
1472  * @retry: number of retries to the command IOCB.
1473  *
1474  * This routine issues a Process Login (PRLI) ELS command for the
1475  * @vport. The PRLI service parameters are set up in the payload of the
1476  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
1477  * is put to the IOCB completion callback func field before invoking the
1478  * routine lpfc_sli_issue_iocb() to send out PRLI command.
1479  *
1480  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1481  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1482  * will be stored into the context1 field of the IOCB for the completion
1483  * callback function to the PRLI ELS command.
1484  *
1485  * Return code
1486  *   0 - successfully issued prli iocb command for @vport
1487  *   1 - failed to issue prli iocb command for @vport
1488  **/
1489 int
1490 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1491                     uint8_t retry)
1492 {
1493         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1494         struct lpfc_hba *phba = vport->phba;
1495         PRLI *npr;
1496         IOCB_t *icmd;
1497         struct lpfc_iocbq *elsiocb;
1498         struct lpfc_sli_ring *pring;
1499         struct lpfc_sli *psli;
1500         uint8_t *pcmd;
1501         uint16_t cmdsize;
1502
1503         psli = &phba->sli;
1504         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1505
1506         cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1507         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1508                                      ndlp->nlp_DID, ELS_CMD_PRLI);
1509         if (!elsiocb)
1510                 return 1;
1511
1512         icmd = &elsiocb->iocb;
1513         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1514
1515         /* For PRLI request, remainder of payload is service parameters */
1516         memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1517         *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1518         pcmd += sizeof(uint32_t);
1519
1520         /* For PRLI, remainder of payload is PRLI parameter page */
1521         npr = (PRLI *) pcmd;
1522         /*
1523          * If our firmware version is 3.20 or later,
1524          * set the following bits for FC-TAPE support.
1525          */
1526         if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1527                 npr->ConfmComplAllowed = 1;
1528                 npr->Retry = 1;
1529                 npr->TaskRetryIdReq = 1;
1530         }
1531         npr->estabImagePair = 1;
1532         npr->readXferRdyDis = 1;
1533
1534         /* For FCP support */
1535         npr->prliType = PRLI_FCP_TYPE;
1536         npr->initiatorFunc = 1;
1537
1538         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1539                 "Issue PRLI:      did:x%x",
1540                 ndlp->nlp_DID, 0, 0);
1541
1542         phba->fc_stat.elsXmitPRLI++;
1543         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1544         spin_lock_irq(shost->host_lock);
1545         ndlp->nlp_flag |= NLP_PRLI_SND;
1546         spin_unlock_irq(shost->host_lock);
1547         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1548                 spin_lock_irq(shost->host_lock);
1549                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
1550                 spin_unlock_irq(shost->host_lock);
1551                 lpfc_els_free_iocb(phba, elsiocb);
1552                 return 1;
1553         }
1554         vport->fc_prli_sent++;
1555         return 0;
1556 }
1557
1558 /**
1559  * lpfc_rscn_disc: Perform rscn discovery for a vport.
1560  * @vport: pointer to a host virtual N_Port data structure.
1561  *
1562  * This routine performs Registration State Change Notification (RSCN)
1563  * discovery for a @vport. If the @vport's node port recovery count is not
1564  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
1565  * the nodes that need recovery. If none of the PLOGI were needed through
1566  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
1567  * invoked to check and handle possible more RSCN came in during the period
1568  * of processing the current ones.
1569  **/
1570 static void
1571 lpfc_rscn_disc(struct lpfc_vport *vport)
1572 {
1573         lpfc_can_disctmo(vport);
1574
1575         /* RSCN discovery */
1576         /* go thru NPR nodes and issue ELS PLOGIs */
1577         if (vport->fc_npr_cnt)
1578                 if (lpfc_els_disc_plogi(vport))
1579                         return;
1580
1581         lpfc_end_rscn(vport);
1582 }
1583
1584 /**
1585  * lpfc_adisc_done: Complete the adisc phase of discovery.
1586  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
1587  *
1588  * This function is called when the final ADISC is completed during discovery.
1589  * This function handles clearing link attention or issuing reg_vpi depending
1590  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
1591  * discovery.
1592  * This function is called with no locks held.
1593  **/
1594 static void
1595 lpfc_adisc_done(struct lpfc_vport *vport)
1596 {
1597         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1598         struct lpfc_hba   *phba = vport->phba;
1599
1600         /*
1601          * For NPIV, cmpl_reg_vpi will set port_state to READY,
1602          * and continue discovery.
1603          */
1604         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1605             !(vport->fc_flag & FC_RSCN_MODE)) {
1606                 lpfc_issue_reg_vpi(phba, vport);
1607                 return;
1608         }
1609         /*
1610         * For SLI2, we need to set port_state to READY
1611         * and continue discovery.
1612         */
1613         if (vport->port_state < LPFC_VPORT_READY) {
1614                 /* If we get here, there is nothing to ADISC */
1615                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1616                         lpfc_issue_clear_la(phba, vport);
1617                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1618                         vport->num_disc_nodes = 0;
1619                         /* go thru NPR list, issue ELS PLOGIs */
1620                         if (vport->fc_npr_cnt)
1621                                 lpfc_els_disc_plogi(vport);
1622                         if (!vport->num_disc_nodes) {
1623                                 spin_lock_irq(shost->host_lock);
1624                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1625                                 spin_unlock_irq(shost->host_lock);
1626                                 lpfc_can_disctmo(vport);
1627                                 lpfc_end_rscn(vport);
1628                         }
1629                 }
1630                 vport->port_state = LPFC_VPORT_READY;
1631         } else
1632                 lpfc_rscn_disc(vport);
1633 }
1634
1635 /**
1636  * lpfc_more_adisc: Issue more adisc as needed.
1637  * @vport: pointer to a host virtual N_Port data structure.
1638  *
1639  * This routine determines whether there are more ndlps on a @vport
1640  * node list need to have Address Discover (ADISC) issued. If so, it will
1641  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
1642  * remaining nodes which need to have ADISC sent.
1643  **/
1644 void
1645 lpfc_more_adisc(struct lpfc_vport *vport)
1646 {
1647         int sentadisc;
1648
1649         if (vport->num_disc_nodes)
1650                 vport->num_disc_nodes--;
1651         /* Continue discovery with <num_disc_nodes> ADISCs to go */
1652         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1653                          "0210 Continue discovery with %d ADISCs to go "
1654                          "Data: x%x x%x x%x\n",
1655                          vport->num_disc_nodes, vport->fc_adisc_cnt,
1656                          vport->fc_flag, vport->port_state);
1657         /* Check to see if there are more ADISCs to be sent */
1658         if (vport->fc_flag & FC_NLP_MORE) {
1659                 lpfc_set_disctmo(vport);
1660                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
1661                 sentadisc = lpfc_els_disc_adisc(vport);
1662         }
1663         if (!vport->num_disc_nodes)
1664                 lpfc_adisc_done(vport);
1665         return;
1666 }
1667
1668 /**
1669  * lpfc_cmpl_els_adisc: Completion callback function for adisc.
1670  * @phba: pointer to lpfc hba data structure.
1671  * @cmdiocb: pointer to lpfc command iocb data structure.
1672  * @rspiocb: pointer to lpfc response iocb data structure.
1673  *
1674  * This routine is the completion function for issuing the Address Discover
1675  * (ADISC) command. It first checks to see whether link went down during
1676  * the discovery process. If so, the node will be marked as node port
1677  * recovery for issuing discover IOCB by the link attention handler and
1678  * exit. Otherwise, the response status is checked. If error was reported
1679  * in the response status, the ADISC command shall be retried by invoking
1680  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
1681  * the response status, the state machine is invoked to set transition
1682  * with respect to NLP_EVT_CMPL_ADISC event.
1683  **/
1684 static void
1685 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1686                     struct lpfc_iocbq *rspiocb)
1687 {
1688         struct lpfc_vport *vport = cmdiocb->vport;
1689         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1690         IOCB_t *irsp;
1691         struct lpfc_nodelist *ndlp;
1692         int  disc;
1693
1694         /* we pass cmdiocb to state machine which needs rspiocb as well */
1695         cmdiocb->context_un.rsp_iocb = rspiocb;
1696
1697         irsp = &(rspiocb->iocb);
1698         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1699
1700         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1701                 "ADISC cmpl:      status:x%x/x%x did:x%x",
1702                 irsp->ulpStatus, irsp->un.ulpWord[4],
1703                 ndlp->nlp_DID);
1704
1705         /* Since ndlp can be freed in the disc state machine, note if this node
1706          * is being used during discovery.
1707          */
1708         spin_lock_irq(shost->host_lock);
1709         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1710         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1711         spin_unlock_irq(shost->host_lock);
1712         /* ADISC completes to NPort <nlp_DID> */
1713         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1714                          "0104 ADISC completes to NPort x%x "
1715                          "Data: x%x x%x x%x x%x x%x\n",
1716                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1717                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1718         /* Check to see if link went down during discovery */
1719         if (lpfc_els_chk_latt(vport)) {
1720                 spin_lock_irq(shost->host_lock);
1721                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1722                 spin_unlock_irq(shost->host_lock);
1723                 goto out;
1724         }
1725
1726         if (irsp->ulpStatus) {
1727                 /* Check for retry */
1728                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1729                         /* ELS command is being retried */
1730                         if (disc) {
1731                                 spin_lock_irq(shost->host_lock);
1732                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1733                                 spin_unlock_irq(shost->host_lock);
1734                                 lpfc_set_disctmo(vport);
1735                         }
1736                         goto out;
1737                 }
1738                 /* ADISC failed */
1739                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1740                 if (!lpfc_error_lost_link(irsp))
1741                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1742                                                 NLP_EVT_CMPL_ADISC);
1743         } else
1744                 /* Good status, call state machine */
1745                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1746                                         NLP_EVT_CMPL_ADISC);
1747
1748         /* Check to see if there are more ADISCs to be sent */
1749         if (disc && vport->num_disc_nodes)
1750                 lpfc_more_adisc(vport);
1751 out:
1752         lpfc_els_free_iocb(phba, cmdiocb);
1753         return;
1754 }
1755
1756 /**
1757  * lpfc_issue_els_adisc: Issue an address discover iocb to an node on a vport.
1758  * @vport: pointer to a virtual N_Port data structure.
1759  * @ndlp: pointer to a node-list data structure.
1760  * @retry: number of retries to the command IOCB.
1761  *
1762  * This routine issues an Address Discover (ADISC) for an @ndlp on a
1763  * @vport. It prepares the payload of the ADISC ELS command, updates the
1764  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
1765  * to issue the ADISC ELS command.
1766  *
1767  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1768  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1769  * will be stored into the context1 field of the IOCB for the completion
1770  * callback function to the ADISC ELS command.
1771  *
1772  * Return code
1773  *   0 - successfully issued adisc
1774  *   1 - failed to issue adisc
1775  **/
1776 int
1777 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1778                      uint8_t retry)
1779 {
1780         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1781         struct lpfc_hba  *phba = vport->phba;
1782         ADISC *ap;
1783         IOCB_t *icmd;
1784         struct lpfc_iocbq *elsiocb;
1785         struct lpfc_sli *psli = &phba->sli;
1786         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1787         uint8_t *pcmd;
1788         uint16_t cmdsize;
1789
1790         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
1791         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1792                                      ndlp->nlp_DID, ELS_CMD_ADISC);
1793         if (!elsiocb)
1794                 return 1;
1795
1796         icmd = &elsiocb->iocb;
1797         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1798
1799         /* For ADISC request, remainder of payload is service parameters */
1800         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
1801         pcmd += sizeof(uint32_t);
1802
1803         /* Fill in ADISC payload */
1804         ap = (ADISC *) pcmd;
1805         ap->hardAL_PA = phba->fc_pref_ALPA;
1806         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
1807         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
1808         ap->DID = be32_to_cpu(vport->fc_myDID);
1809
1810         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1811                 "Issue ADISC:     did:x%x",
1812                 ndlp->nlp_DID, 0, 0);
1813
1814         phba->fc_stat.elsXmitADISC++;
1815         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
1816         spin_lock_irq(shost->host_lock);
1817         ndlp->nlp_flag |= NLP_ADISC_SND;
1818         spin_unlock_irq(shost->host_lock);
1819         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1820                 spin_lock_irq(shost->host_lock);
1821                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
1822                 spin_unlock_irq(shost->host_lock);
1823                 lpfc_els_free_iocb(phba, elsiocb);
1824                 return 1;
1825         }
1826         return 0;
1827 }
1828
1829 /**
1830  * lpfc_cmpl_els_logo: Completion callback function for logo.
1831  * @phba: pointer to lpfc hba data structure.
1832  * @cmdiocb: pointer to lpfc command iocb data structure.
1833  * @rspiocb: pointer to lpfc response iocb data structure.
1834  *
1835  * This routine is the completion function for issuing the ELS Logout (LOGO)
1836  * command. If no error status was reported from the LOGO response, the
1837  * state machine of the associated ndlp shall be invoked for transition with
1838  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
1839  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
1840  **/
1841 static void
1842 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1843                    struct lpfc_iocbq *rspiocb)
1844 {
1845         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1846         struct lpfc_vport *vport = ndlp->vport;
1847         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1848         IOCB_t *irsp;
1849         struct lpfc_sli *psli;
1850
1851         psli = &phba->sli;
1852         /* we pass cmdiocb to state machine which needs rspiocb as well */
1853         cmdiocb->context_un.rsp_iocb = rspiocb;
1854
1855         irsp = &(rspiocb->iocb);
1856         spin_lock_irq(shost->host_lock);
1857         ndlp->nlp_flag &= ~NLP_LOGO_SND;
1858         spin_unlock_irq(shost->host_lock);
1859
1860         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1861                 "LOGO cmpl:       status:x%x/x%x did:x%x",
1862                 irsp->ulpStatus, irsp->un.ulpWord[4],
1863                 ndlp->nlp_DID);
1864         /* LOGO completes to NPort <nlp_DID> */
1865         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1866                          "0105 LOGO completes to NPort x%x "
1867                          "Data: x%x x%x x%x x%x\n",
1868                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1869                          irsp->ulpTimeout, vport->num_disc_nodes);
1870         /* Check to see if link went down during discovery */
1871         if (lpfc_els_chk_latt(vport))
1872                 goto out;
1873
1874         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
1875                 /* NLP_EVT_DEVICE_RM should unregister the RPI
1876                  * which should abort all outstanding IOs.
1877                  */
1878                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1879                                         NLP_EVT_DEVICE_RM);
1880                 goto out;
1881         }
1882
1883         if (irsp->ulpStatus) {
1884                 /* Check for retry */
1885                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1886                         /* ELS command is being retried */
1887                         goto out;
1888                 /* LOGO failed */
1889                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1890                 if (lpfc_error_lost_link(irsp))
1891                         goto out;
1892                 else
1893                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1894                                                 NLP_EVT_CMPL_LOGO);
1895         } else
1896                 /* Good status, call state machine.
1897                  * This will unregister the rpi if needed.
1898                  */
1899                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1900                                         NLP_EVT_CMPL_LOGO);
1901 out:
1902         lpfc_els_free_iocb(phba, cmdiocb);
1903         return;
1904 }
1905
1906 /**
1907  * lpfc_issue_els_logo: Issue a logo to an node on a vport.
1908  * @vport: pointer to a virtual N_Port data structure.
1909  * @ndlp: pointer to a node-list data structure.
1910  * @retry: number of retries to the command IOCB.
1911  *
1912  * This routine constructs and issues an ELS Logout (LOGO) iocb command
1913  * to a remote node, referred by an @ndlp on a @vport. It constructs the
1914  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
1915  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
1916  *
1917  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1918  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1919  * will be stored into the context1 field of the IOCB for the completion
1920  * callback function to the LOGO ELS command.
1921  *
1922  * Return code
1923  *   0 - successfully issued logo
1924  *   1 - failed to issue logo
1925  **/
1926 int
1927 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1928                     uint8_t retry)
1929 {
1930         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1931         struct lpfc_hba  *phba = vport->phba;
1932         IOCB_t *icmd;
1933         struct lpfc_iocbq *elsiocb;
1934         struct lpfc_sli_ring *pring;
1935         struct lpfc_sli *psli;
1936         uint8_t *pcmd;
1937         uint16_t cmdsize;
1938         int rc;
1939
1940         psli = &phba->sli;
1941         pring = &psli->ring[LPFC_ELS_RING];
1942
1943         spin_lock_irq(shost->host_lock);
1944         if (ndlp->nlp_flag & NLP_LOGO_SND) {
1945                 spin_unlock_irq(shost->host_lock);
1946                 return 0;
1947         }
1948         spin_unlock_irq(shost->host_lock);
1949
1950         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
1951         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1952                                      ndlp->nlp_DID, ELS_CMD_LOGO);
1953         if (!elsiocb)
1954                 return 1;
1955
1956         icmd = &elsiocb->iocb;
1957         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1958         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
1959         pcmd += sizeof(uint32_t);
1960
1961         /* Fill in LOGO payload */
1962         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
1963         pcmd += sizeof(uint32_t);
1964         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
1965
1966         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1967                 "Issue LOGO:      did:x%x",
1968                 ndlp->nlp_DID, 0, 0);
1969
1970         phba->fc_stat.elsXmitLOGO++;
1971         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
1972         spin_lock_irq(shost->host_lock);
1973         ndlp->nlp_flag |= NLP_LOGO_SND;
1974         spin_unlock_irq(shost->host_lock);
1975         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1976
1977         if (rc == IOCB_ERROR) {
1978                 spin_lock_irq(shost->host_lock);
1979                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
1980                 spin_unlock_irq(shost->host_lock);
1981                 lpfc_els_free_iocb(phba, elsiocb);
1982                 return 1;
1983         }
1984         return 0;
1985 }
1986
1987 /**
1988  * lpfc_cmpl_els_cmd: Completion callback function for generic els command.
1989  * @phba: pointer to lpfc hba data structure.
1990  * @cmdiocb: pointer to lpfc command iocb data structure.
1991  * @rspiocb: pointer to lpfc response iocb data structure.
1992  *
1993  * This routine is a generic completion callback function for ELS commands.
1994  * Specifically, it is the callback function which does not need to perform
1995  * any command specific operations. It is currently used by the ELS command
1996  * issuing routines for the ELS State Change  Request (SCR),
1997  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
1998  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
1999  * certain debug loggings, this callback function simply invokes the
2000  * lpfc_els_chk_latt() routine to check whether link went down during the
2001  * discovery process.
2002  **/
2003 static void
2004 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2005                   struct lpfc_iocbq *rspiocb)
2006 {
2007         struct lpfc_vport *vport = cmdiocb->vport;
2008         IOCB_t *irsp;
2009
2010         irsp = &rspiocb->iocb;
2011
2012         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2013                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
2014                 irsp->ulpStatus, irsp->un.ulpWord[4],
2015                 irsp->un.elsreq64.remoteID);
2016         /* ELS cmd tag <ulpIoTag> completes */
2017         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2018                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2019                          irsp->ulpIoTag, irsp->ulpStatus,
2020                          irsp->un.ulpWord[4], irsp->ulpTimeout);
2021         /* Check to see if link went down during discovery */
2022         lpfc_els_chk_latt(vport);
2023         lpfc_els_free_iocb(phba, cmdiocb);
2024         return;
2025 }
2026
2027 /**
2028  * lpfc_issue_els_scr: Issue a scr to an node on a vport.
2029  * @vport: pointer to a host virtual N_Port data structure.
2030  * @nportid: N_Port identifier to the remote node.
2031  * @retry: number of retries to the command IOCB.
2032  *
2033  * This routine issues a State Change Request (SCR) to a fabric node
2034  * on a @vport. The remote node @nportid is passed into the function. It
2035  * first search the @vport node list to find the matching ndlp. If no such
2036  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2037  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2038  * routine is invoked to send the SCR IOCB.
2039  *
2040  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2041  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2042  * will be stored into the context1 field of the IOCB for the completion
2043  * callback function to the SCR ELS command.
2044  *
2045  * Return code
2046  *   0 - Successfully issued scr command
2047  *   1 - Failed to issue scr command
2048  **/
2049 int
2050 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2051 {
2052         struct lpfc_hba  *phba = vport->phba;
2053         IOCB_t *icmd;
2054         struct lpfc_iocbq *elsiocb;
2055         struct lpfc_sli_ring *pring;
2056         struct lpfc_sli *psli;
2057         uint8_t *pcmd;
2058         uint16_t cmdsize;
2059         struct lpfc_nodelist *ndlp;
2060
2061         psli = &phba->sli;
2062         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2063         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2064
2065         ndlp = lpfc_findnode_did(vport, nportid);
2066         if (!ndlp) {
2067                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2068                 if (!ndlp)
2069                         return 1;
2070                 lpfc_nlp_init(vport, ndlp, nportid);
2071                 lpfc_enqueue_node(vport, ndlp);
2072         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2073                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2074                 if (!ndlp)
2075                         return 1;
2076         }
2077
2078         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2079                                      ndlp->nlp_DID, ELS_CMD_SCR);
2080
2081         if (!elsiocb) {
2082                 /* This will trigger the release of the node just
2083                  * allocated
2084                  */
2085                 lpfc_nlp_put(ndlp);
2086                 return 1;
2087         }
2088
2089         icmd = &elsiocb->iocb;
2090         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2091
2092         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2093         pcmd += sizeof(uint32_t);
2094
2095         /* For SCR, remainder of payload is SCR parameter page */
2096         memset(pcmd, 0, sizeof(SCR));
2097         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2098
2099         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2100                 "Issue SCR:       did:x%x",
2101                 ndlp->nlp_DID, 0, 0);
2102
2103         phba->fc_stat.elsXmitSCR++;
2104         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2105         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
2106                 /* The additional lpfc_nlp_put will cause the following
2107                  * lpfc_els_free_iocb routine to trigger the rlease of
2108                  * the node.
2109                  */
2110                 lpfc_nlp_put(ndlp);
2111                 lpfc_els_free_iocb(phba, elsiocb);
2112                 return 1;
2113         }
2114         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2115          * trigger the release of node.
2116          */
2117         lpfc_nlp_put(ndlp);
2118         return 0;
2119 }
2120
2121 /**
2122  * lpfc_issue_els_farpr: Issue a farp to an node on a vport.
2123  * @vport: pointer to a host virtual N_Port data structure.
2124  * @nportid: N_Port identifier to the remote node.
2125  * @retry: number of retries to the command IOCB.
2126  *
2127  * This routine issues a Fibre Channel Address Resolution Response
2128  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2129  * is passed into the function. It first search the @vport node list to find
2130  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2131  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2132  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2133  *
2134  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2135  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2136  * will be stored into the context1 field of the IOCB for the completion
2137  * callback function to the PARPR ELS command.
2138  *
2139  * Return code
2140  *   0 - Successfully issued farpr command
2141  *   1 - Failed to issue farpr command
2142  **/
2143 static int
2144 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2145 {
2146         struct lpfc_hba  *phba = vport->phba;
2147         IOCB_t *icmd;
2148         struct lpfc_iocbq *elsiocb;
2149         struct lpfc_sli_ring *pring;
2150         struct lpfc_sli *psli;
2151         FARP *fp;
2152         uint8_t *pcmd;
2153         uint32_t *lp;
2154         uint16_t cmdsize;
2155         struct lpfc_nodelist *ondlp;
2156         struct lpfc_nodelist *ndlp;
2157
2158         psli = &phba->sli;
2159         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2160         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
2161
2162         ndlp = lpfc_findnode_did(vport, nportid);
2163         if (!ndlp) {
2164                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2165                 if (!ndlp)
2166                         return 1;
2167                 lpfc_nlp_init(vport, ndlp, nportid);
2168                 lpfc_enqueue_node(vport, ndlp);
2169         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2170                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2171                 if (!ndlp)
2172                         return 1;
2173         }
2174
2175         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2176                                      ndlp->nlp_DID, ELS_CMD_RNID);
2177         if (!elsiocb) {
2178                 /* This will trigger the release of the node just
2179                  * allocated
2180                  */
2181                 lpfc_nlp_put(ndlp);
2182                 return 1;
2183         }
2184
2185         icmd = &elsiocb->iocb;
2186         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2187
2188         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
2189         pcmd += sizeof(uint32_t);
2190
2191         /* Fill in FARPR payload */
2192         fp = (FARP *) (pcmd);
2193         memset(fp, 0, sizeof(FARP));
2194         lp = (uint32_t *) pcmd;
2195         *lp++ = be32_to_cpu(nportid);
2196         *lp++ = be32_to_cpu(vport->fc_myDID);
2197         fp->Rflags = 0;
2198         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2199
2200         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2201         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2202         ondlp = lpfc_findnode_did(vport, nportid);
2203         if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
2204                 memcpy(&fp->OportName, &ondlp->nlp_portname,
2205                        sizeof(struct lpfc_name));
2206                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
2207                        sizeof(struct lpfc_name));
2208         }
2209
2210         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2211                 "Issue FARPR:     did:x%x",
2212                 ndlp->nlp_DID, 0, 0);
2213
2214         phba->fc_stat.elsXmitFARPR++;
2215         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2216         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
2217                 /* The additional lpfc_nlp_put will cause the following
2218                  * lpfc_els_free_iocb routine to trigger the release of
2219                  * the node.
2220                  */
2221                 lpfc_nlp_put(ndlp);
2222                 lpfc_els_free_iocb(phba, elsiocb);
2223                 return 1;
2224         }
2225         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2226          * trigger the release of the node.
2227          */
2228         lpfc_nlp_put(ndlp);
2229         return 0;
2230 }
2231
2232 /**
2233  * lpfc_cancel_retry_delay_tmo: Cancel the timer with delayed iocb-cmd retry.
2234  * @vport: pointer to a host virtual N_Port data structure.
2235  * @nlp: pointer to a node-list data structure.
2236  *
2237  * This routine cancels the timer with a delayed IOCB-command retry for
2238  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2239  * removes the ELS retry event if it presents. In addition, if the
2240  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2241  * commands are sent for the @vport's nodes that require issuing discovery
2242  * ADISC.
2243  **/
2244 void
2245 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
2246 {
2247         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2248         struct lpfc_work_evt *evtp;
2249
2250         if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2251                 return;
2252         spin_lock_irq(shost->host_lock);
2253         nlp->nlp_flag &= ~NLP_DELAY_TMO;
2254         spin_unlock_irq(shost->host_lock);
2255         del_timer_sync(&nlp->nlp_delayfunc);
2256         nlp->nlp_last_elscmd = 0;
2257         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
2258                 list_del_init(&nlp->els_retry_evt.evt_listp);
2259                 /* Decrement nlp reference count held for the delayed retry */
2260                 evtp = &nlp->els_retry_evt;
2261                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2262         }
2263         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2264                 spin_lock_irq(shost->host_lock);
2265                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2266                 spin_unlock_irq(shost->host_lock);
2267                 if (vport->num_disc_nodes) {
2268                         if (vport->port_state < LPFC_VPORT_READY) {
2269                                 /* Check if there are more ADISCs to be sent */
2270                                 lpfc_more_adisc(vport);
2271                         } else {
2272                                 /* Check if there are more PLOGIs to be sent */
2273                                 lpfc_more_plogi(vport);
2274                                 if (vport->num_disc_nodes == 0) {
2275                                         spin_lock_irq(shost->host_lock);
2276                                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
2277                                         spin_unlock_irq(shost->host_lock);
2278                                         lpfc_can_disctmo(vport);
2279                                         lpfc_end_rscn(vport);
2280                                 }
2281                         }
2282                 }
2283         }
2284         return;
2285 }
2286
2287 /**
2288  * lpfc_els_retry_delay: Timer function with a ndlp delayed function timer.
2289  * @ptr: holder for the pointer to the timer function associated data (ndlp).
2290  *
2291  * This routine is invoked by the ndlp delayed-function timer to check
2292  * whether there is any pending ELS retry event(s) with the node. If not, it
2293  * simply returns. Otherwise, if there is at least one ELS delayed event, it
2294  * adds the delayed events to the HBA work list and invokes the
2295  * lpfc_worker_wake_up() routine to wake up worker thread to process the
2296  * event. Note that lpfc_nlp_get() is called before posting the event to
2297  * the work list to hold reference count of ndlp so that it guarantees the
2298  * reference to ndlp will still be available when the worker thread gets
2299  * to the event associated with the ndlp.
2300  **/
2301 void
2302 lpfc_els_retry_delay(unsigned long ptr)
2303 {
2304         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2305         struct lpfc_vport *vport = ndlp->vport;
2306         struct lpfc_hba   *phba = vport->phba;
2307         unsigned long flags;
2308         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
2309
2310         spin_lock_irqsave(&phba->hbalock, flags);
2311         if (!list_empty(&evtp->evt_listp)) {
2312                 spin_unlock_irqrestore(&phba->hbalock, flags);
2313                 return;
2314         }
2315
2316         /* We need to hold the node by incrementing the reference
2317          * count until the queued work is done
2318          */
2319         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
2320         if (evtp->evt_arg1) {
2321                 evtp->evt = LPFC_EVT_ELS_RETRY;
2322                 list_add_tail(&evtp->evt_listp, &phba->work_list);
2323                 lpfc_worker_wake_up(phba);
2324         }
2325         spin_unlock_irqrestore(&phba->hbalock, flags);
2326         return;
2327 }
2328
2329 /**
2330  * lpfc_els_retry_delay_handler: Work thread handler for ndlp delayed function.
2331  * @ndlp: pointer to a node-list data structure.
2332  *
2333  * This routine is the worker-thread handler for processing the @ndlp delayed
2334  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2335  * the last ELS command from the associated ndlp and invokes the proper ELS
2336  * function according to the delayed ELS command to retry the command.
2337  **/
2338 void
2339 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
2340 {
2341         struct lpfc_vport *vport = ndlp->vport;
2342         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2343         uint32_t cmd, did, retry;
2344
2345         spin_lock_irq(shost->host_lock);
2346         did = ndlp->nlp_DID;
2347         cmd = ndlp->nlp_last_elscmd;
2348         ndlp->nlp_last_elscmd = 0;
2349
2350         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2351                 spin_unlock_irq(shost->host_lock);
2352                 return;
2353         }
2354
2355         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2356         spin_unlock_irq(shost->host_lock);
2357         /*
2358          * If a discovery event readded nlp_delayfunc after timer
2359          * firing and before processing the timer, cancel the
2360          * nlp_delayfunc.
2361          */
2362         del_timer_sync(&ndlp->nlp_delayfunc);
2363         retry = ndlp->nlp_retry;
2364
2365         switch (cmd) {
2366         case ELS_CMD_FLOGI:
2367                 lpfc_issue_els_flogi(vport, ndlp, retry);
2368                 break;
2369         case ELS_CMD_PLOGI:
2370                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
2371                         ndlp->nlp_prev_state = ndlp->nlp_state;
2372                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2373                 }
2374                 break;
2375         case ELS_CMD_ADISC:
2376                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
2377                         ndlp->nlp_prev_state = ndlp->nlp_state;
2378                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2379                 }
2380                 break;
2381         case ELS_CMD_PRLI:
2382                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
2383                         ndlp->nlp_prev_state = ndlp->nlp_state;
2384                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2385                 }
2386                 break;
2387         case ELS_CMD_LOGO:
2388                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
2389                         ndlp->nlp_prev_state = ndlp->nlp_state;
2390                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2391                 }
2392                 break;
2393         case ELS_CMD_FDISC:
2394                 lpfc_issue_els_fdisc(vport, ndlp, retry);
2395                 break;
2396         }
2397         return;
2398 }
2399
2400 /**
2401  * lpfc_els_retry: Make retry decision on an els command iocb.
2402  * @phba: pointer to lpfc hba data structure.
2403  * @cmdiocb: pointer to lpfc command iocb data structure.
2404  * @rspiocb: pointer to lpfc response iocb data structure.
2405  *
2406  * This routine makes a retry decision on an ELS command IOCB, which has
2407  * failed. The following ELS IOCBs use this function for retrying the command
2408  * when previously issued command responsed with error status: FLOGI, PLOGI,
2409  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
2410  * returned error status, it makes the decision whether a retry shall be
2411  * issued for the command, and whether a retry shall be made immediately or
2412  * delayed. In the former case, the corresponding ELS command issuing-function
2413  * is called to retry the command. In the later case, the ELS command shall
2414  * be posted to the ndlp delayed event and delayed function timer set to the
2415  * ndlp for the delayed command issusing.
2416  *
2417  * Return code
2418  *   0 - No retry of els command is made
2419  *   1 - Immediate or delayed retry of els command is made
2420  **/
2421 static int
2422 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2423                struct lpfc_iocbq *rspiocb)
2424 {
2425         struct lpfc_vport *vport = cmdiocb->vport;
2426         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2427         IOCB_t *irsp = &rspiocb->iocb;
2428         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2429         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2430         uint32_t *elscmd;
2431         struct ls_rjt stat;
2432         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
2433         int logerr = 0;
2434         uint32_t cmd = 0;
2435         uint32_t did;
2436
2437
2438         /* Note: context2 may be 0 for internal driver abort
2439          * of delays ELS command.
2440          */
2441
2442         if (pcmd && pcmd->virt) {
2443                 elscmd = (uint32_t *) (pcmd->virt);
2444                 cmd = *elscmd++;
2445         }
2446
2447         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2448                 did = ndlp->nlp_DID;
2449         else {
2450                 /* We should only hit this case for retrying PLOGI */
2451                 did = irsp->un.elsreq64.remoteID;
2452                 ndlp = lpfc_findnode_did(vport, did);
2453                 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2454                     && (cmd != ELS_CMD_PLOGI))
2455                         return 1;
2456         }
2457
2458         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2459                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
2460                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
2461
2462         switch (irsp->ulpStatus) {
2463         case IOSTAT_FCP_RSP_ERROR:
2464         case IOSTAT_REMOTE_STOP:
2465                 break;
2466
2467         case IOSTAT_LOCAL_REJECT:
2468                 switch ((irsp->un.ulpWord[4] & 0xff)) {
2469                 case IOERR_LOOP_OPEN_FAILURE:
2470                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
2471                                 delay = 1000;
2472                         retry = 1;
2473                         break;
2474
2475                 case IOERR_ILLEGAL_COMMAND:
2476                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2477                                          "0124 Retry illegal cmd x%x "
2478                                          "retry:x%x delay:x%x\n",
2479                                          cmd, cmdiocb->retry, delay);
2480                         retry = 1;
2481                         /* All command's retry policy */
2482                         maxretry = 8;
2483                         if (cmdiocb->retry > 2)
2484                                 delay = 1000;
2485                         break;
2486
2487                 case IOERR_NO_RESOURCES:
2488                         logerr = 1; /* HBA out of resources */
2489                         retry = 1;
2490                         if (cmdiocb->retry > 100)
2491                                 delay = 100;
2492                         maxretry = 250;
2493                         break;
2494
2495                 case IOERR_ILLEGAL_FRAME:
2496                         delay = 100;
2497                         retry = 1;
2498                         break;
2499
2500                 case IOERR_SEQUENCE_TIMEOUT:
2501                 case IOERR_INVALID_RPI:
2502                         retry = 1;
2503                         break;
2504                 }
2505                 break;
2506
2507         case IOSTAT_NPORT_RJT:
2508         case IOSTAT_FABRIC_RJT:
2509                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
2510                         retry = 1;
2511                         break;
2512                 }
2513                 break;
2514
2515         case IOSTAT_NPORT_BSY:
2516         case IOSTAT_FABRIC_BSY:
2517                 logerr = 1; /* Fabric / Remote NPort out of resources */
2518                 retry = 1;
2519                 break;
2520
2521         case IOSTAT_LS_RJT:
2522                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
2523                 /* Added for Vendor specifc support
2524                  * Just keep retrying for these Rsn / Exp codes
2525                  */
2526                 switch (stat.un.b.lsRjtRsnCode) {
2527                 case LSRJT_UNABLE_TPC:
2528                         if (stat.un.b.lsRjtRsnCodeExp ==
2529                             LSEXP_CMD_IN_PROGRESS) {
2530                                 if (cmd == ELS_CMD_PLOGI) {
2531                                         delay = 1000;
2532                                         maxretry = 48;
2533                                 }
2534                                 retry = 1;
2535                                 break;
2536                         }
2537                         if (cmd == ELS_CMD_PLOGI) {
2538                                 delay = 1000;
2539                                 maxretry = lpfc_max_els_tries + 1;
2540                                 retry = 1;
2541                                 break;
2542                         }
2543                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2544                           (cmd == ELS_CMD_FDISC) &&
2545                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
2546                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2547                                                  "0125 FDISC Failed (x%x). "
2548                                                  "Fabric out of resources\n",
2549                                                  stat.un.lsRjtError);
2550                                 lpfc_vport_set_state(vport,
2551                                                      FC_VPORT_NO_FABRIC_RSCS);
2552                         }
2553                         break;
2554
2555                 case LSRJT_LOGICAL_BSY:
2556                         if ((cmd == ELS_CMD_PLOGI) ||
2557                             (cmd == ELS_CMD_PRLI)) {
2558                                 delay = 1000;
2559                                 maxretry = 48;
2560                         } else if (cmd == ELS_CMD_FDISC) {
2561                                 /* FDISC retry policy */
2562                                 maxretry = 48;
2563                                 if (cmdiocb->retry >= 32)
2564                                         delay = 1000;
2565                         }
2566                         retry = 1;
2567                         break;
2568
2569                 case LSRJT_LOGICAL_ERR:
2570                         /* There are some cases where switches return this
2571                          * error when they are not ready and should be returning
2572                          * Logical Busy. We should delay every time.
2573                          */
2574                         if (cmd == ELS_CMD_FDISC &&
2575                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
2576                                 maxretry = 3;
2577                                 delay = 1000;
2578                                 retry = 1;
2579                                 break;
2580                         }
2581                 case LSRJT_PROTOCOL_ERR:
2582                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2583                           (cmd == ELS_CMD_FDISC) &&
2584                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
2585                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
2586                           ) {
2587                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2588                                                  "0122 FDISC Failed (x%x). "
2589                                                  "Fabric Detected Bad WWN\n",
2590                                                  stat.un.lsRjtError);
2591                                 lpfc_vport_set_state(vport,
2592                                                      FC_VPORT_FABRIC_REJ_WWN);
2593                         }
2594                         break;
2595                 }
2596                 break;
2597
2598         case IOSTAT_INTERMED_RSP:
2599         case IOSTAT_BA_RJT:
2600                 break;
2601
2602         default:
2603                 break;
2604         }
2605
2606         if (did == FDMI_DID)
2607                 retry = 1;
2608
2609         if ((cmd == ELS_CMD_FLOGI) &&
2610             (phba->fc_topology != TOPOLOGY_LOOP) &&
2611             !lpfc_error_lost_link(irsp)) {
2612                 /* FLOGI retry policy */
2613                 retry = 1;
2614                 maxretry = 48;
2615                 if (cmdiocb->retry >= 32)
2616                         delay = 1000;
2617         }
2618
2619         if ((++cmdiocb->retry) >= maxretry) {
2620                 phba->fc_stat.elsRetryExceeded++;
2621                 retry = 0;
2622         }
2623
2624         if ((vport->load_flag & FC_UNLOADING) != 0)
2625                 retry = 0;
2626
2627         if (retry) {
2628
2629                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
2630                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2631                                  "0107 Retry ELS command x%x to remote "
2632                                  "NPORT x%x Data: x%x x%x\n",
2633                                  cmd, did, cmdiocb->retry, delay);
2634
2635                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
2636                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
2637                         ((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
2638                         /* Don't reset timer for no resources */
2639
2640                         /* If discovery / RSCN timer is running, reset it */
2641                         if (timer_pending(&vport->fc_disctmo) ||
2642                             (vport->fc_flag & FC_RSCN_MODE))
2643                                 lpfc_set_disctmo(vport);
2644                 }
2645
2646                 phba->fc_stat.elsXmitRetry++;
2647                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
2648                         phba->fc_stat.elsDelayRetry++;
2649                         ndlp->nlp_retry = cmdiocb->retry;
2650
2651                         /* delay is specified in milliseconds */
2652                         mod_timer(&ndlp->nlp_delayfunc,
2653                                 jiffies + msecs_to_jiffies(delay));
2654                         spin_lock_irq(shost->host_lock);
2655                         ndlp->nlp_flag |= NLP_DELAY_TMO;
2656                         spin_unlock_irq(shost->host_lock);
2657
2658                         ndlp->nlp_prev_state = ndlp->nlp_state;
2659                         if (cmd == ELS_CMD_PRLI)
2660                                 lpfc_nlp_set_state(vport, ndlp,
2661                                         NLP_STE_REG_LOGIN_ISSUE);
2662                         else
2663                                 lpfc_nlp_set_state(vport, ndlp,
2664                                         NLP_STE_NPR_NODE);
2665                         ndlp->nlp_last_elscmd = cmd;
2666
2667                         return 1;
2668                 }
2669                 switch (cmd) {
2670                 case ELS_CMD_FLOGI:
2671                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2672                         return 1;
2673                 case ELS_CMD_FDISC:
2674                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
2675                         return 1;
2676                 case ELS_CMD_PLOGI:
2677                         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2678                                 ndlp->nlp_prev_state = ndlp->nlp_state;
2679                                 lpfc_nlp_set_state(vport, ndlp,
2680                                                    NLP_STE_PLOGI_ISSUE);
2681                         }
2682                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2683                         return 1;
2684                 case ELS_CMD_ADISC:
2685                         ndlp->nlp_prev_state = ndlp->nlp_state;
2686                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2687                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2688                         return 1;
2689                 case ELS_CMD_PRLI:
2690                         ndlp->nlp_prev_state = ndlp->nlp_state;
2691                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2692                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2693                         return 1;
2694                 case ELS_CMD_LOGO:
2695                         ndlp->nlp_prev_state = ndlp->nlp_state;
2696                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2697                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2698                         return 1;
2699                 }
2700         }
2701         /* No retry ELS command <elsCmd> to remote NPORT <did> */
2702         if (logerr) {
2703                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2704                          "0137 No retry ELS command x%x to remote "
2705                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
2706                          cmd, did, irsp->ulpStatus,
2707                          irsp->un.ulpWord[4]);
2708         }
2709         else {
2710                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2711                          "0108 No retry ELS command x%x to remote "
2712                          "NPORT x%x Retried:%d Error:x%x/%x\n",
2713                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
2714                          irsp->un.ulpWord[4]);
2715         }
2716         return 0;
2717 }
2718
2719 /**
2720  * lpfc_els_free_data: Free lpfc dma buffer and data structure with an iocb.
2721  * @phba: pointer to lpfc hba data structure.
2722  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
2723  *
2724  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
2725  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
2726  * checks to see whether there is a lpfc DMA buffer associated with the
2727  * response of the command IOCB. If so, it will be released before releasing
2728  * the lpfc DMA buffer associated with the IOCB itself.
2729  *
2730  * Return code
2731  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2732  **/
2733 static int
2734 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
2735 {
2736         struct lpfc_dmabuf *buf_ptr;
2737
2738         /* Free the response before processing the command. */
2739         if (!list_empty(&buf_ptr1->list)) {
2740                 list_remove_head(&buf_ptr1->list, buf_ptr,
2741                                  struct lpfc_dmabuf,
2742                                  list);
2743                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2744                 kfree(buf_ptr);
2745         }
2746         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2747         kfree(buf_ptr1);
2748         return 0;
2749 }
2750
2751 /**
2752  * lpfc_els_free_bpl: Free lpfc dma buffer and data structure with bpl.
2753  * @phba: pointer to lpfc hba data structure.
2754  * @buf_ptr: pointer to the lpfc dma buffer data structure.
2755  *
2756  * This routine releases the lpfc Direct Memory Access (DMA) buffer
2757  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
2758  * pool.
2759  *
2760  * Return code
2761  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2762  **/
2763 static int
2764 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
2765 {
2766         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2767         kfree(buf_ptr);
2768         return 0;
2769 }
2770
2771 /**
2772  * lpfc_els_free_iocb: Free a command iocb and its associated resources.
2773  * @phba: pointer to lpfc hba data structure.
2774  * @elsiocb: pointer to lpfc els command iocb data structure.
2775  *
2776  * This routine frees a command IOCB and its associated resources. The
2777  * command IOCB data structure contains the reference to various associated
2778  * resources, these fields must be set to NULL if the associated reference
2779  * not present:
2780  *   context1 - reference to ndlp
2781  *   context2 - reference to cmd
2782  *   context2->next - reference to rsp
2783  *   context3 - reference to bpl
2784  *
2785  * It first properly decrements the reference count held on ndlp for the
2786  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
2787  * set, it invokes the lpfc_els_free_data() routine to release the Direct
2788  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
2789  * adds the DMA buffer the @phba data structure for the delayed release.
2790  * If reference to the Buffer Pointer List (BPL) is present, the
2791  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
2792  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
2793  * invoked to release the IOCB data structure back to @phba IOCBQ list.
2794  *
2795  * Return code
2796  *   0 - Success (currently, always return 0)
2797  **/
2798 int
2799 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
2800 {
2801         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2802         struct lpfc_nodelist *ndlp;
2803
2804         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
2805         if (ndlp) {
2806                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
2807                         lpfc_nlp_put(ndlp);
2808
2809                         /* If the ndlp is not being used by another discovery
2810                          * thread, free it.
2811                          */
2812                         if (!lpfc_nlp_not_used(ndlp)) {
2813                                 /* If ndlp is being used by another discovery
2814                                  * thread, just clear NLP_DEFER_RM
2815                                  */
2816                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
2817                         }
2818                 }
2819                 else
2820                         lpfc_nlp_put(ndlp);
2821                 elsiocb->context1 = NULL;
2822         }
2823         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
2824         if (elsiocb->context2) {
2825                 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
2826                         /* Firmware could still be in progress of DMAing
2827                          * payload, so don't free data buffer till after
2828                          * a hbeat.
2829                          */
2830                         elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
2831                         buf_ptr = elsiocb->context2;
2832                         elsiocb->context2 = NULL;
2833                         if (buf_ptr) {
2834                                 buf_ptr1 = NULL;
2835                                 spin_lock_irq(&phba->hbalock);
2836                                 if (!list_empty(&buf_ptr->list)) {
2837                                         list_remove_head(&buf_ptr->list,
2838                                                 buf_ptr1, struct lpfc_dmabuf,
2839                                                 list);
2840                                         INIT_LIST_HEAD(&buf_ptr1->list);
2841                                         list_add_tail(&buf_ptr1->list,
2842                                                 &phba->elsbuf);
2843                                         phba->elsbuf_cnt++;
2844                                 }
2845                                 INIT_LIST_HEAD(&buf_ptr->list);
2846                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
2847                                 phba->elsbuf_cnt++;
2848                                 spin_unlock_irq(&phba->hbalock);
2849                         }
2850                 } else {
2851                         buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
2852                         lpfc_els_free_data(phba, buf_ptr1);
2853                 }
2854         }
2855
2856         if (elsiocb->context3) {
2857                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2858                 lpfc_els_free_bpl(phba, buf_ptr);
2859         }
2860         lpfc_sli_release_iocbq(phba, elsiocb);
2861         return 0;
2862 }
2863
2864 /**
2865  * lpfc_cmpl_els_logo_acc: Completion callback function to logo acc response.
2866  * @phba: pointer to lpfc hba data structure.
2867  * @cmdiocb: pointer to lpfc command iocb data structure.
2868  * @rspiocb: pointer to lpfc response iocb data structure.
2869  *
2870  * This routine is the completion callback function to the Logout (LOGO)
2871  * Accept (ACC) Response ELS command. This routine is invoked to indicate
2872  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
2873  * release the ndlp if it has the last reference remaining (reference count
2874  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
2875  * field to NULL to inform the following lpfc_els_free_iocb() routine no
2876  * ndlp reference count needs to be decremented. Otherwise, the ndlp
2877  * reference use-count shall be decremented by the lpfc_els_free_iocb()
2878  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
2879  * IOCB data structure.
2880  **/
2881 static void
2882 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2883                        struct lpfc_iocbq *rspiocb)
2884 {
2885         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2886         struct lpfc_vport *vport = cmdiocb->vport;
2887         IOCB_t *irsp;
2888
2889         irsp = &rspiocb->iocb;
2890         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2891                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
2892                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
2893         /* ACC to LOGO completes to NPort <nlp_DID> */
2894         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2895                          "0109 ACC to LOGO completes to NPort x%x "
2896                          "Data: x%x x%x x%x\n",
2897                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2898                          ndlp->nlp_rpi);
2899
2900         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
2901                 /* NPort Recovery mode or node is just allocated */
2902                 if (!lpfc_nlp_not_used(ndlp)) {
2903                         /* If the ndlp is being used by another discovery
2904                          * thread, just unregister the RPI.
2905                          */
2906                         lpfc_unreg_rpi(vport, ndlp);
2907                 } else {
2908                         /* Indicate the node has already released, should
2909                          * not reference to it from within lpfc_els_free_iocb.
2910                          */
2911                         cmdiocb->context1 = NULL;
2912                 }
2913         }
2914         lpfc_els_free_iocb(phba, cmdiocb);
2915         return;
2916 }
2917
2918 /**
2919  * lpfc_mbx_cmpl_dflt_rpi: Completion callbk func for unreg dflt rpi mbox cmd.
2920  * @phba: pointer to lpfc hba data structure.
2921  * @pmb: pointer to the driver internal queue element for mailbox command.
2922  *
2923  * This routine is the completion callback function for unregister default
2924  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
2925  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
2926  * decrements the ndlp reference count held for this completion callback
2927  * function. After that, it invokes the lpfc_nlp_not_used() to check
2928  * whether there is only one reference left on the ndlp. If so, it will
2929  * perform one more decrement and trigger the release of the ndlp.
2930  **/
2931 void
2932 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2933 {
2934         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2935         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2936
2937         pmb->context1 = NULL;
2938         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2939         kfree(mp);
2940         mempool_free(pmb, phba->mbox_mem_pool);
2941         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2942                 lpfc_nlp_put(ndlp);
2943                 /* This is the end of the default RPI cleanup logic for this
2944                  * ndlp. If no other discovery threads are using this ndlp.
2945                  * we should free all resources associated with it.
2946                  */
2947                 lpfc_nlp_not_used(ndlp);
2948         }
2949         return;
2950 }
2951
2952 /**
2953  * lpfc_cmpl_els_rsp: Completion callback function for els response iocb cmd.
2954  * @phba: pointer to lpfc hba data structure.
2955  * @cmdiocb: pointer to lpfc command iocb data structure.
2956  * @rspiocb: pointer to lpfc response iocb data structure.
2957  *
2958  * This routine is the completion callback function for ELS Response IOCB
2959  * command. In normal case, this callback function just properly sets the
2960  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
2961  * field in the command IOCB is not NULL, the referred mailbox command will
2962  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
2963  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
2964  * link down event occurred during the discovery, the lpfc_nlp_not_used()
2965  * routine shall be invoked trying to release the ndlp if no other threads
2966  * are currently referring it.
2967  **/
2968 static void
2969 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2970                   struct lpfc_iocbq *rspiocb)
2971 {
2972         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2973         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
2974         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
2975         IOCB_t  *irsp;
2976         uint8_t *pcmd;
2977         LPFC_MBOXQ_t *mbox = NULL;
2978         struct lpfc_dmabuf *mp = NULL;
2979         uint32_t ls_rjt = 0;
2980
2981         irsp = &rspiocb->iocb;
2982
2983         if (cmdiocb->context_un.mbox)
2984                 mbox = cmdiocb->context_un.mbox;
2985
2986         /* First determine if this is a LS_RJT cmpl. Note, this callback
2987          * function can have cmdiocb->contest1 (ndlp) field set to NULL.
2988          */
2989         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
2990         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
2991             (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
2992                 /* A LS_RJT associated with Default RPI cleanup has its own
2993                  * seperate code path.
2994                  */
2995                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
2996                         ls_rjt = 1;
2997         }
2998
2999         /* Check to see if link went down during discovery */
3000         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
3001                 if (mbox) {
3002                         mp = (struct lpfc_dmabuf *) mbox->context1;
3003                         if (mp) {
3004                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3005                                 kfree(mp);
3006                         }
3007                         mempool_free(mbox, phba->mbox_mem_pool);
3008                 }
3009                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3010                     (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3011                         if (lpfc_nlp_not_used(ndlp)) {
3012                                 ndlp = NULL;
3013                                 /* Indicate the node has already released,
3014                                  * should not reference to it from within
3015                                  * the routine lpfc_els_free_iocb.
3016                                  */
3017                                 cmdiocb->context1 = NULL;
3018                         }
3019                 goto out;
3020         }
3021
3022         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3023                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
3024                 irsp->ulpStatus, irsp->un.ulpWord[4],
3025                 cmdiocb->iocb.un.elsreq64.remoteID);
3026         /* ELS response tag <ulpIoTag> completes */
3027         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3028                          "0110 ELS response tag x%x completes "
3029                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3030                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3031                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3032                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3033                          ndlp->nlp_rpi);
3034         if (mbox) {
3035                 if ((rspiocb->iocb.ulpStatus == 0)
3036                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
3037                         lpfc_unreg_rpi(vport, ndlp);
3038                         /* Increment reference count to ndlp to hold the
3039                          * reference to ndlp for the callback function.
3040                          */
3041                         mbox->context2 = lpfc_nlp_get(ndlp);
3042                         mbox->vport = vport;
3043                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3044                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3045                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3046                         }
3047                         else {
3048                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3049                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3050                                 lpfc_nlp_set_state(vport, ndlp,
3051                                            NLP_STE_REG_LOGIN_ISSUE);
3052                         }
3053                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
3054                             != MBX_NOT_FINISHED)
3055                                 goto out;
3056                         else
3057                                 /* Decrement the ndlp reference count we
3058                                  * set for this failed mailbox command.
3059                                  */
3060                                 lpfc_nlp_put(ndlp);
3061
3062                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
3063                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3064                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
3065                                 "Data: x%x x%x x%x\n",
3066                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3067                                 ndlp->nlp_rpi);
3068
3069                         if (lpfc_nlp_not_used(ndlp)) {
3070                                 ndlp = NULL;
3071                                 /* Indicate node has already been released,
3072                                  * should not reference to it from within
3073                                  * the routine lpfc_els_free_iocb.
3074                                  */
3075                                 cmdiocb->context1 = NULL;
3076                         }
3077                 } else {
3078                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
3079                         if (!lpfc_error_lost_link(irsp) &&
3080                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
3081                                 if (lpfc_nlp_not_used(ndlp)) {
3082                                         ndlp = NULL;
3083                                         /* Indicate node has already been
3084                                          * released, should not reference
3085                                          * to it from within the routine
3086                                          * lpfc_els_free_iocb.
3087                                          */
3088                                         cmdiocb->context1 = NULL;
3089                                 }
3090                         }
3091                 }
3092                 mp = (struct lpfc_dmabuf *) mbox->context1;
3093                 if (mp) {
3094                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3095                         kfree(mp);
3096                 }
3097                 mempool_free(mbox, phba->mbox_mem_pool);
3098         }
3099 out:
3100         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3101                 spin_lock_irq(shost->host_lock);
3102                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
3103                 spin_unlock_irq(shost->host_lock);
3104
3105                 /* If the node is not being used by another discovery thread,
3106                  * and we are sending a reject, we are done with it.
3107                  * Release driver reference count here and free associated
3108                  * resources.
3109                  */
3110                 if (ls_rjt)
3111                         if (lpfc_nlp_not_used(ndlp))
3112                                 /* Indicate node has already been released,
3113                                  * should not reference to it from within
3114                                  * the routine lpfc_els_free_iocb.
3115                                  */
3116                                 cmdiocb->context1 = NULL;
3117         }
3118
3119         lpfc_els_free_iocb(phba, cmdiocb);
3120         return;
3121 }
3122
3123 /**
3124  * lpfc_els_rsp_acc: Prepare and issue an acc response iocb command.
3125  * @vport: pointer to a host virtual N_Port data structure.
3126  * @flag: the els command code to be accepted.
3127  * @oldiocb: pointer to the original lpfc command iocb data structure.
3128  * @ndlp: pointer to a node-list data structure.
3129  * @mbox: pointer to the driver internal queue element for mailbox command.
3130  *
3131  * This routine prepares and issues an Accept (ACC) response IOCB
3132  * command. It uses the @flag to properly set up the IOCB field for the
3133  * specific ACC response command to be issued and invokes the
3134  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3135  * @mbox pointer is passed in, it will be put into the context_un.mbox
3136  * field of the IOCB for the completion callback function to issue the
3137  * mailbox command to the HBA later when callback is invoked.
3138  *
3139  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3140  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3141  * will be stored into the context1 field of the IOCB for the completion
3142  * callback function to the corresponding response ELS IOCB command.
3143  *
3144  * Return code
3145  *   0 - Successfully issued acc response
3146  *   1 - Failed to issue acc response
3147  **/
3148 int
3149 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3150                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3151                  LPFC_MBOXQ_t *mbox)
3152 {
3153         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3154         struct lpfc_hba  *phba = vport->phba;
3155         IOCB_t *icmd;
3156         IOCB_t *oldcmd;
3157         struct lpfc_iocbq *elsiocb;
3158         struct lpfc_sli_ring *pring;
3159         struct lpfc_sli *psli;
3160         uint8_t *pcmd;
3161         uint16_t cmdsize;
3162         int rc;
3163         ELS_PKT *els_pkt_ptr;
3164
3165         psli = &phba->sli;
3166         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
3167         oldcmd = &oldiocb->iocb;
3168
3169         switch (flag) {
3170         case ELS_CMD_ACC:
3171                 cmdsize = sizeof(uint32_t);
3172                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3173                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3174                 if (!elsiocb) {
3175                         spin_lock_irq(shost->host_lock);
3176                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3177                         spin_unlock_irq(shost->host_lock);
3178                         return 1;
3179                 }
3180
3181                 icmd = &elsiocb->iocb;
3182                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3183                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3184                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3185                 pcmd += sizeof(uint32_t);
3186
3187                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3188                         "Issue ACC:       did:x%x flg:x%x",
3189                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3190                 break;
3191         case ELS_CMD_PLOGI:
3192                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
3193                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3194                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3195                 if (!elsiocb)
3196                         return 1;
3197
3198                 icmd = &elsiocb->iocb;
3199                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3200                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3201
3202                 if (mbox)
3203                         elsiocb->context_un.mbox = mbox;
3204
3205                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3206                 pcmd += sizeof(uint32_t);
3207                 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
3208
3209                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3210                         "Issue ACC PLOGI: did:x%x flg:x%x",
3211                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3212                 break;
3213         case ELS_CMD_PRLO:
3214                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
3215                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3216                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3217                 if (!elsiocb)
3218                         return 1;
3219
3220                 icmd = &elsiocb->iocb;
3221                 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
3222                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3223
3224                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
3225                        sizeof(uint32_t) + sizeof(PRLO));
3226                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3227                 els_pkt_ptr = (ELS_PKT *) pcmd;
3228                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
3229
3230                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3231                         "Issue ACC PRLO:  did:x%x flg:x%x",
3232                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3233                 break;
3234         default:
3235                 return 1;
3236         }
3237         /* Xmit ELS ACC response tag <ulpIoTag> */
3238         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3239                          "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
3240                          "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
3241                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3242                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3243                          ndlp->nlp_rpi);
3244         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
3245                 spin_lock_irq(shost->host_lock);
3246                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3247                 spin_unlock_irq(shost->host_lock);
3248                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
3249         } else {
3250                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3251         }
3252
3253         phba->fc_stat.elsXmitACC++;
3254         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3255         if (rc == IOCB_ERROR) {
3256                 lpfc_els_free_iocb(phba, elsiocb);
3257                 return 1;
3258         }
3259         return 0;
3260 }
3261
3262 /**
3263  * lpfc_els_rsp_reject: Propare and issue a rjt response iocb command.
3264  * @vport: pointer to a virtual N_Port data structure.
3265  * @rejectError:
3266  * @oldiocb: pointer to the original lpfc command iocb data structure.
3267  * @ndlp: pointer to a node-list data structure.
3268  * @mbox: pointer to the driver internal queue element for mailbox command.
3269  *
3270  * This routine prepares and issue an Reject (RJT) response IOCB
3271  * command. If a @mbox pointer is passed in, it will be put into the
3272  * context_un.mbox field of the IOCB for the completion callback function
3273  * to issue to the HBA later.
3274  *
3275  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3276  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3277  * will be stored into the context1 field of the IOCB for the completion
3278  * callback function to the reject response ELS IOCB command.
3279  *
3280  * Return code
3281  *   0 - Successfully issued reject response
3282  *   1 - Failed to issue reject response
3283  **/
3284 int
3285 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
3286                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3287                     LPFC_MBOXQ_t *mbox)
3288 {
3289         struct lpfc_hba  *phba = vport->phba;
3290         IOCB_t *icmd;
3291         IOCB_t *oldcmd;
3292         struct lpfc_iocbq *elsiocb;
3293         struct lpfc_sli_ring *pring;
3294         struct lpfc_sli *psli;
3295         uint8_t *pcmd;
3296         uint16_t cmdsize;
3297         int rc;
3298
3299         psli = &phba->sli;
3300         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
3301
3302         cmdsize = 2 * sizeof(uint32_t);
3303         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3304                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
3305         if (!elsiocb)
3306                 return 1;
3307
3308         icmd = &elsiocb->iocb;
3309         oldcmd = &oldiocb->iocb;
3310         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3311         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3312
3313         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
3314         pcmd += sizeof(uint32_t);
3315         *((uint32_t *) (pcmd)) = rejectError;
3316
3317         if (mbox)
3318                 elsiocb->context_un.mbox = mbox;
3319
3320         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
3321         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3322                          "0129 Xmit ELS RJT x%x response tag x%x "
3323                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3324                          "rpi x%x\n",
3325                          rejectError, elsiocb->iotag,
3326                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
3327                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
3328         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3329                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
3330                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
3331
3332         phba->fc_stat.elsXmitLSRJT++;
3333         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3334         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3335
3336         if (rc == IOCB_ERROR) {
3337                 lpfc_els_free_iocb(phba, elsiocb);
3338                 return 1;
3339         }
3340         return 0;
3341 }
3342
3343 /**
3344  * lpfc_els_rsp_adisc_acc: Prepare and issue acc response to adisc iocb cmd.
3345  * @vport: pointer to a virtual N_Port data structure.
3346  * @oldiocb: pointer to the original lpfc command iocb data structure.
3347  * @ndlp: pointer to a node-list data structure.
3348  *
3349  * This routine prepares and issues an Accept (ACC) response to Address
3350  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
3351  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3352  *
3353  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3354  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3355  * will be stored into the context1 field of the IOCB for the completion
3356  * callback function to the ADISC Accept response ELS IOCB command.
3357  *
3358  * Return code
3359  *   0 - Successfully issued acc adisc response
3360  *   1 - Failed to issue adisc acc response
3361  **/
3362 int
3363 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3364                        struct lpfc_nodelist *ndlp)
3365 {
3366         struct lpfc_hba  *phba = vport->phba;
3367         struct lpfc_sli  *psli = &phba->sli;
3368         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3369         ADISC *ap;
3370         IOCB_t *icmd, *oldcmd;
3371         struct lpfc_iocbq *elsiocb;
3372         uint8_t *pcmd;
3373         uint16_t cmdsize;
3374         int rc;
3375
3376         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
3377         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3378                                      ndlp->nlp_DID, ELS_CMD_ACC);
3379         if (!elsiocb)
3380                 return 1;
3381
3382         icmd = &elsiocb->iocb;
3383         oldcmd = &oldiocb->iocb;
3384         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3385
3386         /* Xmit ADISC ACC response tag <ulpIoTag> */
3387         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3388                          "0130 Xmit ADISC ACC response iotag x%x xri: "
3389                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
3390                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3391                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3392                          ndlp->nlp_rpi);
3393         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3394
3395         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3396         pcmd += sizeof(uint32_t);
3397
3398         ap = (ADISC *) (pcmd);
3399         ap->hardAL_PA = phba->fc_pref_ALPA;
3400         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3401         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3402         ap->DID = be32_to_cpu(vport->fc_myDID);
3403
3404         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3405                 "Issue ACC ADISC: did:x%x flg:x%x",
3406                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3407
3408         phba->fc_stat.elsXmitACC++;
3409         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3410         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3411         if (rc == IOCB_ERROR) {
3412                 lpfc_els_free_iocb(phba, elsiocb);
3413                 return 1;
3414         }
3415         return 0;
3416 }
3417
3418 /**
3419  * lpfc_els_rsp_prli_acc: Prepare and issue acc response to prli iocb cmd.
3420  * @vport: pointer to a virtual N_Port data structure.
3421  * @oldiocb: pointer to the original lpfc command iocb data structure.
3422  * @ndlp: pointer to a node-list data structure.
3423  *
3424  * This routine prepares and issues an Accept (ACC) response to Process
3425  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
3426  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3427  *
3428  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3429  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3430  * will be stored into the context1 field of the IOCB for the completion
3431  * callback function to the PRLI Accept response ELS IOCB command.
3432  *
3433  * Return code
3434  *   0 - Successfully issued acc prli response
3435  *   1 - Failed to issue acc prli response
3436  **/
3437 int
3438 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3439                       struct lpfc_nodelist *ndlp)
3440 {
3441         struct lpfc_hba  *phba = vport->phba;
3442         PRLI *npr;
3443         lpfc_vpd_t *vpd;
3444         IOCB_t *icmd;
3445         IOCB_t *oldcmd;
3446         struct lpfc_iocbq *elsiocb;
3447         struct lpfc_sli_ring *pring;
3448         struct lpfc_sli *psli;
3449         uint8_t *pcmd;
3450         uint16_t cmdsize;
3451         int rc;
3452
3453         psli = &phba->sli;
3454         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
3455
3456         cmdsize = sizeof(uint32_t) + sizeof(PRLI);
3457         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3458                 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
3459         if (!elsiocb)
3460                 return 1;
3461
3462         icmd = &elsiocb->iocb;
3463         oldcmd = &oldiocb->iocb;
3464         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3465         /* Xmit PRLI ACC response tag <ulpIoTag> */
3466         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3467                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
3468                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3469                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3470                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3471                          ndlp->nlp_rpi);
3472         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3473
3474         *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
3475         pcmd += sizeof(uint32_t);
3476
3477         /* For PRLI, remainder of payload is PRLI parameter page */
3478         memset(pcmd, 0, sizeof(PRLI));
3479
3480         npr = (PRLI *) pcmd;
3481         vpd = &phba->vpd;
3482         /*
3483          * If the remote port is a target and our firmware version is 3.20 or
3484          * later, set the following bits for FC-TAPE support.
3485          */
3486         if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3487             (vpd->rev.feaLevelHigh >= 0x02)) {
3488                 npr->ConfmComplAllowed = 1;
3489                 npr->Retry = 1;
3490                 npr->TaskRetryIdReq = 1;
3491         }
3492
3493         npr->acceptRspCode = PRLI_REQ_EXECUTED;
3494         npr->estabImagePair = 1;
3495         npr->readXferRdyDis = 1;
3496         npr->ConfmComplAllowed = 1;
3497
3498         npr->prliType = PRLI_FCP_TYPE;
3499         npr->initiatorFunc = 1;
3500
3501         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3502                 "Issue ACC PRLI:  did:x%x flg:x%x",
3503                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3504
3505         phba->fc_stat.elsXmitACC++;
3506         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3507
3508         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3509         if (rc == IOCB_ERROR) {
3510                 lpfc_els_free_iocb(phba, elsiocb);
3511                 return 1;
3512         }
3513         return 0;
3514 }
3515
3516 /**
3517  * lpfc_els_rsp_rnid_acc: Issue rnid acc response iocb command.
3518  * @vport: pointer to a virtual N_Port data structure.
3519  * @format: rnid command format.
3520  * @oldiocb: pointer to the original lpfc command iocb data structure.
3521  * @ndlp: pointer to a node-list data structure.
3522  *
3523  * This routine issues a Request Node Identification Data (RNID) Accept
3524  * (ACC) response. It constructs the RNID ACC response command according to
3525  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
3526  * issue the response. Note that this command does not need to hold the ndlp
3527  * reference count for the callback. So, the ndlp reference count taken by
3528  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
3529  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
3530  * there is no ndlp reference available.
3531  *
3532  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3533  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3534  * will be stored into the context1 field of the IOCB for the completion
3535  * callback function. However, for the RNID Accept Response ELS command,
3536  * this is undone later by this routine after the IOCB is allocated.
3537  *
3538  * Return code
3539  *   0 - Successfully issued acc rnid response
3540  *   1 - Failed to issue acc rnid response
3541  **/
3542 static int
3543 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
3544                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3545 {
3546         struct lpfc_hba  *phba = vport->phba;
3547         RNID *rn;
3548         IOCB_t *icmd, *oldcmd;
3549         struct lpfc_iocbq *elsiocb;
3550         struct lpfc_sli_ring *pring;
3551         struct lpfc_sli *psli;
3552         uint8_t *pcmd;
3553         uint16_t cmdsize;
3554         int rc;
3555
3556         psli = &phba->sli;
3557         pring = &psli->ring[LPFC_ELS_RING];
3558
3559         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
3560                                         + (2 * sizeof(struct lpfc_name));
3561         if (format)
3562                 cmdsize += sizeof(RNID_TOP_DISC);
3563
3564         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3565                                      ndlp->nlp_DID, ELS_CMD_ACC);
3566         if (!elsiocb)
3567                 return 1;
3568
3569         icmd = &elsiocb->iocb;
3570         oldcmd = &oldiocb->iocb;
3571         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3572         /* Xmit RNID ACC response tag <ulpIoTag> */
3573         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3574                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
3575                          elsiocb->iotag, elsiocb->iocb.ulpContext);
3576         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3577         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3578         pcmd += sizeof(uint32_t);
3579
3580         memset(pcmd, 0, sizeof(RNID));
3581         rn = (RNID *) (pcmd);
3582         rn->Format = format;
3583         rn->CommonLen = (2 * sizeof(struct lpfc_name));
3584         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3585         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3586         switch (format) {
3587         case 0:
3588                 rn->SpecificLen = 0;
3589                 break;
3590         case RNID_TOPOLOGY_DISC:
3591                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
3592                 memcpy(&rn->un.topologyDisc.portName,
3593                        &vport->fc_portname, sizeof(struct lpfc_name));
3594                 rn->un.topologyDisc.unitType = RNID_HBA;
3595                 rn->un.topologyDisc.physPort = 0;
3596                 rn->un.topologyDisc.attachedNodes = 0;
3597                 break;
3598         default:
3599                 rn->CommonLen = 0;
3600                 rn->SpecificLen = 0;
3601                 break;
3602         }
3603
3604         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3605                 "Issue ACC RNID:  did:x%x flg:x%x",
3606                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3607
3608         phba->fc_stat.elsXmitACC++;
3609         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3610         lpfc_nlp_put(ndlp);
3611         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
3612                                     * it could be freed */
3613
3614         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3615         if (rc == IOCB_ERROR) {
3616                 lpfc_els_free_iocb(phba, elsiocb);
3617                 return 1;
3618         }
3619         return 0;
3620 }
3621
3622 /**
3623  * lpfc_els_disc_adisc: Issue remaining adisc iocbs to npr nodes of a vport.
3624  * @vport: pointer to a host virtual N_Port data structure.
3625  *
3626  * This routine issues Address Discover (ADISC) ELS commands to those
3627  * N_Ports which are in node port recovery state and ADISC has not been issued
3628  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
3629  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
3630  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
3631  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
3632  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
3633  * IOCBs quit for later pick up. On the other hand, after walking through
3634  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
3635  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
3636  * no more ADISC need to be sent.
3637  *
3638  * Return code
3639  *    The number of N_Ports with adisc issued.
3640  **/
3641 int
3642 lpfc_els_disc_adisc(struct lpfc_vport *vport)
3643 {
3644         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3645         struct lpfc_nodelist *ndlp, *next_ndlp;
3646         int sentadisc = 0;
3647
3648         /* go thru NPR nodes and issue any remaining ELS ADISCs */
3649         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3650                 if (!NLP_CHK_NODE_ACT(ndlp))
3651                         continue;
3652                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3653                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3654                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
3655                         spin_lock_irq(shost->host_lock);
3656                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3657                         spin_unlock_irq(shost->host_lock);
3658                         ndlp->nlp_prev_state = ndlp->nlp_state;
3659                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3660                         lpfc_issue_els_adisc(vport, ndlp, 0);
3661                         sentadisc++;
3662                         vport->num_disc_nodes++;
3663                         if (vport->num_disc_nodes >=
3664                             vport->cfg_discovery_threads) {
3665                                 spin_lock_irq(shost->host_lock);
3666                                 vport->fc_flag |= FC_NLP_MORE;
3667                                 spin_unlock_irq(shost->host_lock);
3668                                 break;
3669                         }
3670                 }
3671         }
3672         if (sentadisc == 0) {
3673                 spin_lock_irq(shost->host_lock);
3674                 vport->fc_flag &= ~FC_NLP_MORE;
3675                 spin_unlock_irq(shost->host_lock);
3676         }
3677         return sentadisc;
3678 }
3679
3680 /**
3681  * lpfc_els_disc_plogi: Issue plogi for all npr nodes of a vport before adisc.
3682  * @vport: pointer to a host virtual N_Port data structure.
3683  *
3684  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
3685  * which are in node port recovery state, with a @vport. Each time an ELS
3686  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
3687  * the per @vport number of discover count (num_disc_nodes) shall be
3688  * incremented. If the num_disc_nodes reaches a pre-configured threshold
3689  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
3690  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
3691  * later pick up. On the other hand, after walking through all the ndlps with
3692  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
3693  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
3694  * PLOGI need to be sent.
3695  *
3696  * Return code
3697  *   The number of N_Ports with plogi issued.
3698  **/
3699 int
3700 lpfc_els_disc_plogi(struct lpfc_vport *vport)
3701 {
3702         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3703         struct lpfc_nodelist *ndlp, *next_ndlp;
3704         int sentplogi = 0;
3705
3706         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
3707         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3708                 if (!NLP_CHK_NODE_ACT(ndlp))
3709                         continue;
3710                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3711                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3712                     (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
3713                     (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
3714                         ndlp->nlp_prev_state = ndlp->nlp_state;
3715                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3716                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
3717                         sentplogi++;
3718                         vport->num_disc_nodes++;
3719                         if (vport->num_disc_nodes >=
3720                             vport->cfg_discovery_threads) {
3721                                 spin_lock_irq(shost->host_lock);
3722                                 vport->fc_flag |= FC_NLP_MORE;
3723                                 spin_unlock_irq(shost->host_lock);
3724                                 break;
3725                         }
3726                 }
3727         }
3728         if (sentplogi) {
3729                 lpfc_set_disctmo(vport);
3730         }
3731         else {
3732                 spin_lock_irq(shost->host_lock);
3733                 vport->fc_flag &= ~FC_NLP_MORE;
3734                 spin_unlock_irq(shost->host_lock);
3735         }
3736         return sentplogi;
3737 }
3738
3739 /**
3740  * lpfc_els_flush_rscn: Clean up any rscn activities with a vport.
3741  * @vport: pointer to a host virtual N_Port data structure.
3742  *
3743  * This routine cleans up any Registration State Change Notification
3744  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
3745  * @vport together with the host_lock is used to prevent multiple thread
3746  * trying to access the RSCN array on a same @vport at the same time.
3747  **/
3748 void
3749 lpfc_els_flush_rscn(struct lpfc_vport *vport)
3750 {
3751         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3752         struct lpfc_hba  *phba = vport->phba;
3753         int i;
3754
3755         spin_lock_irq(shost->host_lock);
3756         if (vport->fc_rscn_flush) {
3757                 /* Another thread is walking fc_rscn_id_list on this vport */
3758                 spin_unlock_irq(shost->host_lock);
3759                 return;
3760         }
3761         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
3762         vport->fc_rscn_flush = 1;
3763         spin_unlock_irq(shost->host_lock);
3764
3765         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
3766                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
3767                 vport->fc_rscn_id_list[i] = NULL;
3768         }
3769         spin_lock_irq(shost->host_lock);
3770         vport->fc_rscn_id_cnt = 0;
3771         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
3772         spin_unlock_irq(shost->host_lock);
3773         lpfc_can_disctmo(vport);
3774         /* Indicate we are done walking this fc_rscn_id_list */
3775         vport->fc_rscn_flush = 0;
3776 }
3777
3778 /**
3779  * lpfc_rscn_payload_check: Check whether there is a pending rscn to a did.
3780  * @vport: pointer to a host virtual N_Port data structure.
3781  * @did: remote destination port identifier.
3782  *
3783  * This routine checks whether there is any pending Registration State
3784  * Configuration Notification (RSCN) to a @did on @vport.
3785  *
3786  * Return code
3787  *   None zero - The @did matched with a pending rscn
3788  *   0 - not able to match @did with a pending rscn
3789  **/
3790 int
3791 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
3792 {
3793         D_ID ns_did;
3794         D_ID rscn_did;
3795         uint32_t *lp;
3796         uint32_t payload_len, i;
3797         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3798
3799         ns_did.un.word = did;
3800
3801         /* Never match fabric nodes for RSCNs */
3802         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
3803                 return 0;
3804
3805         /* If we are doing a FULL RSCN rediscovery, match everything */
3806         if (vport->fc_flag & FC_RSCN_DISCOVERY)
3807                 return did;
3808
3809         spin_lock_irq(shost->host_lock);
3810         if (vport->fc_rscn_flush) {
3811                 /* Another thread is walking fc_rscn_id_list on this vport */
3812                 spin_unlock_irq(shost->host_lock);
3813                 return 0;
3814         }
3815         /* Indicate we are walking fc_rscn_id_list on this vport */
3816         vport->fc_rscn_flush = 1;
3817         spin_unlock_irq(shost->host_lock);
3818         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
3819                 lp = vport->fc_rscn_id_list[i]->virt;
3820                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
3821                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
3822                 while (payload_len) {
3823                         rscn_did.un.word = be32_to_cpu(*lp++);
3824                         payload_len -= sizeof(uint32_t);
3825                         switch (rscn_did.un.b.resv) {
3826                         case 0: /* Single N_Port ID effected */
3827                                 if (ns_did.un.word == rscn_did.un.word)
3828                                         goto return_did_out;
3829                                 break;
3830                         case 1: /* Whole N_Port Area effected */
3831                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
3832                                     && (ns_did.un.b.area == rscn_did.un.b.area))
3833                                         goto return_did_out;
3834                                 break;
3835                         case 2: /* Whole N_Port Domain effected */
3836                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
3837                                         goto return_did_out;
3838                                 break;
3839                         default:
3840                                 /* Unknown Identifier in RSCN node */
3841                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3842                                                  "0217 Unknown Identifier in "
3843                                                  "RSCN payload Data: x%x\n",
3844                                                  rscn_did.un.word);
3845                         case 3: /* Whole Fabric effected */
3846                                 goto return_did_out;
3847                         }
3848                 }
3849         }
3850         /* Indicate we are done with walking fc_rscn_id_list on this vport */
3851         vport->fc_rscn_flush = 0;
3852         return 0;
3853 return_did_out:
3854         /* Indicate we are done with walking fc_rscn_id_list on this vport */
3855         vport->fc_rscn_flush = 0;
3856         return did;
3857 }
3858
3859 /**
3860  * lpfc_rscn_recovery_check: Send recovery event to vport nodes matching rscn
3861  * @vport: pointer to a host virtual N_Port data structure.
3862  *
3863  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
3864  * state machine for a @vport's nodes that are with pending RSCN (Registration
3865  * State Change Notification).
3866  *
3867  * Return code
3868  *   0 - Successful (currently alway return 0)
3869  **/
3870 static int
3871 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
3872 {
3873         struct lpfc_nodelist *ndlp = NULL;
3874
3875         /* Move all affected nodes by pending RSCNs to NPR state. */
3876         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3877                 if (!NLP_CHK_NODE_ACT(ndlp) ||
3878                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
3879                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
3880                         continue;
3881                 lpfc_disc_state_machine(vport, ndlp, NULL,
3882                                         NLP_EVT_DEVICE_RECOVERY);
3883                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
3884         }
3885         return 0;
3886 }
3887
3888 /**
3889  * lpfc_els_rcv_rscn: Process an unsolicited rscn iocb.
3890  * @vport: pointer to a host virtual N_Port data structure.
3891  * @cmdiocb: pointer to lpfc command iocb data structure.
3892  * @ndlp: pointer to a node-list data structure.
3893  *
3894  * This routine processes an unsolicited RSCN (Registration State Change
3895  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
3896  * to invoke fc_host_post_event() routine to the FC transport layer. If the
3897  * discover state machine is about to begin discovery, it just accepts the
3898  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
3899  * contains N_Port IDs for other vports on this HBA, it just accepts the
3900  * RSCN and ignore processing it. If the state machine is in the recovery
3901  * state, the fc_rscn_id_list of this @vport is walked and the
3902  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
3903  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
3904  * routine is invoked to handle the RSCN event.
3905  *
3906  * Return code
3907  *   0 - Just sent the acc response
3908  *   1 - Sent the acc response and waited for name server completion
3909  **/
3910 static int
3911 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3912                   struct lpfc_nodelist *ndlp)
3913 {
3914         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3915         struct lpfc_hba  *phba = vport->phba;
3916         struct lpfc_dmabuf *pcmd;
3917         uint32_t *lp, *datap;
3918         IOCB_t *icmd;
3919         uint32_t payload_len, length, nportid, *cmd;
3920         int rscn_cnt;
3921         int rscn_id = 0, hba_id = 0;
3922         int i;
3923
3924         icmd = &cmdiocb->iocb;
3925         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3926         lp = (uint32_t *) pcmd->virt;
3927
3928         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
3929         payload_len -= sizeof(uint32_t);        /* take off word 0 */
3930         /* RSCN received */
3931         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3932                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
3933                          vport->fc_flag, payload_len, *lp,
3934                          vport->fc_rscn_id_cnt);
3935         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
3936                 fc_host_post_event(shost, fc_get_event_number(),
3937                         FCH_EVT_RSCN, lp[i]);
3938
3939         /* If we are about to begin discovery, just ACC the RSCN.
3940          * Discovery processing will satisfy it.
3941          */
3942         if (vport->port_state <= LPFC_NS_QRY) {
3943                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3944                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
3945                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
3946
3947                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
3948                 return 0;
3949         }
3950
3951         /* If this RSCN just contains NPortIDs for other vports on this HBA,
3952          * just ACC and ignore it.
3953          */
3954         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3955                 !(vport->cfg_peer_port_login)) {
3956                 i = payload_len;
3957                 datap = lp;
3958                 while (i > 0) {
3959                         nportid = *datap++;
3960                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
3961                         i -= sizeof(uint32_t);
3962                         rscn_id++;
3963                         if (lpfc_find_vport_by_did(phba, nportid))
3964                                 hba_id++;
3965                 }
3966                 if (rscn_id == hba_id) {
3967                         /* ALL NPortIDs in RSCN are on HBA */
3968                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3969                                          "0219 Ignore RSCN "
3970                                          "Data: x%x x%x x%x x%x\n",
3971                                          vport->fc_flag, payload_len,
3972                                          *lp, vport->fc_rscn_id_cnt);
3973                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3974                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
3975                                 ndlp->nlp_DID, vport->port_state,
3976                                 ndlp->nlp_flag);
3977
3978                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
3979                                 ndlp, NULL);
3980                         return 0;
3981                 }
3982         }
3983
3984         spin_lock_irq(shost->host_lock);
3985         if (vport->fc_rscn_flush) {
3986                 /* Another thread is walking fc_rscn_id_list on this vport */
3987                 spin_unlock_irq(shost->host_lock);
3988                 vport->fc_flag |= FC_RSCN_DISCOVERY;
3989                 /* Send back ACC */
3990                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
3991                 return 0;
3992         }
3993         /* Indicate we are walking fc_rscn_id_list on this vport */
3994         vport->fc_rscn_flush = 1;
3995         spin_unlock_irq(shost->host_lock);
3996         /* Get the array count after sucessfully have the token */
3997         rscn_cnt = vport->fc_rscn_id_cnt;
3998         /* If we are already processing an RSCN, save the received
3999          * RSCN payload buffer, cmdiocb->context2 to process later.
4000          */
4001         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
4002                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4003                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
4004                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4005
4006                 spin_lock_irq(shost->host_lock);
4007                 vport->fc_flag |= FC_RSCN_DEFERRED;
4008                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
4009                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
4010                         vport->fc_flag |= FC_RSCN_MODE;
4011                         spin_unlock_irq(shost->host_lock);
4012                         if (rscn_cnt) {
4013                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4014                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4015                         }
4016                         if ((rscn_cnt) &&
4017                             (payload_len + length <= LPFC_BPL_SIZE)) {
4018                                 *cmd &= ELS_CMD_MASK;
4019                                 *cmd |= cpu_to_be32(payload_len + length);
4020                                 memcpy(((uint8_t *)cmd) + length, lp,
4021                                        payload_len);
4022                         } else {
4023                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4024                                 vport->fc_rscn_id_cnt++;
4025                                 /* If we zero, cmdiocb->context2, the calling
4026                                  * routine will not try to free it.
4027                                  */
4028                                 cmdiocb->context2 = NULL;
4029                         }
4030                         /* Deferred RSCN */
4031                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4032                                          "0235 Deferred RSCN "
4033                                          "Data: x%x x%x x%x\n",
4034                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4035                                          vport->port_state);
4036                 } else {
4037                         vport->fc_flag |= FC_RSCN_DISCOVERY;
4038                         spin_unlock_irq(shost->host_lock);
4039                         /* ReDiscovery RSCN */
4040                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4041                                          "0234 ReDiscovery RSCN "
4042                                          "Data: x%x x%x x%x\n",
4043                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4044                                          vport->port_state);
4045                 }
4046                 /* Indicate we are done walking fc_rscn_id_list on this vport */
4047                 vport->fc_rscn_flush = 0;
4048                 /* Send back ACC */
4049                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4050                 /* send RECOVERY event for ALL nodes that match RSCN payload */
4051                 lpfc_rscn_recovery_check(vport);
4052                 spin_lock_irq(shost->host_lock);
4053                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
4054                 spin_unlock_irq(shost->host_lock);
4055                 return 0;
4056         }
4057         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4058                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
4059                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4060
4061         spin_lock_irq(shost->host_lock);
4062         vport->fc_flag |= FC_RSCN_MODE;
4063         spin_unlock_irq(shost->host_lock);
4064         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
4065         /* Indicate we are done walking fc_rscn_id_list on this vport */
4066         vport->fc_rscn_flush = 0;
4067         /*
4068          * If we zero, cmdiocb->context2, the calling routine will
4069          * not try to free it.
4070          */
4071         cmdiocb->context2 = NULL;
4072         lpfc_set_disctmo(vport);
4073         /* Send back ACC */
4074         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4075         /* send RECOVERY event for ALL nodes that match RSCN payload */
4076         lpfc_rscn_recovery_check(vport);
4077         return lpfc_els_handle_rscn(vport);
4078 }
4079
4080 /**
4081  * lpfc_els_handle_rscn: Handle rscn for a vport.
4082  * @vport: pointer to a host virtual N_Port data structure.
4083  *
4084  * This routine handles the Registration State Configuration Notification
4085  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
4086  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
4087  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
4088  * NameServer shall be issued. If CT command to the NameServer fails to be
4089  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
4090  * RSCN activities with the @vport.
4091  *
4092  * Return code
4093  *   0 - Cleaned up rscn on the @vport
4094  *   1 - Wait for plogi to name server before proceed
4095  **/
4096 int
4097 lpfc_els_handle_rscn(struct lpfc_vport *vport)
4098 {
4099         struct lpfc_nodelist *ndlp;
4100         struct lpfc_hba *phba = vport->phba;
4101
4102         /* Ignore RSCN if the port is being torn down. */
4103         if (vport->load_flag & FC_UNLOADING) {
4104                 lpfc_els_flush_rscn(vport);
4105                 return 0;
4106         }
4107
4108         /* Start timer for RSCN processing */
4109         lpfc_set_disctmo(vport);
4110
4111         /* RSCN processed */
4112         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4113                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
4114                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
4115                          vport->port_state);
4116
4117         /* To process RSCN, first compare RSCN data with NameServer */
4118         vport->fc_ns_retry = 0;
4119         vport->num_disc_nodes = 0;
4120
4121         ndlp = lpfc_findnode_did(vport, NameServer_DID);
4122         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
4123             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
4124                 /* Good ndlp, issue CT Request to NameServer */
4125                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
4126                         /* Wait for NameServer query cmpl before we can
4127                            continue */
4128                         return 1;
4129         } else {
4130                 /* If login to NameServer does not exist, issue one */
4131                 /* Good status, issue PLOGI to NameServer */
4132                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
4133                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4134                         /* Wait for NameServer login cmpl before we can
4135                            continue */
4136                         return 1;
4137
4138                 if (ndlp) {
4139                         ndlp = lpfc_enable_node(vport, ndlp,
4140                                                 NLP_STE_PLOGI_ISSUE);
4141                         if (!ndlp) {
4142                                 lpfc_els_flush_rscn(vport);
4143                                 return 0;
4144                         }
4145                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
4146                 } else {
4147                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4148                         if (!ndlp) {
4149                                 lpfc_els_flush_rscn(vport);
4150                                 return 0;
4151                         }
4152                         lpfc_nlp_init(vport, ndlp, NameServer_DID);
4153                         ndlp->nlp_prev_state = ndlp->nlp_state;
4154                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4155                 }
4156                 ndlp->nlp_type |= NLP_FABRIC;
4157                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
4158                 /* Wait for NameServer login cmpl before we can
4159                  * continue
4160                  */
4161                 return 1;
4162         }
4163
4164         lpfc_els_flush_rscn(vport);
4165         return 0;
4166 }
4167
4168 /**
4169  * lpfc_els_rcv_flogi: Process an unsolicited flogi iocb.
4170  * @vport: pointer to a host virtual N_Port data structure.
4171  * @cmdiocb: pointer to lpfc command iocb data structure.
4172  * @ndlp: pointer to a node-list data structure.
4173  *
4174  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
4175  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
4176  * point topology. As an unsolicited FLOGI should not be received in a loop
4177  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
4178  * lpfc_check_sparm() routine is invoked to check the parameters in the
4179  * unsolicited FLOGI. If parameters validation failed, the routine
4180  * lpfc_els_rsp_reject() shall be called with reject reason code set to
4181  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
4182  * FLOGI shall be compared with the Port WWN of the @vport to determine who
4183  * will initiate PLOGI. The higher lexicographical value party shall has
4184  * higher priority (as the winning port) and will initiate PLOGI and
4185  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
4186  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
4187  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
4188  *
4189  * Return code
4190  *   0 - Successfully processed the unsolicited flogi
4191  *   1 - Failed to process the unsolicited flogi
4192  **/
4193 static int
4194 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4195                    struct lpfc_nodelist *ndlp)
4196 {
4197         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4198         struct lpfc_hba  *phba = vport->phba;
4199         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4200         uint32_t *lp = (uint32_t *) pcmd->virt;
4201         IOCB_t *icmd = &cmdiocb->iocb;
4202         struct serv_parm *sp;
4203         LPFC_MBOXQ_t *mbox;
4204         struct ls_rjt stat;
4205         uint32_t cmd, did;
4206         int rc;
4207
4208         cmd = *lp++;
4209         sp = (struct serv_parm *) lp;
4210
4211         /* FLOGI received */
4212
4213         lpfc_set_disctmo(vport);
4214
4215         if (phba->fc_topology == TOPOLOGY_LOOP) {
4216                 /* We should never receive a FLOGI in loop mode, ignore it */
4217                 did = icmd->un.elsreq64.remoteID;
4218
4219                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
4220                    Loop Mode */
4221                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4222                                  "0113 An FLOGI ELS command x%x was "
4223                                  "received from DID x%x in Loop Mode\n",
4224                                  cmd, did);
4225                 return 1;
4226         }
4227
4228         did = Fabric_DID;
4229
4230         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
4231                 /* For a FLOGI we accept, then if our portname is greater
4232                  * then the remote portname we initiate Nport login.
4233                  */
4234
4235                 rc = memcmp(&vport->fc_portname, &sp->portName,
4236                             sizeof(struct lpfc_name));
4237
4238                 if (!rc) {
4239                         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4240                         if (!mbox)
4241                                 return 1;
4242
4243                         lpfc_linkdown(phba);
4244                         lpfc_init_link(phba, mbox,
4245                                        phba->cfg_topology,
4246                                        phba->cfg_link_speed);
4247                         mbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
4248                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4249                         mbox->vport = vport;
4250                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4251                         lpfc_set_loopback_flag(phba);
4252                         if (rc == MBX_NOT_FINISHED) {
4253                                 mempool_free(mbox, phba->mbox_mem_pool);
4254                         }
4255                         return 1;
4256                 } else if (rc > 0) {    /* greater than */
4257                         spin_lock_irq(shost->host_lock);
4258                         vport->fc_flag |= FC_PT2PT_PLOGI;
4259                         spin_unlock_irq(shost->host_lock);
4260                 }
4261                 spin_lock_irq(shost->host_lock);
4262                 vport->fc_flag |= FC_PT2PT;
4263                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4264                 spin_unlock_irq(shost->host_lock);
4265         } else {
4266                 /* Reject this request because invalid parameters */
4267                 stat.un.b.lsRjtRsvd0 = 0;
4268                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4269                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
4270                 stat.un.b.vendorUnique = 0;
4271                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4272                         NULL);
4273                 return 1;
4274         }
4275
4276         /* Send back ACC */
4277         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
4278
4279         return 0;
4280 }
4281
4282 /**
4283  * lpfc_els_rcv_rnid: Process an unsolicited rnid iocb.
4284  * @vport: pointer to a host virtual N_Port data structure.
4285  * @cmdiocb: pointer to lpfc command iocb data structure.
4286  * @ndlp: pointer to a node-list data structure.
4287  *
4288  * This routine processes Request Node Identification Data (RNID) IOCB
4289  * received as an ELS unsolicited event. Only when the RNID specified format
4290  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
4291  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
4292  * Accept (ACC) the RNID ELS command. All the other RNID formats are
4293  * rejected by invoking the lpfc_els_rsp_reject() routine.
4294  *
4295  * Return code
4296  *   0 - Successfully processed rnid iocb (currently always return 0)
4297  **/
4298 static int
4299 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4300                   struct lpfc_nodelist *ndlp)
4301 {
4302         struct lpfc_dmabuf *pcmd;
4303         uint32_t *lp;
4304         IOCB_t *icmd;
4305         RNID *rn;
4306         struct ls_rjt stat;
4307         uint32_t cmd, did;
4308
4309         icmd = &cmdiocb->iocb;
4310         did = icmd->un.elsreq64.remoteID;
4311         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4312         lp = (uint32_t *) pcmd->virt;
4313
4314         cmd = *lp++;
4315         rn = (RNID *) lp;
4316
4317         /* RNID received */
4318
4319         switch (rn->Format) {
4320         case 0:
4321         case RNID_TOPOLOGY_DISC:
4322                 /* Send back ACC */
4323                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
4324                 break;
4325         default:
4326                 /* Reject this request because format not supported */
4327                 stat.un.b.lsRjtRsvd0 = 0;
4328                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4329                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4330                 stat.un.b.vendorUnique = 0;
4331                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4332                         NULL);
4333         }
4334         return 0;
4335 }
4336
4337 /**
4338  * lpfc_els_rcv_lirr: Process an unsolicited lirr iocb.
4339  * @vport: pointer to a host virtual N_Port data structure.
4340  * @cmdiocb: pointer to lpfc command iocb data structure.
4341  * @ndlp: pointer to a node-list data structure.
4342  *
4343  * This routine processes a Link Incident Report Registration(LIRR) IOCB
4344  * received as an ELS unsolicited event. Currently, this function just invokes
4345  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
4346  *
4347  * Return code
4348  *   0 - Successfully processed lirr iocb (currently always return 0)
4349  **/
4350 static int
4351 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4352                   struct lpfc_nodelist *ndlp)
4353 {
4354         struct ls_rjt stat;
4355
4356         /* For now, unconditionally reject this command */
4357         stat.un.b.lsRjtRsvd0 = 0;
4358         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4359         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4360         stat.un.b.vendorUnique = 0;
4361         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4362         return 0;
4363 }
4364
4365 /**
4366  * lpfc_els_rsp_rps_acc: Completion callbk func for MBX_READ_LNK_STAT mbox cmd.
4367  * @phba: pointer to lpfc hba data structure.
4368  * @pmb: pointer to the driver internal queue element for mailbox command.
4369  *
4370  * This routine is the completion callback function for the MBX_READ_LNK_STAT
4371  * mailbox command. This callback function is to actually send the Accept
4372  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
4373  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
4374  * mailbox command, constructs the RPS response with the link statistics
4375  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
4376  * response to the RPS.
4377  *
4378  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4379  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4380  * will be stored into the context1 field of the IOCB for the completion
4381  * callback function to the RPS Accept Response ELS IOCB command.
4382  *
4383  **/
4384 static void
4385 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4386 {
4387         struct lpfc_sli *psli = &phba->sli;
4388         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
4389         MAILBOX_t *mb;
4390         IOCB_t *icmd;
4391         RPS_RSP *rps_rsp;
4392         uint8_t *pcmd;
4393         struct lpfc_iocbq *elsiocb;
4394         struct lpfc_nodelist *ndlp;
4395         uint16_t xri, status;
4396         uint32_t cmdsize;
4397
4398         mb = &pmb->mb;
4399
4400         ndlp = (struct lpfc_nodelist *) pmb->context2;
4401         xri = (uint16_t) ((unsigned long)(pmb->context1));
4402         pmb->context1 = NULL;
4403         pmb->context2 = NULL;
4404
4405         if (mb->mbxStatus) {
4406                 mempool_free(pmb, phba->mbox_mem_pool);
4407                 return;
4408         }
4409
4410         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
4411         mempool_free(pmb, phba->mbox_mem_pool);
4412         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
4413                                      lpfc_max_els_tries, ndlp,
4414                                      ndlp->nlp_DID, ELS_CMD_ACC);
4415
4416         /* Decrement the ndlp reference count from previous mbox command */
4417         lpfc_nlp_put(ndlp);
4418
4419         if (!elsiocb)
4420                 return;
4421
4422         icmd = &elsiocb->iocb;
4423         icmd->ulpContext = xri;
4424
4425         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4426         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4427         pcmd += sizeof(uint32_t); /* Skip past command */
4428         rps_rsp = (RPS_RSP *)pcmd;
4429
4430         if (phba->fc_topology != TOPOLOGY_LOOP)
4431                 status = 0x10;
4432         else
4433                 status = 0x8;
4434         if (phba->pport->fc_flag & FC_FABRIC)
4435                 status |= 0x4;
4436
4437         rps_rsp->rsvd1 = 0;
4438         rps_rsp->portStatus = cpu_to_be16(status);
4439         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
4440         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
4441         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
4442         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
4443         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
4444         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
4445         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
4446         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
4447                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
4448                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4449                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4450                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4451                          ndlp->nlp_rpi);
4452         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4453         phba->fc_stat.elsXmitACC++;
4454         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR)
4455                 lpfc_els_free_iocb(phba, elsiocb);
4456         return;
4457 }
4458
4459 /**
4460  * lpfc_els_rcv_rps: Process an unsolicited rps iocb.
4461  * @vport: pointer to a host virtual N_Port data structure.
4462  * @cmdiocb: pointer to lpfc command iocb data structure.
4463  * @ndlp: pointer to a node-list data structure.
4464  *
4465  * This routine processes Read Port Status (RPS) IOCB received as an
4466  * ELS unsolicited event. It first checks the remote port state. If the
4467  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
4468  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
4469  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
4470  * for reading the HBA link statistics. It is for the callback function,
4471  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
4472  * to actually sending out RPS Accept (ACC) response.
4473  *
4474  * Return codes
4475  *   0 - Successfully processed rps iocb (currently always return 0)
4476  **/
4477 static int
4478 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4479                  struct lpfc_nodelist *ndlp)
4480 {
4481         struct lpfc_hba *phba = vport->phba;
4482         uint32_t *lp;
4483         uint8_t flag;
4484         LPFC_MBOXQ_t *mbox;
4485         struct lpfc_dmabuf *pcmd;
4486         RPS *rps;
4487         struct ls_rjt stat;
4488
4489         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4490             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
4491                 /* reject the unsolicited RPS request and done with it */
4492                 goto reject_out;
4493
4494         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4495         lp = (uint32_t *) pcmd->virt;
4496         flag = (be32_to_cpu(*lp++) & 0xf);
4497         rps = (RPS *) lp;
4498
4499         if ((flag == 0) ||
4500             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
4501             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
4502                                     sizeof(struct lpfc_name)) == 0))) {
4503
4504                 printk("Fix me....\n");
4505                 dump_stack();
4506                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
4507                 if (mbox) {
4508                         lpfc_read_lnk_stat(phba, mbox);
4509                         mbox->context1 =
4510                             (void *)((unsigned long) cmdiocb->iocb.ulpContext);
4511                         mbox->context2 = lpfc_nlp_get(ndlp);
4512                         mbox->vport = vport;
4513                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
4514                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4515                                 != MBX_NOT_FINISHED)
4516                                 /* Mbox completion will send ELS Response */
4517                                 return 0;
4518                         /* Decrement reference count used for the failed mbox
4519                          * command.
4520                          */
4521                         lpfc_nlp_put(ndlp);
4522                         mempool_free(mbox, phba->mbox_mem_pool);
4523                 }
4524         }
4525
4526 reject_out:
4527         /* issue rejection response */
4528         stat.un.b.lsRjtRsvd0 = 0;
4529         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4530         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4531         stat.un.b.vendorUnique = 0;
4532         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4533         return 0;
4534 }
4535
4536 /**
4537  * lpfc_els_rsp_rpl_acc: Issue an accept rpl els command.
4538  * @vport: pointer to a host virtual N_Port data structure.
4539  * @cmdsize: size of the ELS command.
4540  * @oldiocb: pointer to the original lpfc command iocb data structure.
4541  * @ndlp: pointer to a node-list data structure.
4542  *
4543  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
4544  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
4545  *
4546  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4547  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4548  * will be stored into the context1 field of the IOCB for the completion
4549  * callback function to the RPL Accept Response ELS command.
4550  *
4551  * Return code
4552  *   0 - Successfully issued ACC RPL ELS command
4553  *   1 - Failed to issue ACC RPL ELS command
4554  **/
4555 static int
4556 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
4557                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4558 {
4559         struct lpfc_hba *phba = vport->phba;
4560         IOCB_t *icmd, *oldcmd;
4561         RPL_RSP rpl_rsp;
4562         struct lpfc_iocbq *elsiocb;
4563         struct lpfc_sli *psli = &phba->sli;
4564         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
4565         uint8_t *pcmd;
4566
4567         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4568                                      ndlp->nlp_DID, ELS_CMD_ACC);
4569
4570         if (!elsiocb)
4571                 return 1;
4572
4573         icmd = &elsiocb->iocb;
4574         oldcmd = &oldiocb->iocb;
4575         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
4576
4577         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4578         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4579         pcmd += sizeof(uint16_t);
4580         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
4581         pcmd += sizeof(uint16_t);
4582
4583         /* Setup the RPL ACC payload */
4584         rpl_rsp.listLen = be32_to_cpu(1);
4585         rpl_rsp.index = 0;
4586         rpl_rsp.port_num_blk.portNum = 0;
4587         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
4588         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
4589             sizeof(struct lpfc_name));
4590         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
4591         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
4592         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4593                          "0120 Xmit ELS RPL ACC response tag x%x "
4594                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4595                          "rpi x%x\n",
4596                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4597                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4598                          ndlp->nlp_rpi);
4599         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4600         phba->fc_stat.elsXmitACC++;
4601         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
4602                 lpfc_els_free_iocb(phba, elsiocb);
4603                 return 1;
4604         }
4605         return 0;
4606 }
4607
4608 /**
4609  * lpfc_els_rcv_rpl: Process an unsolicited rpl iocb.
4610  * @vport: pointer to a host virtual N_Port data structure.
4611  * @cmdiocb: pointer to lpfc command iocb data structure.
4612  * @ndlp: pointer to a node-list data structure.
4613  *
4614  * This routine processes Read Port List (RPL) IOCB received as an ELS
4615  * unsolicited event. It first checks the remote port state. If the remote
4616  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
4617  * invokes the lpfc_els_rsp_reject() routine to send reject response.
4618  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
4619  * to accept the RPL.
4620  *
4621  * Return code
4622  *   0 - Successfully processed rpl iocb (currently always return 0)
4623  **/
4624 static int
4625 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4626                  struct lpfc_nodelist *ndlp)
4627 {
4628         struct lpfc_dmabuf *pcmd;
4629         uint32_t *lp;
4630         uint32_t maxsize;
4631         uint16_t cmdsize;
4632         RPL *rpl;
4633         struct ls_rjt stat;
4634
4635         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4636             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
4637                 /* issue rejection response */
4638                 stat.un.b.lsRjtRsvd0 = 0;
4639                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4640                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4641                 stat.un.b.vendorUnique = 0;
4642                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4643                         NULL);
4644                 /* rejected the unsolicited RPL request and done with it */
4645                 return 0;
4646         }
4647
4648         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4649         lp = (uint32_t *) pcmd->virt;
4650         rpl = (RPL *) (lp + 1);
4651
4652         maxsize = be32_to_cpu(rpl->maxsize);
4653
4654         /* We support only one port */
4655         if ((rpl->index == 0) &&
4656             ((maxsize == 0) ||
4657              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
4658                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
4659         } else {
4660                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
4661         }
4662         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
4663
4664         return 0;
4665 }
4666
4667 /**
4668  * lpfc_els_rcv_farp: Process an unsolicited farp request els command.
4669  * @vport: pointer to a virtual N_Port data structure.
4670  * @cmdiocb: pointer to lpfc command iocb data structure.
4671  * @ndlp: pointer to a node-list data structure.
4672  *
4673  * This routine processes Fibre Channel Address Resolution Protocol
4674  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
4675  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
4676  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
4677  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
4678  * remote PortName is compared against the FC PortName stored in the @vport
4679  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
4680  * compared against the FC NodeName stored in the @vport data structure.
4681  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
4682  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
4683  * invoked to send out FARP Response to the remote node. Before sending the
4684  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
4685  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
4686  * routine is invoked to log into the remote port first.
4687  *
4688  * Return code
4689  *   0 - Either the FARP Match Mode not supported or successfully processed
4690  **/
4691 static int
4692 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4693                   struct lpfc_nodelist *ndlp)
4694 {
4695         struct lpfc_dmabuf *pcmd;
4696         uint32_t *lp;
4697         IOCB_t *icmd;
4698         FARP *fp;
4699         uint32_t cmd, cnt, did;
4700
4701         icmd = &cmdiocb->iocb;
4702         did = icmd->un.elsreq64.remoteID;
4703         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4704         lp = (uint32_t *) pcmd->virt;
4705
4706         cmd = *lp++;
4707         fp = (FARP *) lp;
4708         /* FARP-REQ received from DID <did> */
4709         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4710                          "0601 FARP-REQ received from DID x%x\n", did);
4711         /* We will only support match on WWPN or WWNN */
4712         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
4713                 return 0;
4714         }
4715
4716         cnt = 0;
4717         /* If this FARP command is searching for my portname */
4718         if (fp->Mflags & FARP_MATCH_PORT) {
4719                 if (memcmp(&fp->RportName, &vport->fc_portname,
4720                            sizeof(struct lpfc_name)) == 0)
4721                         cnt = 1;
4722         }
4723
4724         /* If this FARP command is searching for my nodename */
4725         if (fp->Mflags & FARP_MATCH_NODE) {
4726                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
4727                            sizeof(struct lpfc_name)) == 0)
4728                         cnt = 1;
4729         }
4730
4731         if (cnt) {
4732                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
4733                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
4734                         /* Log back into the node before sending the FARP. */
4735                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
4736                                 ndlp->nlp_prev_state = ndlp->nlp_state;
4737                                 lpfc_nlp_set_state(vport, ndlp,
4738                                                    NLP_STE_PLOGI_ISSUE);
4739                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4740                         }
4741
4742                         /* Send a FARP response to that node */
4743                         if (fp->Rflags & FARP_REQUEST_FARPR)
4744                                 lpfc_issue_els_farpr(vport, did, 0);
4745                 }
4746         }
4747         return 0;
4748 }
4749
4750 /**
4751  * lpfc_els_rcv_farpr: Process an unsolicited farp response iocb.
4752  * @vport: pointer to a host virtual N_Port data structure.
4753  * @cmdiocb: pointer to lpfc command iocb data structure.
4754  * @ndlp: pointer to a node-list data structure.
4755  *
4756  * This routine processes Fibre Channel Address Resolution Protocol
4757  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
4758  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
4759  * the FARP response request.
4760  *
4761  * Return code
4762  *   0 - Successfully processed FARPR IOCB (currently always return 0)
4763  **/
4764 static int
4765 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4766                    struct lpfc_nodelist  *ndlp)
4767 {
4768         struct lpfc_dmabuf *pcmd;
4769         uint32_t *lp;
4770         IOCB_t *icmd;
4771         uint32_t cmd, did;
4772
4773         icmd = &cmdiocb->iocb;
4774         did = icmd->un.elsreq64.remoteID;
4775         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4776         lp = (uint32_t *) pcmd->virt;
4777
4778         cmd = *lp++;
4779         /* FARP-RSP received from DID <did> */
4780         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4781                          "0600 FARP-RSP received from DID x%x\n", did);
4782         /* ACCEPT the Farp resp request */
4783         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4784
4785         return 0;
4786 }
4787
4788 /**
4789  * lpfc_els_rcv_fan: Process an unsolicited fan iocb command.
4790  * @vport: pointer to a host virtual N_Port data structure.
4791  * @cmdiocb: pointer to lpfc command iocb data structure.
4792  * @fan_ndlp: pointer to a node-list data structure.
4793  *
4794  * This routine processes a Fabric Address Notification (FAN) IOCB
4795  * command received as an ELS unsolicited event. The FAN ELS command will
4796  * only be processed on a physical port (i.e., the @vport represents the
4797  * physical port). The fabric NodeName and PortName from the FAN IOCB are
4798  * compared against those in the phba data structure. If any of those is
4799  * different, the lpfc_initial_flogi() routine is invoked to initialize
4800  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
4801  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
4802  * is invoked to register login to the fabric.
4803  *
4804  * Return code
4805  *   0 - Successfully processed fan iocb (currently always return 0).
4806  **/
4807 static int
4808 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4809                  struct lpfc_nodelist *fan_ndlp)
4810 {
4811         struct lpfc_hba *phba = vport->phba;
4812         uint32_t *lp;
4813         FAN *fp;
4814
4815         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
4816         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
4817         fp = (FAN *) ++lp;
4818         /* FAN received; Fan does not have a reply sequence */
4819         if ((vport == phba->pport) &&
4820             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
4821                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
4822                             sizeof(struct lpfc_name))) ||
4823                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
4824                             sizeof(struct lpfc_name)))) {
4825                         /* This port has switched fabrics. FLOGI is required */
4826                         lpfc_initial_flogi(vport);
4827                 } else {
4828                         /* FAN verified - skip FLOGI */
4829                         vport->fc_myDID = vport->fc_prevDID;
4830                         lpfc_issue_fabric_reglogin(vport);
4831                 }
4832         }
4833         return 0;
4834 }
4835
4836 /**
4837  * lpfc_els_timeout: Handler funciton to the els timer.
4838  * @ptr: holder for the timer function associated data.
4839  *
4840  * This routine is invoked by the ELS timer after timeout. It posts the ELS
4841  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
4842  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
4843  * up the worker thread. It is for the worker thread to invoke the routine
4844  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
4845  **/
4846 void
4847 lpfc_els_timeout(unsigned long ptr)
4848 {
4849         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4850         struct lpfc_hba   *phba = vport->phba;
4851         uint32_t tmo_posted;
4852         unsigned long iflag;
4853
4854         spin_lock_irqsave(&vport->work_port_lock, iflag);
4855         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
4856         if (!tmo_posted)
4857                 vport->work_port_events |= WORKER_ELS_TMO;
4858         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
4859
4860         if (!tmo_posted)
4861                 lpfc_worker_wake_up(phba);
4862         return;
4863 }
4864
4865 /**
4866  * lpfc_els_timeout_handler: Process an els timeout event.
4867  * @vport: pointer to a virtual N_Port data structure.
4868  *
4869  * This routine is the actual handler function that processes an ELS timeout
4870  * event. It walks the ELS ring to get and abort all the IOCBs (except the
4871  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
4872  * invoking the lpfc_sli_issue_abort_iotag() routine.
4873  **/
4874 void
4875 lpfc_els_timeout_handler(struct lpfc_vport *vport)
4876 {
4877         struct lpfc_hba  *phba = vport->phba;
4878         struct lpfc_sli_ring *pring;
4879         struct lpfc_iocbq *tmp_iocb, *piocb;
4880         IOCB_t *cmd = NULL;
4881         struct lpfc_dmabuf *pcmd;
4882         uint32_t els_command = 0;
4883         uint32_t timeout;
4884         uint32_t remote_ID = 0xffffffff;
4885
4886         /* If the timer is already canceled do nothing */
4887         if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
4888                 return;
4889         }
4890         spin_lock_irq(&phba->hbalock);
4891         timeout = (uint32_t)(phba->fc_ratov << 1);
4892
4893         pring = &phba->sli.ring[LPFC_ELS_RING];
4894
4895         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
4896                 cmd = &piocb->iocb;
4897
4898                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
4899                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
4900                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4901                         continue;
4902
4903                 if (piocb->vport != vport)
4904                         continue;
4905
4906                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
4907                 if (pcmd)
4908                         els_command = *(uint32_t *) (pcmd->virt);
4909
4910                 if (els_command == ELS_CMD_FARP ||
4911                     els_command == ELS_CMD_FARPR ||
4912                     els_command == ELS_CMD_FDISC)
4913                         continue;
4914
4915                 if (piocb->drvrTimeout > 0) {
4916                         if (piocb->drvrTimeout >= timeout)
4917                                 piocb->drvrTimeout -= timeout;
4918                         else
4919                                 piocb->drvrTimeout = 0;
4920                         continue;
4921                 }
4922
4923                 remote_ID = 0xffffffff;
4924                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
4925                         remote_ID = cmd->un.elsreq64.remoteID;
4926                 else {
4927                         struct lpfc_nodelist *ndlp;
4928                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
4929                         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4930                                 remote_ID = ndlp->nlp_DID;
4931                 }
4932                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4933                                  "0127 ELS timeout Data: x%x x%x x%x "
4934                                  "x%x\n", els_command,
4935                                  remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
4936                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
4937         }
4938         spin_unlock_irq(&phba->hbalock);
4939
4940         if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
4941                 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
4942 }
4943
4944 /**
4945  * lpfc_els_flush_cmd: Clean up the outstanding els commands to a vport.
4946  * @vport: pointer to a host virtual N_Port data structure.
4947  *
4948  * This routine is used to clean up all the outstanding ELS commands on a
4949  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
4950  * routine. After that, it walks the ELS transmit queue to remove all the
4951  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
4952  * the IOCBs with a non-NULL completion callback function, the callback
4953  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
4954  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
4955  * callback function, the IOCB will simply be released. Finally, it walks
4956  * the ELS transmit completion queue to issue an abort IOCB to any transmit
4957  * completion queue IOCB that is associated with the @vport and is not
4958  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
4959  * part of the discovery state machine) out to HBA by invoking the
4960  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
4961  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
4962  * the IOCBs are aborted when this function returns.
4963  **/
4964 void
4965 lpfc_els_flush_cmd(struct lpfc_vport *vport)
4966 {
4967         LIST_HEAD(completions);
4968         struct lpfc_hba  *phba = vport->phba;
4969         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4970         struct lpfc_iocbq *tmp_iocb, *piocb;
4971         IOCB_t *cmd = NULL;
4972
4973         lpfc_fabric_abort_vport(vport);
4974
4975         spin_lock_irq(&phba->hbalock);
4976         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
4977                 cmd = &piocb->iocb;
4978
4979                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
4980                         continue;
4981                 }
4982
4983                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
4984                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
4985                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
4986                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4987                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
4988                         continue;
4989
4990                 if (piocb->vport != vport)
4991                         continue;
4992
4993                 list_move_tail(&piocb->list, &completions);
4994                 pring->txq_cnt--;
4995         }
4996
4997         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
4998                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
4999                         continue;
5000                 }
5001
5002                 if (piocb->vport != vport)
5003                         continue;
5004
5005                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5006         }
5007         spin_unlock_irq(&phba->hbalock);
5008
5009         while (!list_empty(&completions)) {
5010                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
5011                 cmd = &piocb->iocb;
5012                 list_del_init(&piocb->list);
5013
5014                 if (!piocb->iocb_cmpl)
5015                         lpfc_sli_release_iocbq(phba, piocb);
5016                 else {
5017                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
5018                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
5019                         (piocb->iocb_cmpl) (phba, piocb, piocb);
5020                 }
5021         }
5022
5023         return;
5024 }
5025
5026 /**
5027  * lpfc_els_flush_all_cmd: Clean up all the outstanding els commands to a HBA.
5028  * @phba: pointer to lpfc hba data structure.
5029  *
5030  * This routine is used to clean up all the outstanding ELS commands on a
5031  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
5032  * routine. After that, it walks the ELS transmit queue to remove all the
5033  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
5034  * the IOCBs with the completion callback function associated, the callback
5035  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5036  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
5037  * callback function associated, the IOCB will simply be released. Finally,
5038  * it walks the ELS transmit completion queue to issue an abort IOCB to any
5039  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
5040  * management plane IOCBs that are not part of the discovery state machine)
5041  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
5042  **/
5043 void
5044 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
5045 {
5046         LIST_HEAD(completions);
5047         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5048         struct lpfc_iocbq *tmp_iocb, *piocb;
5049         IOCB_t *cmd = NULL;
5050
5051         lpfc_fabric_abort_hba(phba);
5052         spin_lock_irq(&phba->hbalock);
5053         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5054                 cmd = &piocb->iocb;
5055                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5056                         continue;
5057                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5058                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5059                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5060                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5061                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
5062                         continue;
5063                 list_move_tail(&piocb->list, &completions);
5064                 pring->txq_cnt--;
5065         }
5066         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5067                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5068                         continue;
5069                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5070         }
5071         spin_unlock_irq(&phba->hbalock);
5072         while (!list_empty(&completions)) {
5073                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
5074                 cmd = &piocb->iocb;
5075                 list_del_init(&piocb->list);
5076                 if (!piocb->iocb_cmpl)
5077                         lpfc_sli_release_iocbq(phba, piocb);
5078                 else {
5079                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
5080                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
5081                         (piocb->iocb_cmpl) (phba, piocb, piocb);
5082                 }
5083         }
5084         return;
5085 }
5086
5087 /**
5088  * lpfc_els_unsol_buffer: Process an unsolicited event data buffer.
5089  * @phba: pointer to lpfc hba data structure.
5090  * @pring: pointer to a SLI ring.
5091  * @vport: pointer to a host virtual N_Port data structure.
5092  * @elsiocb: pointer to lpfc els command iocb data structure.
5093  *
5094  * This routine is used for processing the IOCB associated with a unsolicited
5095  * event. It first determines whether there is an existing ndlp that matches
5096  * the DID from the unsolicited IOCB. If not, it will create a new one with
5097  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
5098  * IOCB is then used to invoke the proper routine and to set up proper state
5099  * of the discovery state machine.
5100  **/
5101 static void
5102 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5103                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
5104 {
5105         struct Scsi_Host  *shost;
5106         struct lpfc_nodelist *ndlp;
5107         struct ls_rjt stat;
5108         uint32_t *payload;
5109         uint32_t cmd, did, newnode, rjt_err = 0;
5110         IOCB_t *icmd = &elsiocb->iocb;
5111
5112         if (!vport || !(elsiocb->context2))
5113                 goto dropit;
5114
5115         newnode = 0;
5116         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
5117         cmd = *payload;
5118         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
5119                 lpfc_post_buffer(phba, pring, 1);
5120
5121         did = icmd->un.rcvels.remoteID;
5122         if (icmd->ulpStatus) {
5123                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5124                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
5125                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
5126                 goto dropit;
5127         }
5128
5129         /* Check to see if link went down during discovery */
5130         if (lpfc_els_chk_latt(vport))
5131                 goto dropit;
5132
5133         /* Ignore traffic recevied during vport shutdown. */
5134         if (vport->load_flag & FC_UNLOADING)
5135                 goto dropit;
5136
5137         ndlp = lpfc_findnode_did(vport, did);
5138         if (!ndlp) {
5139                 /* Cannot find existing Fabric ndlp, so allocate a new one */
5140                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5141                 if (!ndlp)
5142                         goto dropit;
5143
5144                 lpfc_nlp_init(vport, ndlp, did);
5145                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5146                 newnode = 1;
5147                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5148                         ndlp->nlp_type |= NLP_FABRIC;
5149         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5150                 ndlp = lpfc_enable_node(vport, ndlp,
5151                                         NLP_STE_UNUSED_NODE);
5152                 if (!ndlp)
5153                         goto dropit;
5154                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5155                 newnode = 1;
5156                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5157                         ndlp->nlp_type |= NLP_FABRIC;
5158         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
5159                 /* This is similar to the new node path */
5160                 ndlp = lpfc_nlp_get(ndlp);
5161                 if (!ndlp)
5162                         goto dropit;
5163                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5164                 newnode = 1;
5165         }
5166
5167         phba->fc_stat.elsRcvFrame++;
5168
5169         elsiocb->context1 = lpfc_nlp_get(ndlp);
5170         elsiocb->vport = vport;
5171
5172         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
5173                 cmd &= ELS_CMD_MASK;
5174         }
5175         /* ELS command <elsCmd> received from NPORT <did> */
5176         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5177                          "0112 ELS command x%x received from NPORT x%x "
5178                          "Data: x%x\n", cmd, did, vport->port_state);
5179         switch (cmd) {
5180         case ELS_CMD_PLOGI:
5181                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5182                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
5183                         did, vport->port_state, ndlp->nlp_flag);
5184
5185                 phba->fc_stat.elsRcvPLOGI++;
5186                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
5187
5188                 if (vport->port_state < LPFC_DISC_AUTH) {
5189                         if (!(phba->pport->fc_flag & FC_PT2PT) ||
5190                                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
5191                                 rjt_err = LSRJT_UNABLE_TPC;
5192                                 break;
5193                         }
5194                         /* We get here, and drop thru, if we are PT2PT with
5195                          * another NPort and the other side has initiated
5196                          * the PLOGI before responding to our FLOGI.
5197                          */
5198                 }
5199
5200                 shost = lpfc_shost_from_vport(vport);
5201                 spin_lock_irq(shost->host_lock);
5202                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
5203                 spin_unlock_irq(shost->host_lock);
5204
5205                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5206                                         NLP_EVT_RCV_PLOGI);
5207
5208                 break;
5209         case ELS_CMD_FLOGI:
5210                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5211                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
5212                         did, vport->port_state, ndlp->nlp_flag);
5213
5214                 phba->fc_stat.elsRcvFLOGI++;
5215                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
5216                 if (newnode)
5217                         lpfc_nlp_put(ndlp);
5218                 break;
5219         case ELS_CMD_LOGO:
5220                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5221                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
5222                         did, vport->port_state, ndlp->nlp_flag);
5223
5224                 phba->fc_stat.elsRcvLOGO++;
5225                 if (vport->port_state < LPFC_DISC_AUTH) {
5226                         rjt_err = LSRJT_UNABLE_TPC;
5227                         break;
5228                 }
5229                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
5230                 break;
5231         case ELS_CMD_PRLO:
5232                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5233                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
5234                         did, vport->port_state, ndlp->nlp_flag);
5235
5236                 phba->fc_stat.elsRcvPRLO++;
5237                 if (vport->port_state < LPFC_DISC_AUTH) {
5238                         rjt_err = LSRJT_UNABLE_TPC;
5239                         break;
5240                 }
5241                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
5242                 break;
5243         case ELS_CMD_RSCN:
5244                 phba->fc_stat.elsRcvRSCN++;
5245                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
5246                 if (newnode)
5247                         lpfc_nlp_put(ndlp);
5248                 break;
5249         case ELS_CMD_ADISC:
5250                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5251                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
5252                         did, vport->port_state, ndlp->nlp_flag);
5253
5254                 phba->fc_stat.elsRcvADISC++;
5255                 if (vport->port_state < LPFC_DISC_AUTH) {
5256                         rjt_err = LSRJT_UNABLE_TPC;
5257                         break;
5258                 }
5259                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5260                                         NLP_EVT_RCV_ADISC);
5261                 break;
5262         case ELS_CMD_PDISC:
5263                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5264                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
5265                         did, vport->port_state, ndlp->nlp_flag);
5266
5267                 phba->fc_stat.elsRcvPDISC++;
5268                 if (vport->port_state < LPFC_DISC_AUTH) {
5269                         rjt_err = LSRJT_UNABLE_TPC;
5270                         break;
5271                 }
5272                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5273                                         NLP_EVT_RCV_PDISC);
5274                 break;
5275         case ELS_CMD_FARPR:
5276                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5277                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
5278                         did, vport->port_state, ndlp->nlp_flag);
5279
5280                 phba->fc_stat.elsRcvFARPR++;
5281                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
5282                 break;
5283         case ELS_CMD_FARP:
5284                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5285                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
5286                         did, vport->port_state, ndlp->nlp_flag);
5287
5288                 phba->fc_stat.elsRcvFARP++;
5289                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
5290                 break;
5291         case ELS_CMD_FAN:
5292                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5293                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
5294                         did, vport->port_state, ndlp->nlp_flag);
5295
5296                 phba->fc_stat.elsRcvFAN++;
5297                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
5298                 break;
5299         case ELS_CMD_PRLI:
5300                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5301                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
5302                         did, vport->port_state, ndlp->nlp_flag);
5303
5304                 phba->fc_stat.elsRcvPRLI++;
5305                 if (vport->port_state < LPFC_DISC_AUTH) {
5306                         rjt_err = LSRJT_UNABLE_TPC;
5307                         break;
5308                 }
5309                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
5310                 break;
5311         case ELS_CMD_LIRR:
5312                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5313                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
5314                         did, vport->port_state, ndlp->nlp_flag);
5315
5316                 phba->fc_stat.elsRcvLIRR++;
5317                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
5318                 if (newnode)
5319                         lpfc_nlp_put(ndlp);
5320                 break;
5321         case ELS_CMD_RPS:
5322                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5323                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
5324                         did, vport->port_state, ndlp->nlp_flag);
5325
5326                 phba->fc_stat.elsRcvRPS++;
5327                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
5328                 if (newnode)
5329                         lpfc_nlp_put(ndlp);
5330                 break;
5331         case ELS_CMD_RPL:
5332                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5333                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
5334                         did, vport->port_state, ndlp->nlp_flag);
5335
5336                 phba->fc_stat.elsRcvRPL++;
5337                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
5338                 if (newnode)
5339                         lpfc_nlp_put(ndlp);
5340                 break;
5341         case ELS_CMD_RNID:
5342                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5343                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
5344                         did, vport->port_state, ndlp->nlp_flag);
5345
5346                 phba->fc_stat.elsRcvRNID++;
5347                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
5348                 if (newnode)
5349                         lpfc_nlp_put(ndlp);
5350                 break;
5351         default:
5352                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5353                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
5354                         cmd, did, vport->port_state);
5355
5356                 /* Unsupported ELS command, reject */
5357                 rjt_err = LSRJT_INVALID_CMD;
5358
5359                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
5360                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5361                                  "0115 Unknown ELS command x%x "
5362                                  "received from NPORT x%x\n", cmd, did);
5363                 if (newnode)
5364                         lpfc_nlp_put(ndlp);
5365                 break;
5366         }
5367
5368         /* check if need to LS_RJT received ELS cmd */
5369         if (rjt_err) {
5370                 memset(&stat, 0, sizeof(stat));
5371                 stat.un.b.lsRjtRsnCode = rjt_err;
5372                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
5373                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
5374                         NULL);
5375         }
5376
5377         lpfc_nlp_put(elsiocb->context1);
5378         elsiocb->context1 = NULL;
5379         return;
5380
5381 dropit:
5382         if (vport && !(vport->load_flag & FC_UNLOADING))
5383                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5384                         "(%d):0111 Dropping received ELS cmd "
5385                         "Data: x%x x%x x%x\n",
5386                         vport->vpi, icmd->ulpStatus,
5387                         icmd->un.ulpWord[4], icmd->ulpTimeout);
5388         phba->fc_stat.elsRcvDrop++;
5389 }
5390
5391 /**
5392  * lpfc_find_vport_by_vpid: Find a vport on a HBA through vport identifier.
5393  * @phba: pointer to lpfc hba data structure.
5394  * @vpi: host virtual N_Port identifier.
5395  *
5396  * This routine finds a vport on a HBA (referred by @phba) through a
5397  * @vpi. The function walks the HBA's vport list and returns the address
5398  * of the vport with the matching @vpi.
5399  *
5400  * Return code
5401  *    NULL - No vport with the matching @vpi found
5402  *    Otherwise - Address to the vport with the matching @vpi.
5403  **/
5404 static struct lpfc_vport *
5405 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5406 {
5407         struct lpfc_vport *vport;
5408         unsigned long flags;
5409
5410         spin_lock_irqsave(&phba->hbalock, flags);
5411         list_for_each_entry(vport, &phba->port_list, listentry) {
5412                 if (vport->vpi == vpi) {
5413                         spin_unlock_irqrestore(&phba->hbalock, flags);
5414                         return vport;
5415                 }
5416         }
5417         spin_unlock_irqrestore(&phba->hbalock, flags);
5418         return NULL;
5419 }
5420
5421 /**
5422  * lpfc_els_unsol_event: Process an unsolicited event from an els sli ring.
5423  * @phba: pointer to lpfc hba data structure.
5424  * @pring: pointer to a SLI ring.
5425  * @elsiocb: pointer to lpfc els iocb data structure.
5426  *
5427  * This routine is used to process an unsolicited event received from a SLI
5428  * (Service Level Interface) ring. The actual processing of the data buffer
5429  * associated with the unsolicited event is done by invoking the routine
5430  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
5431  * SLI ring on which the unsolicited event was received.
5432  **/
5433 void
5434 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5435                      struct lpfc_iocbq *elsiocb)
5436 {
5437         struct lpfc_vport *vport = phba->pport;
5438         IOCB_t *icmd = &elsiocb->iocb;
5439         dma_addr_t paddr;
5440         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
5441         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
5442
5443         elsiocb->context1 = NULL;
5444         elsiocb->context2 = NULL;
5445         elsiocb->context3 = NULL;
5446
5447         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
5448                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
5449         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
5450             (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
5451                 phba->fc_stat.NoRcvBuf++;
5452                 /* Not enough posted buffers; Try posting more buffers */
5453                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
5454                         lpfc_post_buffer(phba, pring, 0);
5455                 return;
5456         }
5457
5458         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5459             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
5460              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
5461                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
5462                         vport = phba->pport;
5463                 else {
5464                         uint16_t vpi = icmd->unsli3.rcvsli3.vpi;
5465                         vport = lpfc_find_vport_by_vpid(phba, vpi);
5466                 }
5467         }
5468         /* If there are no BDEs associated
5469          * with this IOCB, there is nothing to do.
5470          */
5471         if (icmd->ulpBdeCount == 0)
5472                 return;
5473
5474         /* type of ELS cmd is first 32bit word
5475          * in packet
5476          */
5477         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5478                 elsiocb->context2 = bdeBuf1;
5479         } else {
5480                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
5481                                  icmd->un.cont64[0].addrLow);
5482                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
5483                                                              paddr);
5484         }
5485
5486         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5487         /*
5488          * The different unsolicited event handlers would tell us
5489          * if they are done with "mp" by setting context2 to NULL.
5490          */
5491         if (elsiocb->context2) {
5492                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
5493                 elsiocb->context2 = NULL;
5494         }
5495
5496         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
5497         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
5498             icmd->ulpBdeCount == 2) {
5499                 elsiocb->context2 = bdeBuf2;
5500                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5501                 /* free mp if we are done with it */
5502                 if (elsiocb->context2) {
5503                         lpfc_in_buf_free(phba, elsiocb->context2);
5504                         elsiocb->context2 = NULL;
5505                 }
5506         }
5507 }
5508
5509 /**
5510  * lpfc_do_scr_ns_plogi: Issue a plogi to the name server for scr.
5511  * @phba: pointer to lpfc hba data structure.
5512  * @vport: pointer to a virtual N_Port data structure.
5513  *
5514  * This routine issues a Port Login (PLOGI) to the Name Server with
5515  * State Change Request (SCR) for a @vport. This routine will create an
5516  * ndlp for the Name Server associated to the @vport if such node does
5517  * not already exist. The PLOGI to Name Server is issued by invoking the
5518  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
5519  * (FDMI) is configured to the @vport, a FDMI node will be created and
5520  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
5521  **/
5522 void
5523 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5524 {
5525         struct lpfc_nodelist *ndlp, *ndlp_fdmi;
5526
5527         ndlp = lpfc_findnode_did(vport, NameServer_DID);
5528         if (!ndlp) {
5529                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5530                 if (!ndlp) {
5531                         if (phba->fc_topology == TOPOLOGY_LOOP) {
5532                                 lpfc_disc_start(vport);
5533                                 return;
5534                         }
5535                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5536                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5537                                          "0251 NameServer login: no memory\n");
5538                         return;
5539                 }
5540                 lpfc_nlp_init(vport, ndlp, NameServer_DID);
5541         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5542                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5543                 if (!ndlp) {
5544                         if (phba->fc_topology == TOPOLOGY_LOOP) {
5545                                 lpfc_disc_start(vport);
5546                                 return;
5547                         }
5548                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5549                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5550                                         "0348 NameServer login: node freed\n");
5551                         return;
5552                 }
5553         }
5554         ndlp->nlp_type |= NLP_FABRIC;
5555
5556         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5557
5558         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
5559                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5560                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5561                                  "0252 Cannot issue NameServer login\n");
5562                 return;
5563         }
5564
5565         if (vport->cfg_fdmi_on) {
5566                 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
5567                                           GFP_KERNEL);
5568                 if (ndlp_fdmi) {
5569                         lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
5570                         ndlp_fdmi->nlp_type |= NLP_FABRIC;
5571                         lpfc_nlp_set_state(vport, ndlp_fdmi,
5572                                 NLP_STE_PLOGI_ISSUE);
5573                         lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
5574                                              0);
5575                 }
5576         }
5577         return;
5578 }
5579
5580 /**
5581  * lpfc_cmpl_reg_new_vport: Completion callback function to register new vport.
5582  * @phba: pointer to lpfc hba data structure.
5583  * @pmb: pointer to the driver internal queue element for mailbox command.
5584  *
5585  * This routine is the completion callback function to register new vport
5586  * mailbox command. If the new vport mailbox command completes successfully,
5587  * the fabric registration login shall be performed on physical port (the
5588  * new vport created is actually a physical port, with VPI 0) or the port
5589  * login to Name Server for State Change Request (SCR) will be performed
5590  * on virtual port (real virtual port, with VPI greater than 0).
5591  **/
5592 static void
5593 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5594 {
5595         struct lpfc_vport *vport = pmb->vport;
5596         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
5597         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5598         MAILBOX_t *mb = &pmb->mb;
5599
5600         spin_lock_irq(shost->host_lock);
5601         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
5602         spin_unlock_irq(shost->host_lock);
5603
5604         if (mb->mbxStatus) {
5605                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5606                                  "0915 Register VPI failed: 0x%x\n",
5607                                  mb->mbxStatus);
5608
5609                 switch (mb->mbxStatus) {
5610                 case 0x11:      /* unsupported feature */
5611                 case 0x9603:    /* max_vpi exceeded */
5612                 case 0x9602:    /* Link event since CLEAR_LA */
5613                         /* giving up on vport registration */
5614                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5615                         spin_lock_irq(shost->host_lock);
5616                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
5617                         spin_unlock_irq(shost->host_lock);
5618                         lpfc_can_disctmo(vport);
5619                         break;
5620                 default:
5621                         /* Try to recover from this error */
5622                         lpfc_mbx_unreg_vpi(vport);
5623                         spin_lock_irq(shost->host_lock);
5624                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
5625                         spin_unlock_irq(shost->host_lock);
5626                         if (vport->port_type == LPFC_PHYSICAL_PORT)
5627                                 lpfc_initial_flogi(vport);
5628                         else
5629                                 lpfc_initial_fdisc(vport);
5630                         break;
5631                 }
5632
5633         } else {
5634                 if (vport == phba->pport)
5635                         lpfc_issue_fabric_reglogin(vport);
5636                 else
5637                         lpfc_do_scr_ns_plogi(phba, vport);
5638         }
5639
5640         /* Now, we decrement the ndlp reference count held for this
5641          * callback function
5642          */
5643         lpfc_nlp_put(ndlp);
5644
5645         mempool_free(pmb, phba->mbox_mem_pool);
5646         return;
5647 }
5648
5649 /**
5650  * lpfc_register_new_vport: Register a new vport with a HBA.
5651  * @phba: pointer to lpfc hba data structure.
5652  * @vport: pointer to a host virtual N_Port data structure.
5653  * @ndlp: pointer to a node-list data structure.
5654  *
5655  * This routine registers the @vport as a new virtual port with a HBA.
5656  * It is done through a registering vpi mailbox command.
5657  **/
5658 static void
5659 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
5660                         struct lpfc_nodelist *ndlp)
5661 {
5662         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5663         LPFC_MBOXQ_t *mbox;
5664
5665         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5666         if (mbox) {
5667                 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, mbox);
5668                 mbox->vport = vport;
5669                 mbox->context2 = lpfc_nlp_get(ndlp);
5670                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
5671                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5672                     == MBX_NOT_FINISHED) {
5673                         /* mailbox command not success, decrement ndlp
5674                          * reference count for this command
5675                          */
5676                         lpfc_nlp_put(ndlp);
5677                         mempool_free(mbox, phba->mbox_mem_pool);
5678
5679                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5680                                 "0253 Register VPI: Can't send mbox\n");
5681                         goto mbox_err_exit;
5682                 }
5683         } else {
5684                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5685                                  "0254 Register VPI: no memory\n");
5686                 goto mbox_err_exit;
5687         }
5688         return;
5689
5690 mbox_err_exit:
5691         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5692         spin_lock_irq(shost->host_lock);
5693         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
5694         spin_unlock_irq(shost->host_lock);
5695         return;
5696 }
5697
5698 /**
5699  * lpfc_cmpl_els_fdisc: Completion function for fdisc iocb command.
5700  * @phba: pointer to lpfc hba data structure.
5701  * @cmdiocb: pointer to lpfc command iocb data structure.
5702  * @rspiocb: pointer to lpfc response iocb data structure.
5703  *
5704  * This routine is the completion callback function to a Fabric Discover
5705  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
5706  * single threaded, each FDISC completion callback function will reset
5707  * the discovery timer for all vports such that the timers will not get
5708  * unnecessary timeout. The function checks the FDISC IOCB status. If error
5709  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
5710  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
5711  * assigned to the vport has been changed with the completion of the FDISC
5712  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
5713  * are unregistered from the HBA, and then the lpfc_register_new_vport()
5714  * routine is invoked to register new vport with the HBA. Otherwise, the
5715  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
5716  * Server for State Change Request (SCR).
5717  **/
5718 static void
5719 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5720                     struct lpfc_iocbq *rspiocb)
5721 {
5722         struct lpfc_vport *vport = cmdiocb->vport;
5723         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
5724         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
5725         struct lpfc_nodelist *np;
5726         struct lpfc_nodelist *next_np;
5727         IOCB_t *irsp = &rspiocb->iocb;
5728         struct lpfc_iocbq *piocb;
5729
5730         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5731                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
5732                          irsp->ulpStatus, irsp->un.ulpWord[4],
5733                          vport->fc_prevDID);
5734         /* Since all FDISCs are being single threaded, we
5735          * must reset the discovery timer for ALL vports
5736          * waiting to send FDISC when one completes.
5737          */
5738         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
5739                 lpfc_set_disctmo(piocb->vport);
5740         }
5741
5742         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5743                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
5744                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
5745
5746         if (irsp->ulpStatus) {
5747                 /* Check for retry */
5748                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
5749                         goto out;
5750                 /* FDISC failed */
5751                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5752                                  "0126 FDISC failed. (%d/%d)\n",
5753                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
5754                 goto fdisc_failed;
5755         }
5756                 if (vport->fc_vport->vport_state == FC_VPORT_INITIALIZING)
5757                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5758                 lpfc_nlp_put(ndlp);
5759                 /* giving up on FDISC. Cancel discovery timer */
5760                 lpfc_can_disctmo(vport);
5761         spin_lock_irq(shost->host_lock);
5762         vport->fc_flag |= FC_FABRIC;
5763         if (vport->phba->fc_topology == TOPOLOGY_LOOP)
5764                 vport->fc_flag |=  FC_PUBLIC_LOOP;
5765         spin_unlock_irq(shost->host_lock);
5766
5767         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
5768         lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
5769         if ((vport->fc_prevDID != vport->fc_myDID) &&
5770                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
5771                 /* If our NportID changed, we need to ensure all
5772                  * remaining NPORTs get unreg_login'ed so we can
5773                  * issue unreg_vpi.
5774                  */
5775                 list_for_each_entry_safe(np, next_np,
5776                         &vport->fc_nodes, nlp_listp) {
5777                         if (!NLP_CHK_NODE_ACT(ndlp) ||
5778                             (np->nlp_state != NLP_STE_NPR_NODE) ||
5779                             !(np->nlp_flag & NLP_NPR_ADISC))
5780                                 continue;
5781                         spin_lock_irq(shost->host_lock);
5782                         np->nlp_flag &= ~NLP_NPR_ADISC;
5783                         spin_unlock_irq(shost->host_lock);
5784                         lpfc_unreg_rpi(vport, np);
5785                 }
5786                 lpfc_mbx_unreg_vpi(vport);
5787                 spin_lock_irq(shost->host_lock);
5788                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
5789                 spin_unlock_irq(shost->host_lock);
5790         }
5791
5792         if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
5793                 lpfc_register_new_vport(phba, vport, ndlp);
5794         else
5795                 lpfc_do_scr_ns_plogi(phba, vport);
5796         goto out;
5797 fdisc_failed:
5798         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5799         /* Cancel discovery timer */
5800         lpfc_can_disctmo(vport);
5801         lpfc_nlp_put(ndlp);
5802 out:
5803         lpfc_els_free_iocb(phba, cmdiocb);
5804 }
5805
5806 /**
5807  * lpfc_issue_els_fdisc: Issue a fdisc iocb command.
5808  * @vport: pointer to a virtual N_Port data structure.
5809  * @ndlp: pointer to a node-list data structure.
5810  * @retry: number of retries to the command IOCB.
5811  *
5812  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
5813  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
5814  * routine to issue the IOCB, which makes sure only one outstanding fabric
5815  * IOCB will be sent off HBA at any given time.
5816  *
5817  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5818  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5819  * will be stored into the context1 field of the IOCB for the completion
5820  * callback function to the FDISC ELS command.
5821  *
5822  * Return code
5823  *   0 - Successfully issued fdisc iocb command
5824  *   1 - Failed to issue fdisc iocb command
5825  **/
5826 static int
5827 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5828                      uint8_t retry)
5829 {
5830         struct lpfc_hba *phba = vport->phba;
5831         IOCB_t *icmd;
5832         struct lpfc_iocbq *elsiocb;
5833         struct serv_parm *sp;
5834         uint8_t *pcmd;
5835         uint16_t cmdsize;
5836         int did = ndlp->nlp_DID;
5837         int rc;
5838
5839         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
5840         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
5841                                      ELS_CMD_FDISC);
5842         if (!elsiocb) {
5843                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5844                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5845                                  "0255 Issue FDISC: no IOCB\n");
5846                 return 1;
5847         }
5848
5849         icmd = &elsiocb->iocb;
5850         icmd->un.elsreq64.myID = 0;
5851         icmd->un.elsreq64.fl = 1;
5852
5853         /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
5854         icmd->ulpCt_h = 1;
5855         icmd->ulpCt_l = 0;
5856
5857         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5858         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
5859         pcmd += sizeof(uint32_t); /* CSP Word 1 */
5860         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
5861         sp = (struct serv_parm *) pcmd;
5862         /* Setup CSPs accordingly for Fabric */
5863         sp->cmn.e_d_tov = 0;
5864         sp->cmn.w2.r_a_tov = 0;
5865         sp->cls1.classValid = 0;
5866         sp->cls2.seqDelivery = 1;
5867         sp->cls3.seqDelivery = 1;
5868
5869         pcmd += sizeof(uint32_t); /* CSP Word 2 */
5870         pcmd += sizeof(uint32_t); /* CSP Word 3 */
5871         pcmd += sizeof(uint32_t); /* CSP Word 4 */
5872         pcmd += sizeof(uint32_t); /* Port Name */
5873         memcpy(pcmd, &vport->fc_portname, 8);
5874         pcmd += sizeof(uint32_t); /* Node Name */
5875         pcmd += sizeof(uint32_t); /* Node Name */
5876         memcpy(pcmd, &vport->fc_nodename, 8);
5877
5878         lpfc_set_disctmo(vport);
5879
5880         phba->fc_stat.elsXmitFDISC++;
5881         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
5882
5883         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5884                 "Issue FDISC:     did:x%x",
5885                 did, 0, 0);
5886
5887         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
5888         if (rc == IOCB_ERROR) {
5889                 lpfc_els_free_iocb(phba, elsiocb);
5890                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5891                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5892                                  "0256 Issue FDISC: Cannot send IOCB\n");
5893                 return 1;
5894         }
5895         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
5896         vport->port_state = LPFC_FDISC;
5897         return 0;
5898 }
5899
5900 /**
5901  * lpfc_cmpl_els_npiv_logo: Completion function with vport logo.
5902  * @phba: pointer to lpfc hba data structure.
5903  * @cmdiocb: pointer to lpfc command iocb data structure.
5904  * @rspiocb: pointer to lpfc response iocb data structure.
5905  *
5906  * This routine is the completion callback function to the issuing of a LOGO
5907  * ELS command off a vport. It frees the command IOCB and then decrement the
5908  * reference count held on ndlp for this completion function, indicating that
5909  * the reference to the ndlp is no long needed. Note that the
5910  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
5911  * callback function and an additional explicit ndlp reference decrementation
5912  * will trigger the actual release of the ndlp.
5913  **/
5914 static void
5915 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5916                         struct lpfc_iocbq *rspiocb)
5917 {
5918         struct lpfc_vport *vport = cmdiocb->vport;
5919         IOCB_t *irsp;
5920         struct lpfc_nodelist *ndlp;
5921         ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
5922
5923         irsp = &rspiocb->iocb;
5924         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5925                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
5926                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
5927
5928         lpfc_els_free_iocb(phba, cmdiocb);
5929         vport->unreg_vpi_cmpl = VPORT_ERROR;
5930
5931         /* Trigger the release of the ndlp after logo */
5932         lpfc_nlp_put(ndlp);
5933 }
5934
5935 /**
5936  * lpfc_issue_els_npiv_logo: Issue a logo off a vport.
5937  * @vport: pointer to a virtual N_Port data structure.
5938  * @ndlp: pointer to a node-list data structure.
5939  *
5940  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
5941  *
5942  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5943  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5944  * will be stored into the context1 field of the IOCB for the completion
5945  * callback function to the LOGO ELS command.
5946  *
5947  * Return codes
5948  *   0 - Successfully issued logo off the @vport
5949  *   1 - Failed to issue logo off the @vport
5950  **/
5951 int
5952 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5953 {
5954         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5955         struct lpfc_hba  *phba = vport->phba;
5956         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5957         IOCB_t *icmd;
5958         struct lpfc_iocbq *elsiocb;
5959         uint8_t *pcmd;
5960         uint16_t cmdsize;
5961
5962         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
5963         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
5964                                      ELS_CMD_LOGO);
5965         if (!elsiocb)
5966                 return 1;
5967
5968         icmd = &elsiocb->iocb;
5969         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5970         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
5971         pcmd += sizeof(uint32_t);
5972
5973         /* Fill in LOGO payload */
5974         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
5975         pcmd += sizeof(uint32_t);
5976         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
5977
5978         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5979                 "Issue LOGO npiv  did:x%x flg:x%x",
5980                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
5981
5982         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
5983         spin_lock_irq(shost->host_lock);
5984         ndlp->nlp_flag |= NLP_LOGO_SND;
5985         spin_unlock_irq(shost->host_lock);
5986         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
5987                 spin_lock_irq(shost->host_lock);
5988                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
5989                 spin_unlock_irq(shost->host_lock);
5990                 lpfc_els_free_iocb(phba, elsiocb);
5991                 return 1;
5992         }
5993         return 0;
5994 }
5995
5996 /**
5997  * lpfc_fabric_block_timeout: Handler function to the fabric block timer.
5998  * @ptr: holder for the timer function associated data.
5999  *
6000  * This routine is invoked by the fabric iocb block timer after
6001  * timeout. It posts the fabric iocb block timeout event by setting the
6002  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
6003  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
6004  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
6005  * posted event WORKER_FABRIC_BLOCK_TMO.
6006  **/
6007 void
6008 lpfc_fabric_block_timeout(unsigned long ptr)
6009 {
6010         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
6011         unsigned long iflags;
6012         uint32_t tmo_posted;
6013
6014         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
6015         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
6016         if (!tmo_posted)
6017                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
6018         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
6019
6020         if (!tmo_posted)
6021                 lpfc_worker_wake_up(phba);
6022         return;
6023 }
6024
6025 /**
6026  * lpfc_resume_fabric_iocbs: Issue a fabric iocb from driver internal list.
6027  * @phba: pointer to lpfc hba data structure.
6028  *
6029  * This routine issues one fabric iocb from the driver internal list to
6030  * the HBA. It first checks whether it's ready to issue one fabric iocb to
6031  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
6032  * remove one pending fabric iocb from the driver internal list and invokes
6033  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
6034  **/
6035 static void
6036 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
6037 {
6038         struct lpfc_iocbq *iocb;
6039         unsigned long iflags;
6040         int ret;
6041         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6042         IOCB_t *cmd;
6043
6044 repeat:
6045         iocb = NULL;
6046         spin_lock_irqsave(&phba->hbalock, iflags);
6047         /* Post any pending iocb to the SLI layer */
6048         if (atomic_read(&phba->fabric_iocb_count) == 0) {
6049                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
6050                                  list);
6051                 if (iocb)
6052                         /* Increment fabric iocb count to hold the position */
6053                         atomic_inc(&phba->fabric_iocb_count);
6054         }
6055         spin_unlock_irqrestore(&phba->hbalock, iflags);
6056         if (iocb) {
6057                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6058                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6059                 iocb->iocb_flag |= LPFC_IO_FABRIC;
6060
6061                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6062                         "Fabric sched1:   ste:x%x",
6063                         iocb->vport->port_state, 0, 0);
6064
6065                 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
6066
6067                 if (ret == IOCB_ERROR) {
6068                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6069                         iocb->fabric_iocb_cmpl = NULL;
6070                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6071                         cmd = &iocb->iocb;
6072                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6073                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6074                         iocb->iocb_cmpl(phba, iocb, iocb);
6075
6076                         atomic_dec(&phba->fabric_iocb_count);
6077                         goto repeat;
6078                 }
6079         }
6080
6081         return;
6082 }
6083
6084 /**
6085  * lpfc_unblock_fabric_iocbs: Unblock issuing fabric iocb command.
6086  * @phba: pointer to lpfc hba data structure.
6087  *
6088  * This routine unblocks the  issuing fabric iocb command. The function
6089  * will clear the fabric iocb block bit and then invoke the routine
6090  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
6091  * from the driver internal fabric iocb list.
6092  **/
6093 void
6094 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
6095 {
6096         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6097
6098         lpfc_resume_fabric_iocbs(phba);
6099         return;
6100 }
6101
6102 /**
6103  * lpfc_block_fabric_iocbs: Block issuing fabric iocb command.
6104  * @phba: pointer to lpfc hba data structure.
6105  *
6106  * This routine blocks the issuing fabric iocb for a specified amount of
6107  * time (currently 100 ms). This is done by set the fabric iocb block bit
6108  * and set up a timeout timer for 100ms. When the block bit is set, no more
6109  * fabric iocb will be issued out of the HBA.
6110  **/
6111 static void
6112 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
6113 {
6114         int blocked;
6115
6116         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6117         /* Start a timer to unblock fabric iocbs after 100ms */
6118         if (!blocked)
6119                 mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
6120
6121         return;
6122 }
6123
6124 /**
6125  * lpfc_cmpl_fabric_iocb: Completion callback function for fabric iocb.
6126  * @phba: pointer to lpfc hba data structure.
6127  * @cmdiocb: pointer to lpfc command iocb data structure.
6128  * @rspiocb: pointer to lpfc response iocb data structure.
6129  *
6130  * This routine is the callback function that is put to the fabric iocb's
6131  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
6132  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
6133  * function first restores and invokes the original iocb's callback function
6134  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
6135  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
6136  **/
6137 static void
6138 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6139         struct lpfc_iocbq *rspiocb)
6140 {
6141         struct ls_rjt stat;
6142
6143         if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
6144                 BUG();
6145
6146         switch (rspiocb->iocb.ulpStatus) {
6147                 case IOSTAT_NPORT_RJT:
6148                 case IOSTAT_FABRIC_RJT:
6149                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
6150                                 lpfc_block_fabric_iocbs(phba);
6151                         }
6152                         break;
6153
6154                 case IOSTAT_NPORT_BSY:
6155                 case IOSTAT_FABRIC_BSY:
6156                         lpfc_block_fabric_iocbs(phba);
6157                         break;
6158
6159                 case IOSTAT_LS_RJT:
6160                         stat.un.lsRjtError =
6161                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
6162                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
6163                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
6164                                 lpfc_block_fabric_iocbs(phba);
6165                         break;
6166         }
6167
6168         if (atomic_read(&phba->fabric_iocb_count) == 0)
6169                 BUG();
6170
6171         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
6172         cmdiocb->fabric_iocb_cmpl = NULL;
6173         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
6174         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
6175
6176         atomic_dec(&phba->fabric_iocb_count);
6177         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
6178                 /* Post any pending iocbs to HBA */
6179                 lpfc_resume_fabric_iocbs(phba);
6180         }
6181 }
6182
6183 /**
6184  * lpfc_issue_fabric_iocb: Issue a fabric iocb command.
6185  * @phba: pointer to lpfc hba data structure.
6186  * @iocb: pointer to lpfc command iocb data structure.
6187  *
6188  * This routine is used as the top-level API for issuing a fabric iocb command
6189  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
6190  * function makes sure that only one fabric bound iocb will be outstanding at
6191  * any given time. As such, this function will first check to see whether there
6192  * is already an outstanding fabric iocb on the wire. If so, it will put the
6193  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
6194  * issued later. Otherwise, it will issue the iocb on the wire and update the
6195  * fabric iocb count it indicate that there is one fabric iocb on the wire.
6196  *
6197  * Note, this implementation has a potential sending out fabric IOCBs out of
6198  * order. The problem is caused by the construction of the "ready" boolen does
6199  * not include the condition that the internal fabric IOCB list is empty. As
6200  * such, it is possible a fabric IOCB issued by this routine might be "jump"
6201  * ahead of the fabric IOCBs in the internal list.
6202  *
6203  * Return code
6204  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
6205  *   IOCB_ERROR - failed to issue fabric iocb
6206  **/
6207 static int
6208 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
6209 {
6210         unsigned long iflags;
6211         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6212         int ready;
6213         int ret;
6214
6215         if (atomic_read(&phba->fabric_iocb_count) > 1)
6216                 BUG();
6217
6218         spin_lock_irqsave(&phba->hbalock, iflags);
6219         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
6220                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6221
6222         if (ready)
6223                 /* Increment fabric iocb count to hold the position */
6224                 atomic_inc(&phba->fabric_iocb_count);
6225         spin_unlock_irqrestore(&phba->hbalock, iflags);
6226         if (ready) {
6227                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6228                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6229                 iocb->iocb_flag |= LPFC_IO_FABRIC;
6230
6231                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6232                         "Fabric sched2:   ste:x%x",
6233                         iocb->vport->port_state, 0, 0);
6234
6235                 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
6236
6237                 if (ret == IOCB_ERROR) {
6238                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6239                         iocb->fabric_iocb_cmpl = NULL;
6240                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6241                         atomic_dec(&phba->fabric_iocb_count);
6242                 }
6243         } else {
6244                 spin_lock_irqsave(&phba->hbalock, iflags);
6245                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
6246                 spin_unlock_irqrestore(&phba->hbalock, iflags);
6247                 ret = IOCB_SUCCESS;
6248         }
6249         return ret;
6250 }
6251
6252 /**
6253  * lpfc_fabric_abort_vport: Abort a vport's iocbs from driver fabric iocb list.
6254  * @vport: pointer to a virtual N_Port data structure.
6255  *
6256  * This routine aborts all the IOCBs associated with a @vport from the
6257  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6258  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6259  * list, removes each IOCB associated with the @vport off the list, set the
6260  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6261  * associated with the IOCB.
6262  **/
6263 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
6264 {
6265         LIST_HEAD(completions);
6266         struct lpfc_hba  *phba = vport->phba;
6267         struct lpfc_iocbq *tmp_iocb, *piocb;
6268         IOCB_t *cmd;
6269
6270         spin_lock_irq(&phba->hbalock);
6271         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6272                                  list) {
6273
6274                 if (piocb->vport != vport)
6275                         continue;
6276
6277                 list_move_tail(&piocb->list, &completions);
6278         }
6279         spin_unlock_irq(&phba->hbalock);
6280
6281         while (!list_empty(&completions)) {
6282                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
6283                 list_del_init(&piocb->list);
6284
6285                 cmd = &piocb->iocb;
6286                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6287                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6288                 (piocb->iocb_cmpl) (phba, piocb, piocb);
6289         }
6290 }
6291
6292 /**
6293  * lpfc_fabric_abort_nport: Abort a ndlp's iocbs from driver fabric iocb list.
6294  * @ndlp: pointer to a node-list data structure.
6295  *
6296  * This routine aborts all the IOCBs associated with an @ndlp from the
6297  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6298  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6299  * list, removes each IOCB associated with the @ndlp off the list, set the
6300  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6301  * associated with the IOCB.
6302  **/
6303 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
6304 {
6305         LIST_HEAD(completions);
6306         struct lpfc_hba  *phba = ndlp->vport->phba;
6307         struct lpfc_iocbq *tmp_iocb, *piocb;
6308         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6309         IOCB_t *cmd;
6310
6311         spin_lock_irq(&phba->hbalock);
6312         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6313                                  list) {
6314                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
6315
6316                         list_move_tail(&piocb->list, &completions);
6317                 }
6318         }
6319         spin_unlock_irq(&phba->hbalock);
6320
6321         while (!list_empty(&completions)) {
6322                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
6323                 list_del_init(&piocb->list);
6324
6325                 cmd = &piocb->iocb;
6326                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6327                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6328                 (piocb->iocb_cmpl) (phba, piocb, piocb);
6329         }
6330 }
6331
6332 /**
6333  * lpfc_fabric_abort_hba: Abort all iocbs on driver fabric iocb list.
6334  * @phba: pointer to lpfc hba data structure.
6335  *
6336  * This routine aborts all the IOCBs currently on the driver internal
6337  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
6338  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
6339  * list, removes IOCBs off the list, set the status feild to
6340  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
6341  * the IOCB.
6342  **/
6343 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
6344 {
6345         LIST_HEAD(completions);
6346         struct lpfc_iocbq *piocb;
6347         IOCB_t *cmd;
6348
6349         spin_lock_irq(&phba->hbalock);
6350         list_splice_init(&phba->fabric_iocb_list, &completions);
6351         spin_unlock_irq(&phba->hbalock);
6352
6353         while (!list_empty(&completions)) {
6354                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
6355                 list_del_init(&piocb->list);
6356
6357                 cmd = &piocb->iocb;
6358                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6359                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6360                 (piocb->iocb_cmpl) (phba, piocb, piocb);
6361         }
6362 }