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