1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Broadcom. All Rights Reserved. The term
4 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
8 #include "../libefc_sli/sli4.h"
13 efc_nport_free_resources(struct efc_nport *nport, int evt, void *data)
15 struct efc *efc = nport->efc;
17 /* Clear the nport attached flag */
18 nport->attached = false;
20 /* Free the service parameters buffer */
21 if (nport->dma.virt) {
22 dma_free_coherent(&efc->pci->dev, nport->dma.size,
23 nport->dma.virt, nport->dma.phys);
24 memset(&nport->dma, 0, sizeof(struct efc_dma));
27 /* Free the SLI resources */
28 sli_resource_free(efc->sli, SLI4_RSRC_VPI, nport->indicator);
30 efc_nport_cb(efc, evt, nport);
34 efc_nport_get_mbox_status(struct efc_nport *nport, u8 *mqe, int status)
36 struct efc *efc = nport->efc;
37 struct sli4_mbox_command_header *hdr =
38 (struct sli4_mbox_command_header *)mqe;
40 if (status || le16_to_cpu(hdr->status)) {
41 efc_log_debug(efc, "bad status vpi=%#x st=%x hdr=%x\n",
42 nport->indicator, status, le16_to_cpu(hdr->status));
50 efc_nport_free_unreg_vpi_cb(struct efc *efc, int status, u8 *mqe, void *arg)
52 struct efc_nport *nport = arg;
53 int evt = EFC_EVT_NPORT_FREE_OK;
56 rc = efc_nport_get_mbox_status(nport, mqe, status);
58 evt = EFC_EVT_NPORT_FREE_FAIL;
60 efc_nport_free_resources(nport, evt, mqe);
65 efc_nport_free_unreg_vpi(struct efc_nport *nport)
67 struct efc *efc = nport->efc;
69 u8 data[SLI4_BMBX_SIZE];
71 rc = sli_cmd_unreg_vpi(efc->sli, data, nport->indicator,
72 SLI4_UNREG_TYPE_PORT);
74 efc_log_err(efc, "UNREG_VPI format failure\n");
75 efc_nport_free_resources(nport, EFC_EVT_NPORT_FREE_FAIL, data);
79 rc = efc->tt.issue_mbox_rqst(efc->base, data,
80 efc_nport_free_unreg_vpi_cb, nport);
82 efc_log_err(efc, "UNREG_VPI command failure\n");
83 efc_nport_free_resources(nport, EFC_EVT_NPORT_FREE_FAIL, data);
88 efc_nport_send_evt(struct efc_nport *nport, int evt, void *data)
90 struct efc *efc = nport->efc;
92 /* Now inform the registered callbacks */
93 efc_nport_cb(efc, evt, nport);
95 /* Set the nport attached flag */
96 if (evt == EFC_EVT_NPORT_ATTACH_OK)
97 nport->attached = true;
99 /* If there is a pending free request, then handle it now */
100 if (nport->free_req_pending)
101 efc_nport_free_unreg_vpi(nport);
105 efc_nport_alloc_init_vpi_cb(struct efc *efc, int status, u8 *mqe, void *arg)
107 struct efc_nport *nport = arg;
109 if (efc_nport_get_mbox_status(nport, mqe, status)) {
110 efc_nport_free_resources(nport, EFC_EVT_NPORT_ALLOC_FAIL, mqe);
114 efc_nport_send_evt(nport, EFC_EVT_NPORT_ALLOC_OK, mqe);
119 efc_nport_alloc_init_vpi(struct efc_nport *nport)
121 struct efc *efc = nport->efc;
122 u8 data[SLI4_BMBX_SIZE];
125 /* If there is a pending free request, then handle it now */
126 if (nport->free_req_pending) {
127 efc_nport_free_resources(nport, EFC_EVT_NPORT_FREE_OK, data);
131 rc = sli_cmd_init_vpi(efc->sli, data,
132 nport->indicator, nport->domain->indicator);
134 efc_log_err(efc, "INIT_VPI format failure\n");
135 efc_nport_free_resources(nport, EFC_EVT_NPORT_ALLOC_FAIL, data);
139 rc = efc->tt.issue_mbox_rqst(efc->base, data,
140 efc_nport_alloc_init_vpi_cb, nport);
142 efc_log_err(efc, "INIT_VPI command failure\n");
143 efc_nport_free_resources(nport, EFC_EVT_NPORT_ALLOC_FAIL, data);
148 efc_nport_alloc_read_sparm64_cb(struct efc *efc, int status, u8 *mqe, void *arg)
150 struct efc_nport *nport = arg;
153 if (efc_nport_get_mbox_status(nport, mqe, status)) {
154 efc_nport_free_resources(nport, EFC_EVT_NPORT_ALLOC_FAIL, mqe);
158 payload = nport->dma.virt;
160 memcpy(&nport->sli_wwpn, payload + SLI4_READ_SPARM64_WWPN_OFFSET,
161 sizeof(nport->sli_wwpn));
162 memcpy(&nport->sli_wwnn, payload + SLI4_READ_SPARM64_WWNN_OFFSET,
163 sizeof(nport->sli_wwnn));
165 dma_free_coherent(&efc->pci->dev, nport->dma.size, nport->dma.virt,
167 memset(&nport->dma, 0, sizeof(struct efc_dma));
168 efc_nport_alloc_init_vpi(nport);
173 efc_nport_alloc_read_sparm64(struct efc *efc, struct efc_nport *nport)
175 u8 data[SLI4_BMBX_SIZE];
178 /* Allocate memory for the service parameters */
179 nport->dma.size = EFC_SPARAM_DMA_SZ;
180 nport->dma.virt = dma_alloc_coherent(&efc->pci->dev,
181 nport->dma.size, &nport->dma.phys,
183 if (!nport->dma.virt) {
184 efc_log_err(efc, "Failed to allocate DMA memory\n");
185 efc_nport_free_resources(nport, EFC_EVT_NPORT_ALLOC_FAIL, data);
189 rc = sli_cmd_read_sparm64(efc->sli, data,
190 &nport->dma, nport->indicator);
192 efc_log_err(efc, "READ_SPARM64 format failure\n");
193 efc_nport_free_resources(nport, EFC_EVT_NPORT_ALLOC_FAIL, data);
197 rc = efc->tt.issue_mbox_rqst(efc->base, data,
198 efc_nport_alloc_read_sparm64_cb, nport);
200 efc_log_err(efc, "READ_SPARM64 command failure\n");
201 efc_nport_free_resources(nport, EFC_EVT_NPORT_ALLOC_FAIL, data);
206 efc_cmd_nport_alloc(struct efc *efc, struct efc_nport *nport,
207 struct efc_domain *domain, u8 *wwpn)
211 nport->indicator = U32_MAX;
212 nport->free_req_pending = false;
215 memcpy(&nport->sli_wwpn, wwpn, sizeof(nport->sli_wwpn));
218 * allocate a VPI object for the port and stores it in the
219 * indicator field of the port object.
221 if (sli_resource_alloc(efc->sli, SLI4_RSRC_VPI,
222 &nport->indicator, &index)) {
223 efc_log_err(efc, "VPI allocation failure\n");
229 * If the WWPN is NULL, fetch the default
230 * WWPN and WWNN before initializing the VPI
233 efc_nport_alloc_read_sparm64(efc, nport);
235 efc_nport_alloc_init_vpi(nport);
237 /* domain NULL and wwpn non-NULL */
238 efc_log_err(efc, "need WWN for physical port\n");
239 sli_resource_free(efc->sli, SLI4_RSRC_VPI, nport->indicator);
247 efc_nport_attach_reg_vpi_cb(struct efc *efc, int status, u8 *mqe,
250 struct efc_nport *nport = arg;
252 nport->attaching = false;
253 if (efc_nport_get_mbox_status(nport, mqe, status)) {
254 efc_nport_free_resources(nport, EFC_EVT_NPORT_ATTACH_FAIL, mqe);
258 efc_nport_send_evt(nport, EFC_EVT_NPORT_ATTACH_OK, mqe);
263 efc_cmd_nport_attach(struct efc *efc, struct efc_nport *nport, u32 fc_id)
265 u8 buf[SLI4_BMBX_SIZE];
269 efc_log_err(efc, "bad param(s) nport=%p\n", nport);
273 nport->fc_id = fc_id;
275 /* register previously-allocated VPI with the device */
276 rc = sli_cmd_reg_vpi(efc->sli, buf, nport->fc_id,
277 nport->sli_wwpn, nport->indicator,
278 nport->domain->indicator, false);
280 efc_log_err(efc, "REG_VPI format failure\n");
281 efc_nport_free_resources(nport, EFC_EVT_NPORT_ATTACH_FAIL, buf);
285 rc = efc->tt.issue_mbox_rqst(efc->base, buf,
286 efc_nport_attach_reg_vpi_cb, nport);
288 efc_log_err(efc, "REG_VPI command failure\n");
289 efc_nport_free_resources(nport, EFC_EVT_NPORT_ATTACH_FAIL, buf);
291 nport->attaching = true;
298 efc_cmd_nport_free(struct efc *efc, struct efc_nport *nport)
301 efc_log_err(efc, "bad parameter(s) nport=%p\n", nport);
305 /* Issue the UNREG_VPI command to free the assigned VPI context */
307 efc_nport_free_unreg_vpi(nport);
308 else if (nport->attaching)
309 nport->free_req_pending = true;
311 efc_sm_post_event(&nport->sm, EFC_EVT_NPORT_FREE_OK, NULL);
317 efc_domain_get_mbox_status(struct efc_domain *domain, u8 *mqe, int status)
319 struct efc *efc = domain->efc;
320 struct sli4_mbox_command_header *hdr =
321 (struct sli4_mbox_command_header *)mqe;
323 if (status || le16_to_cpu(hdr->status)) {
324 efc_log_debug(efc, "bad status vfi=%#x st=%x hdr=%x\n",
325 domain->indicator, status,
326 le16_to_cpu(hdr->status));
334 efc_domain_free_resources(struct efc_domain *domain, int evt, void *data)
336 struct efc *efc = domain->efc;
338 /* Free the service parameters buffer */
339 if (domain->dma.virt) {
340 dma_free_coherent(&efc->pci->dev,
341 domain->dma.size, domain->dma.virt,
343 memset(&domain->dma, 0, sizeof(struct efc_dma));
346 /* Free the SLI resources */
347 sli_resource_free(efc->sli, SLI4_RSRC_VFI, domain->indicator);
349 efc_domain_cb(efc, evt, domain);
353 efc_domain_send_nport_evt(struct efc_domain *domain,
354 int port_evt, int domain_evt, void *data)
356 struct efc *efc = domain->efc;
358 /* Send alloc/attach ok to the physical nport */
359 efc_nport_send_evt(domain->nport, port_evt, NULL);
361 /* Now inform the registered callbacks */
362 efc_domain_cb(efc, domain_evt, domain);
366 efc_domain_alloc_read_sparm64_cb(struct efc *efc, int status, u8 *mqe,
369 struct efc_domain *domain = arg;
371 if (efc_domain_get_mbox_status(domain, mqe, status)) {
372 efc_domain_free_resources(domain,
373 EFC_HW_DOMAIN_ALLOC_FAIL, mqe);
377 efc_domain_send_nport_evt(domain, EFC_EVT_NPORT_ALLOC_OK,
378 EFC_HW_DOMAIN_ALLOC_OK, mqe);
383 efc_domain_alloc_read_sparm64(struct efc_domain *domain)
385 struct efc *efc = domain->efc;
386 u8 data[SLI4_BMBX_SIZE];
389 rc = sli_cmd_read_sparm64(efc->sli, data, &domain->dma, 0);
391 efc_log_err(efc, "READ_SPARM64 format failure\n");
392 efc_domain_free_resources(domain,
393 EFC_HW_DOMAIN_ALLOC_FAIL, data);
397 rc = efc->tt.issue_mbox_rqst(efc->base, data,
398 efc_domain_alloc_read_sparm64_cb, domain);
400 efc_log_err(efc, "READ_SPARM64 command failure\n");
401 efc_domain_free_resources(domain,
402 EFC_HW_DOMAIN_ALLOC_FAIL, data);
407 efc_domain_alloc_init_vfi_cb(struct efc *efc, int status, u8 *mqe,
410 struct efc_domain *domain = arg;
412 if (efc_domain_get_mbox_status(domain, mqe, status)) {
413 efc_domain_free_resources(domain,
414 EFC_HW_DOMAIN_ALLOC_FAIL, mqe);
418 efc_domain_alloc_read_sparm64(domain);
423 efc_domain_alloc_init_vfi(struct efc_domain *domain)
425 struct efc *efc = domain->efc;
426 struct efc_nport *nport = domain->nport;
427 u8 data[SLI4_BMBX_SIZE];
431 * For FC, the HW alread registered an FCFI.
432 * Copy FCF information into the domain and jump to INIT_VFI.
434 domain->fcf_indicator = efc->fcfi;
435 rc = sli_cmd_init_vfi(efc->sli, data, domain->indicator,
436 domain->fcf_indicator, nport->indicator);
438 efc_log_err(efc, "INIT_VFI format failure\n");
439 efc_domain_free_resources(domain,
440 EFC_HW_DOMAIN_ALLOC_FAIL, data);
444 efc_log_err(efc, "%s issue mbox\n", __func__);
445 rc = efc->tt.issue_mbox_rqst(efc->base, data,
446 efc_domain_alloc_init_vfi_cb, domain);
448 efc_log_err(efc, "INIT_VFI command failure\n");
449 efc_domain_free_resources(domain,
450 EFC_HW_DOMAIN_ALLOC_FAIL, data);
455 efc_cmd_domain_alloc(struct efc *efc, struct efc_domain *domain, u32 fcf)
459 if (!domain || !domain->nport) {
460 efc_log_err(efc, "bad parameter(s) domain=%p nport=%p\n",
461 domain, domain ? domain->nport : NULL);
465 /* allocate memory for the service parameters */
466 domain->dma.size = EFC_SPARAM_DMA_SZ;
467 domain->dma.virt = dma_alloc_coherent(&efc->pci->dev,
469 &domain->dma.phys, GFP_KERNEL);
470 if (!domain->dma.virt) {
471 efc_log_err(efc, "Failed to allocate DMA memory\n");
476 domain->fcf_indicator = U32_MAX;
477 domain->indicator = U32_MAX;
479 if (sli_resource_alloc(efc->sli, SLI4_RSRC_VFI, &domain->indicator,
481 efc_log_err(efc, "VFI allocation failure\n");
483 dma_free_coherent(&efc->pci->dev,
484 domain->dma.size, domain->dma.virt,
486 memset(&domain->dma, 0, sizeof(struct efc_dma));
491 efc_domain_alloc_init_vfi(domain);
496 efc_domain_attach_reg_vfi_cb(struct efc *efc, int status, u8 *mqe,
499 struct efc_domain *domain = arg;
501 if (efc_domain_get_mbox_status(domain, mqe, status)) {
502 efc_domain_free_resources(domain,
503 EFC_HW_DOMAIN_ATTACH_FAIL, mqe);
507 efc_domain_send_nport_evt(domain, EFC_EVT_NPORT_ATTACH_OK,
508 EFC_HW_DOMAIN_ATTACH_OK, mqe);
513 efc_cmd_domain_attach(struct efc *efc, struct efc_domain *domain, u32 fc_id)
515 u8 buf[SLI4_BMBX_SIZE];
519 efc_log_err(efc, "bad param(s) domain=%p\n", domain);
523 domain->nport->fc_id = fc_id;
525 rc = sli_cmd_reg_vfi(efc->sli, buf, SLI4_BMBX_SIZE, domain->indicator,
526 domain->fcf_indicator, domain->dma,
527 domain->nport->indicator, domain->nport->sli_wwpn,
528 domain->nport->fc_id);
530 efc_log_err(efc, "REG_VFI format failure\n");
534 rc = efc->tt.issue_mbox_rqst(efc->base, buf,
535 efc_domain_attach_reg_vfi_cb, domain);
537 efc_log_err(efc, "REG_VFI command failure\n");
544 efc_domain_free_resources(domain, EFC_HW_DOMAIN_ATTACH_FAIL, buf);
550 efc_domain_free_unreg_vfi_cb(struct efc *efc, int status, u8 *mqe, void *arg)
552 struct efc_domain *domain = arg;
553 int evt = EFC_HW_DOMAIN_FREE_OK;
556 rc = efc_domain_get_mbox_status(domain, mqe, status);
558 evt = EFC_HW_DOMAIN_FREE_FAIL;
562 efc_domain_free_resources(domain, evt, mqe);
567 efc_domain_free_unreg_vfi(struct efc_domain *domain)
569 struct efc *efc = domain->efc;
571 u8 data[SLI4_BMBX_SIZE];
573 rc = sli_cmd_unreg_vfi(efc->sli, data, domain->indicator,
574 SLI4_UNREG_TYPE_DOMAIN);
576 efc_log_err(efc, "UNREG_VFI format failure\n");
580 rc = efc->tt.issue_mbox_rqst(efc->base, data,
581 efc_domain_free_unreg_vfi_cb, domain);
583 efc_log_err(efc, "UNREG_VFI command failure\n");
590 efc_domain_free_resources(domain, EFC_HW_DOMAIN_FREE_FAIL, data);
594 efc_cmd_domain_free(struct efc *efc, struct efc_domain *domain)
597 efc_log_err(efc, "bad parameter(s) domain=%p\n", domain);
601 efc_domain_free_unreg_vfi(domain);
606 efc_cmd_node_alloc(struct efc *efc, struct efc_remote_node *rnode, u32 fc_addr,
607 struct efc_nport *nport)
609 /* Check for invalid indicator */
610 if (rnode->indicator != U32_MAX) {
612 "RPI allocation failure addr=%#x rpi=%#x\n",
613 fc_addr, rnode->indicator);
617 /* NULL SLI port indicates an unallocated remote node */
620 if (sli_resource_alloc(efc->sli, SLI4_RSRC_RPI,
621 &rnode->indicator, &rnode->index)) {
622 efc_log_err(efc, "RPI allocation failure addr=%#x\n",
627 rnode->fc_id = fc_addr;
628 rnode->nport = nport;
634 efc_cmd_node_attach_cb(struct efc *efc, int status, u8 *mqe, void *arg)
636 struct efc_remote_node *rnode = arg;
637 struct sli4_mbox_command_header *hdr =
638 (struct sli4_mbox_command_header *)mqe;
641 if (status || le16_to_cpu(hdr->status)) {
642 efc_log_debug(efc, "bad status cqe=%#x mqe=%#x\n", status,
643 le16_to_cpu(hdr->status));
644 rnode->attached = false;
645 evt = EFC_EVT_NODE_ATTACH_FAIL;
647 rnode->attached = true;
648 evt = EFC_EVT_NODE_ATTACH_OK;
651 efc_remote_node_cb(efc, evt, rnode);
657 efc_cmd_node_attach(struct efc *efc, struct efc_remote_node *rnode,
658 struct efc_dma *sparms)
661 u8 buf[SLI4_BMBX_SIZE];
663 if (!rnode || !sparms) {
664 efc_log_err(efc, "bad parameter(s) rnode=%p sparms=%p\n",
670 * If the attach count is non-zero, this RPI has already been reg'd.
671 * Otherwise, register the RPI
673 if (rnode->index == U32_MAX) {
674 efc_log_err(efc, "bad parameter rnode->index invalid\n");
678 /* Update a remote node object with the remote port's service params */
679 if (!sli_cmd_reg_rpi(efc->sli, buf, rnode->indicator,
680 rnode->nport->indicator, rnode->fc_id, sparms, 0, 0))
681 rc = efc->tt.issue_mbox_rqst(efc->base, buf,
682 efc_cmd_node_attach_cb, rnode);
688 efc_node_free_resources(struct efc *efc, struct efc_remote_node *rnode)
693 efc_log_err(efc, "bad parameter rnode=%p\n", rnode);
698 if (rnode->attached) {
699 efc_log_err(efc, "rnode is still attached\n");
702 if (rnode->indicator != U32_MAX) {
703 if (sli_resource_free(efc->sli, SLI4_RSRC_RPI,
706 "RPI free fail RPI %d addr=%#x\n",
707 rnode->indicator, rnode->fc_id);
710 rnode->indicator = U32_MAX;
711 rnode->index = U32_MAX;
720 efc_cmd_node_free_cb(struct efc *efc, int status, u8 *mqe, void *arg)
722 struct efc_remote_node *rnode = arg;
723 struct sli4_mbox_command_header *hdr =
724 (struct sli4_mbox_command_header *)mqe;
725 int evt = EFC_EVT_NODE_FREE_FAIL;
728 if (status || le16_to_cpu(hdr->status)) {
729 efc_log_debug(efc, "bad status cqe=%#x mqe=%#x\n", status,
730 le16_to_cpu(hdr->status));
733 * In certain cases, a non-zero MQE status is OK (all must be
738 if (!rnode->attached ||
739 (le16_to_cpu(hdr->status) != SLI4_MBX_STATUS_RPI_NOT_REG))
744 rnode->attached = false;
745 evt = EFC_EVT_NODE_FREE_OK;
748 efc_remote_node_cb(efc, evt, rnode);
754 efc_cmd_node_detach(struct efc *efc, struct efc_remote_node *rnode)
756 u8 buf[SLI4_BMBX_SIZE];
760 efc_log_err(efc, "bad parameter rnode=%p\n", rnode);
765 if (!rnode->attached)
770 if (!sli_cmd_unreg_rpi(efc->sli, buf, rnode->indicator,
771 SLI4_RSRC_RPI, U32_MAX))
772 rc = efc->tt.issue_mbox_rqst(efc->base, buf,
773 efc_cmd_node_free_cb, rnode);
776 efc_log_err(efc, "UNREG_RPI failed\n");