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