Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / scsi / elx / efct / efct_driver.c
blob55d2301bfd7de81da17e43408046f347415a3e0a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2021 Broadcom. All Rights Reserved. The term
4 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
5 */
7 #include "efct_driver.h"
9 #include "efct_hw.h"
10 #include "efct_unsol.h"
11 #include "efct_scsi.h"
13 LIST_HEAD(efct_devices);
15 static int logmask;
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,
31 static int
32 efct_device_init(void)
34 int rc;
36 /* driver-wide init for target-server */
37 rc = efct_scsi_tgt_driver_init();
38 if (rc) {
39 pr_err("efct_scsi_tgt_init failed rc=%d\n", rc);
40 return rc;
43 rc = efct_scsi_reg_fc_transport();
44 if (rc) {
45 efct_scsi_tgt_driver_exit();
46 pr_err("failed to register to FC host\n");
47 return rc;
50 return 0;
53 static void
54 efct_device_shutdown(void)
56 efct_scsi_release_fc_transport();
58 efct_scsi_tgt_driver_exit();
61 static void *
62 efct_device_alloc(u32 nid)
64 struct efct *efct = NULL;
66 efct = kzalloc_node(sizeof(*efct), GFP_KERNEL, nid);
67 if (!efct)
68 return efct;
70 INIT_LIST_HEAD(&efct->list_entry);
71 list_add_tail(&efct->list_entry, &efct_devices);
73 return efct;
76 static void
77 efct_teardown_msix(struct efct *efct)
79 u32 i;
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);
89 static int
90 efct_efclib_config(struct efct *efct, struct libefc_function_template *tt)
92 struct efc *efc;
93 struct sli4 *sli;
94 int rc = 0;
96 efc = kzalloc(sizeof(*efc), GFP_KERNEL);
97 if (!efc)
98 return -ENOMEM;
100 efct->efcport = efc;
102 memcpy(&efc->tt, tt, sizeof(*tt));
103 efc->base = efct;
104 efc->pci = efct->pci;
106 efc->def_wwnn = efct_get_wwnn(&efct->hw);
107 efc->def_wwpn = efct_get_wwpn(&efct->hw);
108 efc->enable_tgt = 1;
109 efc->log_level = EFC_LOG_LIB;
111 sli = &efct->hw.sli;
112 efc->max_xfer_size = sli->sge_supported_length *
113 sli_get_max_sgl(&efct->hw.sli);
114 efc->sli = sli;
115 efc->fcfi = efct->hw.fcf_indicator;
117 rc = efcport_init(efc);
118 if (rc)
119 efc_log_err(efc, "efcport_init failed\n");
121 return rc;
124 static int efct_request_firmware_update(struct efct *efct);
126 static const char*
127 efct_pci_model(u16 device)
129 switch (device) {
130 case EFCT_DEVICE_LANCER_G6: return "LPE31004";
131 case EFCT_DEVICE_LANCER_G7: return "LPE36000";
132 default: return "unknown";
136 static int
137 efct_device_attach(struct efct *efct)
139 u32 rc = 0, i = 0;
141 if (efct->attached) {
142 efc_log_err(efct, "Device is already attached\n");
143 return -EIO;
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);
159 if (!efct->xport) {
160 efc_log_err(efct, "failed to allocate transport object\n");
161 rc = -ENOMEM;
162 goto out;
165 rc = efct_xport_attach(efct->xport);
166 if (rc) {
167 efc_log_err(efct, "failed to attach transport object\n");
168 goto xport_out;
171 rc = efct_xport_initialize(efct->xport);
172 if (rc) {
173 efc_log_err(efct, "failed to initialize transport object\n");
174 goto xport_out;
177 rc = efct_efclib_config(efct, &efct_libefc_templ);
178 if (rc) {
179 efc_log_err(efct, "failed to init efclib\n");
180 goto efclib_out;
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);
193 return rc;
195 efclib_out:
196 efct_xport_detach(efct->xport);
197 xport_out:
198 efct_xport_free(efct->xport);
199 efct->xport = NULL;
200 out:
201 return rc;
204 static int
205 efct_device_detach(struct efct *efct)
207 int i;
209 if (!efct || !efct->attached) {
210 pr_err("Device is not attached\n");
211 return -EIO;
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);
223 efct->xport = NULL;
225 efcport_destroy(efct->efcport);
226 kfree(efct->efcport);
228 efct->attached = false;
230 return 0;
233 static void
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);
246 static int
247 efct_firmware_write(struct efct *efct, const u8 *buf, size_t buf_len,
248 u8 *change_status)
250 int rc = 0;
251 u32 bytes_left;
252 u32 xfer_size;
253 u32 offset;
254 struct efc_dma dma;
255 int last = 0;
256 struct efct_fw_write_result result;
258 init_completion(&result.done);
260 bytes_left = buf_len;
261 offset = 0;
263 dma.size = FW_WRITE_BUFSIZE;
264 dma.virt = dma_alloc_coherent(&efct->pci->dev,
265 dma.size, &dma.phys, GFP_KERNEL);
266 if (!dma.virt)
267 return -ENOMEM;
269 while (bytes_left > 0) {
270 if (bytes_left > FW_WRITE_BUFSIZE)
271 xfer_size = FW_WRITE_BUFSIZE;
272 else
273 xfer_size = bytes_left;
275 memcpy(dma.virt, buf + offset, xfer_size);
277 if (bytes_left == xfer_size)
278 last = 1;
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) {
284 rc = -ENXIO;
285 break;
288 if (result.actual_xfer == 0 || result.status != 0) {
289 rc = -EFAULT;
290 break;
293 if (last)
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);
301 return rc;
304 static int
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
310 * during attach.
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");
317 return -EIO;
320 efc_log_info(efct, "successfully reset firmware.Now resetting port\n");
322 efct_device_detach(efct);
323 return efct_device_attach(efct);
326 static int
327 efct_request_firmware_update(struct efct *efct)
329 int rc = 0;
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);
337 if (rc) {
338 efc_log_debug(efct, "Firmware file(%s) not found.\n", file_name);
339 return rc;
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))) {
346 efc_log_debug(efct,
347 "Skip update. Firmware is already up to date.\n");
348 goto exit;
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);
355 if (rc) {
356 efc_log_err(efct, "Firmware update failed. rc = %d\n", rc);
357 goto exit;
360 efc_log_info(efct, "Firmware updated successfully\n");
361 switch (fw_change_status) {
362 case 0x00:
363 efc_log_info(efct, "New firmware is active.\n");
364 break;
365 case 0x01:
366 efc_log_info(efct,
367 "System reboot needed to activate the new firmware\n");
368 break;
369 case 0x02:
370 case 0x03:
371 efc_log_info(efct,
372 "firmware reset to activate the new firmware\n");
373 efct_fw_reset(efct);
374 break;
375 default:
376 efc_log_info(efct, "Unexpected value change_status:%d\n",
377 fw_change_status);
378 break;
381 exit:
382 release_firmware(fw);
384 return rc;
387 static void
388 efct_device_free(struct efct *efct)
390 if (efct) {
391 list_del(&efct->list_entry);
392 kfree(efct);
396 static int
397 efct_device_interrupts_required(struct efct *efct)
399 int rc;
401 rc = efct_hw_setup(&efct->hw, efct, efct->pci);
402 if (rc < 0)
403 return rc;
405 return efct->hw.config.n_eq;
408 static irqreturn_t
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);
415 return IRQ_HANDLED;
418 static irqreturn_t
419 efct_intr_msix(int irq, void *handle)
421 return IRQ_WAKE_THREAD;
424 static int
425 efct_setup_msix(struct efct *efct, u32 num_intrs)
427 int rc = 0, i;
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__);
432 return -EIO;
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);
440 if (rc < 0) {
441 dev_err(&efct->pci->dev, "Failed to alloc irq : %d\n", rc);
442 return 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;
450 intr_ctx->index = i;
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);
455 if (rc) {
456 dev_err(&efct->pci->dev,
457 "Failed to register %d vector: %d\n", i, rc);
458 goto out;
462 return rc;
464 out:
465 while (--i >= 0)
466 free_irq(pci_irq_vector(efct->pci, i),
467 &efct->intr_context[i]);
469 pci_free_irq_vectors(efct->pci);
470 return rc;
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 */
479 static int
480 efct_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
482 struct efct *efct = NULL;
483 int rc;
484 u32 i, r;
485 int num_interrupts = 0;
486 int nid;
488 dev_info(&pdev->dev, "%s\n", EFCT_DRIVER_NAME);
490 rc = pci_enable_device_mem(pdev);
491 if (rc)
492 return rc;
494 pci_set_master(pdev);
496 rc = pci_set_mwi(pdev);
497 if (rc) {
498 dev_info(&pdev->dev, "pci_set_mwi returned %d\n", rc);
499 goto mwi_out;
502 rc = pci_request_regions(pdev, EFCT_DRIVER_NAME);
503 if (rc) {
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);
510 if (nid < 0) {
511 dev_err(&pdev->dev, "Warning Numa node ID is %d\n", nid);
512 nid = 0;
515 /* Allocate efct */
516 efct = efct_device_alloc(nid);
517 if (!efct) {
518 dev_err(&pdev->dev, "Failed to allocate efct\n");
519 rc = -ENOMEM;
520 goto alloc_out;
523 efct->pci = pdev;
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));
531 r++;
535 * If the 64-bit attribute is set, both this BAR and the
536 * next form the complete address. Skip processing the
537 * next BAR.
539 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM_64)
540 i++;
543 pci_set_drvdata(pdev, efct);
545 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
546 if (rc) {
547 dev_err(&pdev->dev, "setting DMA_BIT_MASK failed\n");
548 goto dma_mask_out;
551 num_interrupts = efct_device_interrupts_required(efct);
552 if (num_interrupts < 0) {
553 efc_log_err(efct, "efct_device_interrupts_required failed\n");
554 rc = -1;
555 goto dma_mask_out;
559 * Initialize MSIX interrupts, note,
560 * efct_setup_msix() enables the interrupt
562 rc = efct_setup_msix(efct, num_interrupts);
563 if (rc) {
564 dev_err(&pdev->dev, "Can't setup msix\n");
565 goto dma_mask_out;
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);
574 if (rc)
575 goto attach_out;
577 return 0;
579 attach_out:
580 efct_teardown_msix(efct);
581 dma_mask_out:
582 pci_set_drvdata(pdev, NULL);
584 for (i = 0; i < EFCT_PCI_MAX_REGS; i++) {
585 if (efct->reg[i])
586 iounmap(efct->reg[i]);
588 efct_device_free(efct);
589 alloc_out:
590 pci_release_regions(pdev);
591 req_regions_out:
592 pci_clear_mwi(pdev);
593 mwi_out:
594 pci_disable_device(pdev);
595 return rc;
598 static void
599 efct_pci_remove(struct pci_dev *pdev)
601 struct efct *efct = pci_get_drvdata(pdev);
602 u32 i;
604 if (!efct)
605 return;
607 efct_device_detach(efct);
609 efct_teardown_msix(efct);
611 for (i = 0; i < EFCT_PCI_MAX_REGS; i++) {
612 if (efct->reg[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);
625 static void
626 efct_device_prep_for_reset(struct efct *efct, struct pci_dev *pdev)
628 if (efct) {
629 efc_log_debug(efct,
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);
638 static void
639 efct_device_prep_for_recover(struct efct *efct)
641 if (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.
658 * Return codes
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);
666 pci_ers_result_t rc;
668 switch (state) {
669 case pci_channel_io_normal:
670 efct_device_prep_for_recover(efct);
671 rc = PCI_ERS_RESULT_CAN_RECOVER;
672 break;
673 case pci_channel_io_frozen:
674 efct_device_prep_for_reset(efct, pdev);
675 rc = PCI_ERS_RESULT_NEED_RESET;
676 break;
677 case pci_channel_io_perm_failure:
678 efct_device_detach(efct);
679 rc = PCI_ERS_RESULT_DISCONNECT;
680 break;
681 default:
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;
685 break;
688 return rc;
691 static pci_ers_result_t
692 efct_pci_io_slot_reset(struct pci_dev *pdev)
694 int rc;
695 struct efct *efct = pci_get_drvdata(pdev);
697 rc = pci_enable_device_mem(pdev);
698 if (rc) {
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);
713 if (rc)
714 efc_log_err(efct, "rc %d returned, IRQ allocation failed\n",
715 rc);
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;
725 static void
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,
752 static
753 int __init efct_init(void)
755 int rc;
757 rc = efct_device_init();
758 if (rc) {
759 pr_err("efct_device_init failed rc=%d\n", rc);
760 return rc;
763 rc = pci_register_driver(&efct_pci_driver);
764 if (rc) {
765 pr_err("pci_register_driver failed rc=%d\n", rc);
766 efct_device_shutdown();
769 return rc;
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");