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.
7 #include "efct_driver.h"
10 #include "efct_unsol.h"
11 #include "efct_scsi.h"
13 LIST_HEAD(efct_devices
);
16 module_param(logmask
, int, 0444);
17 MODULE_PARM_DESC(logmask
, "logging bitmask (default 0)");
19 static struct libefc_function_template efct_libefc_templ
= {
20 .issue_mbox_rqst
= efct_issue_mbox_rqst
,
21 .send_els
= efct_els_hw_srrs_send
,
22 .send_bls
= efct_efc_bls_send
,
24 .new_nport
= efct_scsi_tgt_new_nport
,
25 .del_nport
= efct_scsi_tgt_del_nport
,
26 .scsi_new_node
= efct_scsi_new_initiator
,
27 .scsi_del_node
= efct_scsi_del_initiator
,
28 .hw_seq_free
= efct_efc_hw_sequence_free
,
32 efct_device_init(void)
36 /* driver-wide init for target-server */
37 rc
= efct_scsi_tgt_driver_init();
39 pr_err("efct_scsi_tgt_init failed rc=%d\n", rc
);
43 rc
= efct_scsi_reg_fc_transport();
45 efct_scsi_tgt_driver_exit();
46 pr_err("failed to register to FC host\n");
54 efct_device_shutdown(void)
56 efct_scsi_release_fc_transport();
58 efct_scsi_tgt_driver_exit();
62 efct_device_alloc(u32 nid
)
64 struct efct
*efct
= NULL
;
66 efct
= kzalloc_node(sizeof(*efct
), GFP_KERNEL
, nid
);
70 INIT_LIST_HEAD(&efct
->list_entry
);
71 list_add_tail(&efct
->list_entry
, &efct_devices
);
77 efct_teardown_msix(struct efct
*efct
)
81 for (i
= 0; i
< efct
->n_msix_vec
; i
++) {
82 free_irq(pci_irq_vector(efct
->pci
, i
),
83 &efct
->intr_context
[i
]);
86 pci_free_irq_vectors(efct
->pci
);
90 efct_efclib_config(struct efct
*efct
, struct libefc_function_template
*tt
)
96 efc
= kzalloc(sizeof(*efc
), GFP_KERNEL
);
102 memcpy(&efc
->tt
, tt
, sizeof(*tt
));
104 efc
->pci
= efct
->pci
;
106 efc
->def_wwnn
= efct_get_wwnn(&efct
->hw
);
107 efc
->def_wwpn
= efct_get_wwpn(&efct
->hw
);
109 efc
->log_level
= EFC_LOG_LIB
;
112 efc
->max_xfer_size
= sli
->sge_supported_length
*
113 sli_get_max_sgl(&efct
->hw
.sli
);
115 efc
->fcfi
= efct
->hw
.fcf_indicator
;
117 rc
= efcport_init(efc
);
119 efc_log_err(efc
, "efcport_init failed\n");
124 static int efct_request_firmware_update(struct efct
*efct
);
127 efct_pci_model(u16 device
)
130 case EFCT_DEVICE_LANCER_G6
: return "LPE31004";
131 case EFCT_DEVICE_LANCER_G7
: return "LPE36000";
132 default: return "unknown";
137 efct_device_attach(struct efct
*efct
)
141 if (efct
->attached
) {
142 efc_log_err(efct
, "Device is already attached\n");
146 snprintf(efct
->name
, sizeof(efct
->name
), "[%s%d] ", "fc",
147 efct
->instance_index
);
149 efct
->logmask
= logmask
;
150 efct
->filter_def
= EFCT_DEFAULT_FILTER
;
151 efct
->max_isr_time_msec
= EFCT_OS_MAX_ISR_TIME_MSEC
;
153 efct
->model
= efct_pci_model(efct
->pci
->device
);
155 efct
->efct_req_fw_upgrade
= true;
157 /* Allocate transport object and bring online */
158 efct
->xport
= efct_xport_alloc(efct
);
160 efc_log_err(efct
, "failed to allocate transport object\n");
165 rc
= efct_xport_attach(efct
->xport
);
167 efc_log_err(efct
, "failed to attach transport object\n");
171 rc
= efct_xport_initialize(efct
->xport
);
173 efc_log_err(efct
, "failed to initialize transport object\n");
177 rc
= efct_efclib_config(efct
, &efct_libefc_templ
);
179 efc_log_err(efct
, "failed to init efclib\n");
183 for (i
= 0; i
< efct
->n_msix_vec
; i
++) {
184 efc_log_debug(efct
, "irq %d enabled\n", i
);
185 enable_irq(pci_irq_vector(efct
->pci
, i
));
188 efct
->attached
= true;
190 if (efct
->efct_req_fw_upgrade
)
191 efct_request_firmware_update(efct
);
196 efct_xport_detach(efct
->xport
);
198 efct_xport_free(efct
->xport
);
205 efct_device_detach(struct efct
*efct
)
209 if (!efct
|| !efct
->attached
) {
210 pr_err("Device is not attached\n");
214 if (efct_xport_control(efct
->xport
, EFCT_XPORT_SHUTDOWN
))
215 efc_log_err(efct
, "Transport Shutdown timed out\n");
217 for (i
= 0; i
< efct
->n_msix_vec
; i
++)
218 disable_irq(pci_irq_vector(efct
->pci
, i
));
220 efct_xport_detach(efct
->xport
);
222 efct_xport_free(efct
->xport
);
225 efcport_destroy(efct
->efcport
);
226 kfree(efct
->efcport
);
228 efct
->attached
= false;
234 efct_fw_write_cb(int status
, u32 actual_write_length
,
235 u32 change_status
, void *arg
)
237 struct efct_fw_write_result
*result
= arg
;
239 result
->status
= status
;
240 result
->actual_xfer
= actual_write_length
;
241 result
->change_status
= change_status
;
243 complete(&result
->done
);
247 efct_firmware_write(struct efct
*efct
, const u8
*buf
, size_t buf_len
,
256 struct efct_fw_write_result result
;
258 init_completion(&result
.done
);
260 bytes_left
= buf_len
;
263 dma
.size
= FW_WRITE_BUFSIZE
;
264 dma
.virt
= dma_alloc_coherent(&efct
->pci
->dev
,
265 dma
.size
, &dma
.phys
, GFP_KERNEL
);
269 while (bytes_left
> 0) {
270 if (bytes_left
> FW_WRITE_BUFSIZE
)
271 xfer_size
= FW_WRITE_BUFSIZE
;
273 xfer_size
= bytes_left
;
275 memcpy(dma
.virt
, buf
+ offset
, xfer_size
);
277 if (bytes_left
== xfer_size
)
280 efct_hw_firmware_write(&efct
->hw
, &dma
, xfer_size
, offset
,
281 last
, efct_fw_write_cb
, &result
);
283 if (wait_for_completion_interruptible(&result
.done
) != 0) {
288 if (result
.actual_xfer
== 0 || result
.status
!= 0) {
294 *change_status
= result
.change_status
;
296 bytes_left
-= result
.actual_xfer
;
297 offset
+= result
.actual_xfer
;
300 dma_free_coherent(&efct
->pci
->dev
, dma
.size
, dma
.virt
, dma
.phys
);
305 efct_fw_reset(struct efct
*efct
)
308 * Firmware reset to activate the new firmware.
309 * Function 0 will update and load the new firmware
312 if (timer_pending(&efct
->xport
->stats_timer
))
313 del_timer(&efct
->xport
->stats_timer
);
315 if (efct_hw_reset(&efct
->hw
, EFCT_HW_RESET_FIRMWARE
)) {
316 efc_log_info(efct
, "failed to reset firmware\n");
320 efc_log_info(efct
, "successfully reset firmware.Now resetting port\n");
322 efct_device_detach(efct
);
323 return efct_device_attach(efct
);
327 efct_request_firmware_update(struct efct
*efct
)
330 u8 file_name
[256], fw_change_status
= 0;
331 const struct firmware
*fw
;
332 struct efct_hw_grp_hdr
*fw_image
;
334 snprintf(file_name
, 256, "%s.grp", efct
->model
);
336 rc
= request_firmware(&fw
, file_name
, &efct
->pci
->dev
);
338 efc_log_debug(efct
, "Firmware file(%s) not found.\n", file_name
);
342 fw_image
= (struct efct_hw_grp_hdr
*)fw
->data
;
344 if (!strncmp(efct
->hw
.sli
.fw_name
[0], fw_image
->revision
,
345 strnlen(fw_image
->revision
, 16))) {
347 "Skip update. Firmware is already up to date.\n");
351 efc_log_info(efct
, "Firmware update is initiated. %s -> %s\n",
352 efct
->hw
.sli
.fw_name
[0], fw_image
->revision
);
354 rc
= efct_firmware_write(efct
, fw
->data
, fw
->size
, &fw_change_status
);
356 efc_log_err(efct
, "Firmware update failed. rc = %d\n", rc
);
360 efc_log_info(efct
, "Firmware updated successfully\n");
361 switch (fw_change_status
) {
363 efc_log_info(efct
, "New firmware is active.\n");
367 "System reboot needed to activate the new firmware\n");
372 "firmware reset to activate the new firmware\n");
376 efc_log_info(efct
, "Unexpected value change_status:%d\n",
382 release_firmware(fw
);
388 efct_device_free(struct efct
*efct
)
391 list_del(&efct
->list_entry
);
397 efct_device_interrupts_required(struct efct
*efct
)
401 rc
= efct_hw_setup(&efct
->hw
, efct
, efct
->pci
);
405 return efct
->hw
.config
.n_eq
;
409 efct_intr_thread(int irq
, void *handle
)
411 struct efct_intr_context
*intr_ctx
= handle
;
412 struct efct
*efct
= intr_ctx
->efct
;
414 efct_hw_process(&efct
->hw
, intr_ctx
->index
, efct
->max_isr_time_msec
);
419 efct_intr_msix(int irq
, void *handle
)
421 return IRQ_WAKE_THREAD
;
425 efct_setup_msix(struct efct
*efct
, u32 num_intrs
)
429 if (!pci_find_capability(efct
->pci
, PCI_CAP_ID_MSIX
)) {
430 dev_err(&efct
->pci
->dev
,
431 "%s : MSI-X not available\n", __func__
);
435 efct
->n_msix_vec
= num_intrs
;
437 rc
= pci_alloc_irq_vectors(efct
->pci
, num_intrs
, num_intrs
,
438 PCI_IRQ_MSIX
| PCI_IRQ_AFFINITY
);
441 dev_err(&efct
->pci
->dev
, "Failed to alloc irq : %d\n", rc
);
445 for (i
= 0; i
< num_intrs
; i
++) {
446 struct efct_intr_context
*intr_ctx
= NULL
;
448 intr_ctx
= &efct
->intr_context
[i
];
449 intr_ctx
->efct
= efct
;
452 rc
= request_threaded_irq(pci_irq_vector(efct
->pci
, i
),
453 efct_intr_msix
, efct_intr_thread
, 0,
454 EFCT_DRIVER_NAME
, intr_ctx
);
456 dev_err(&efct
->pci
->dev
,
457 "Failed to register %d vector: %d\n", i
, rc
);
466 free_irq(pci_irq_vector(efct
->pci
, i
),
467 &efct
->intr_context
[i
]);
469 pci_free_irq_vectors(efct
->pci
);
473 static struct pci_device_id efct_pci_table
[] = {
474 {PCI_DEVICE(EFCT_VENDOR_ID
, EFCT_DEVICE_LANCER_G6
), 0},
475 {PCI_DEVICE(EFCT_VENDOR_ID
, EFCT_DEVICE_LANCER_G7
), 0},
476 {} /* terminate list */
480 efct_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
482 struct efct
*efct
= NULL
;
485 int num_interrupts
= 0;
488 dev_info(&pdev
->dev
, "%s\n", EFCT_DRIVER_NAME
);
490 rc
= pci_enable_device_mem(pdev
);
494 pci_set_master(pdev
);
496 rc
= pci_set_mwi(pdev
);
498 dev_info(&pdev
->dev
, "pci_set_mwi returned %d\n", rc
);
502 rc
= pci_request_regions(pdev
, EFCT_DRIVER_NAME
);
504 dev_err(&pdev
->dev
, "pci_request_regions failed %d\n", rc
);
505 goto req_regions_out
;
508 /* Fetch the Numa node id for this device */
509 nid
= dev_to_node(&pdev
->dev
);
511 dev_err(&pdev
->dev
, "Warning Numa node ID is %d\n", nid
);
516 efct
= efct_device_alloc(nid
);
518 dev_err(&pdev
->dev
, "Failed to allocate efct\n");
524 efct
->numa_node
= nid
;
526 /* Map all memory BARs */
527 for (i
= 0, r
= 0; i
< EFCT_PCI_MAX_REGS
; i
++) {
528 if (pci_resource_flags(pdev
, i
) & IORESOURCE_MEM
) {
529 efct
->reg
[r
] = ioremap(pci_resource_start(pdev
, i
),
530 pci_resource_len(pdev
, i
));
535 * If the 64-bit attribute is set, both this BAR and the
536 * next form the complete address. Skip processing the
539 if (pci_resource_flags(pdev
, i
) & IORESOURCE_MEM_64
)
543 pci_set_drvdata(pdev
, efct
);
545 rc
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
547 dev_err(&pdev
->dev
, "setting DMA_BIT_MASK failed\n");
551 num_interrupts
= efct_device_interrupts_required(efct
);
552 if (num_interrupts
< 0) {
553 efc_log_err(efct
, "efct_device_interrupts_required failed\n");
559 * Initialize MSIX interrupts, note,
560 * efct_setup_msix() enables the interrupt
562 rc
= efct_setup_msix(efct
, num_interrupts
);
564 dev_err(&pdev
->dev
, "Can't setup msix\n");
567 /* Disable interrupt for now */
568 for (i
= 0; i
< efct
->n_msix_vec
; i
++) {
569 efc_log_debug(efct
, "irq %d disabled\n", i
);
570 disable_irq(pci_irq_vector(efct
->pci
, i
));
573 rc
= efct_device_attach(efct
);
580 efct_teardown_msix(efct
);
582 pci_set_drvdata(pdev
, NULL
);
584 for (i
= 0; i
< EFCT_PCI_MAX_REGS
; i
++) {
586 iounmap(efct
->reg
[i
]);
588 efct_device_free(efct
);
590 pci_release_regions(pdev
);
594 pci_disable_device(pdev
);
599 efct_pci_remove(struct pci_dev
*pdev
)
601 struct efct
*efct
= pci_get_drvdata(pdev
);
607 efct_device_detach(efct
);
609 efct_teardown_msix(efct
);
611 for (i
= 0; i
< EFCT_PCI_MAX_REGS
; i
++) {
613 iounmap(efct
->reg
[i
]);
616 pci_set_drvdata(pdev
, NULL
);
618 efct_device_free(efct
);
620 pci_release_regions(pdev
);
622 pci_disable_device(pdev
);
626 efct_device_prep_for_reset(struct efct
*efct
, struct pci_dev
*pdev
)
630 "PCI channel disable preparing for reset\n");
631 efct_device_detach(efct
);
632 /* Disable interrupt and pci device */
633 efct_teardown_msix(efct
);
635 pci_disable_device(pdev
);
639 efct_device_prep_for_recover(struct efct
*efct
)
642 efc_log_debug(efct
, "PCI channel preparing for recovery\n");
643 efct_hw_io_abort_all(&efct
->hw
);
648 * efct_pci_io_error_detected - method for handling PCI I/O error
649 * @pdev: pointer to PCI device.
650 * @state: the current PCI connection state.
652 * This routine is registered to the PCI subsystem for error handling. This
653 * function is called by the PCI subsystem after a PCI bus error affecting
654 * this device has been detected. When this routine is invoked, it dispatches
655 * device error detected handling routine, which will perform the proper
656 * error detected operation.
659 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
660 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
662 static pci_ers_result_t
663 efct_pci_io_error_detected(struct pci_dev
*pdev
, pci_channel_state_t state
)
665 struct efct
*efct
= pci_get_drvdata(pdev
);
669 case pci_channel_io_normal
:
670 efct_device_prep_for_recover(efct
);
671 rc
= PCI_ERS_RESULT_CAN_RECOVER
;
673 case pci_channel_io_frozen
:
674 efct_device_prep_for_reset(efct
, pdev
);
675 rc
= PCI_ERS_RESULT_NEED_RESET
;
677 case pci_channel_io_perm_failure
:
678 efct_device_detach(efct
);
679 rc
= PCI_ERS_RESULT_DISCONNECT
;
682 efc_log_debug(efct
, "Unknown PCI error state:0x%x\n", state
);
683 efct_device_prep_for_reset(efct
, pdev
);
684 rc
= PCI_ERS_RESULT_NEED_RESET
;
691 static pci_ers_result_t
692 efct_pci_io_slot_reset(struct pci_dev
*pdev
)
695 struct efct
*efct
= pci_get_drvdata(pdev
);
697 rc
= pci_enable_device_mem(pdev
);
699 efc_log_err(efct
, "failed to enable PCI device after reset\n");
700 return PCI_ERS_RESULT_DISCONNECT
;
704 * As the new kernel behavior of pci_restore_state() API call clears
705 * device saved_state flag, need to save the restored state again.
708 pci_save_state(pdev
);
710 pci_set_master(pdev
);
712 rc
= efct_setup_msix(efct
, efct
->n_msix_vec
);
714 efc_log_err(efct
, "rc %d returned, IRQ allocation failed\n",
717 /* Perform device reset */
718 efct_device_detach(efct
);
719 /* Bring device to online*/
720 efct_device_attach(efct
);
722 return PCI_ERS_RESULT_RECOVERED
;
726 efct_pci_io_resume(struct pci_dev
*pdev
)
728 struct efct
*efct
= pci_get_drvdata(pdev
);
730 /* Perform device reset */
731 efct_device_detach(efct
);
732 /* Bring device to online*/
733 efct_device_attach(efct
);
736 MODULE_DEVICE_TABLE(pci
, efct_pci_table
);
738 static struct pci_error_handlers efct_pci_err_handler
= {
739 .error_detected
= efct_pci_io_error_detected
,
740 .slot_reset
= efct_pci_io_slot_reset
,
741 .resume
= efct_pci_io_resume
,
744 static struct pci_driver efct_pci_driver
= {
745 .name
= EFCT_DRIVER_NAME
,
746 .id_table
= efct_pci_table
,
747 .probe
= efct_pci_probe
,
748 .remove
= efct_pci_remove
,
749 .err_handler
= &efct_pci_err_handler
,
753 int __init
efct_init(void)
757 rc
= efct_device_init();
759 pr_err("efct_device_init failed rc=%d\n", rc
);
763 rc
= pci_register_driver(&efct_pci_driver
);
765 pr_err("pci_register_driver failed rc=%d\n", rc
);
766 efct_device_shutdown();
772 static void __exit
efct_exit(void)
774 pci_unregister_driver(&efct_pci_driver
);
775 efct_device_shutdown();
778 module_init(efct_init
);
779 module_exit(efct_exit
);
780 MODULE_VERSION(EFCT_DRIVER_VERSION
);
781 MODULE_DESCRIPTION("Emulex Fibre Channel Target driver");
782 MODULE_LICENSE("GPL");
783 MODULE_AUTHOR("Broadcom");