1 /* SPDX-License-Identifier: BSD-3-Clause */ 2 /* Copyright(c) 2007-2025 Intel Corporation */ 3 #include "qat_freebsd.h" 4 #include <adf_accel_devices.h> 5 #include <adf_common_drv.h> 6 #include <adf_cfg.h> 7 #include "adf_4xxxvf_hw_data.h" 8 #include "adf_gen4_hw_data.h" 9 #include "adf_fw_counters.h" 10 #include "adf_cfg_device.h" 11 #include "adf_dbgfs.h" 12 #include <sys/types.h> 13 #include <sys/kernel.h> 14 #include <sys/malloc.h> 15 #include <machine/bus_dma.h> 16 #include <dev/pci/pcireg.h> 17 18 static MALLOC_DEFINE(M_QAT_4XXXVF, "qat_4xxxvf", "qat_4xxxvf"); 19 20 #define ADF_SYSTEM_DEVICE(device_id) \ 21 { \ 22 PCI_VENDOR_ID_INTEL, device_id \ 23 } 24 25 static const struct pci_device_id adf_pci_tbl[] = 26 { ADF_SYSTEM_DEVICE(ADF_4XXXIOV_PCI_DEVICE_ID), 27 ADF_SYSTEM_DEVICE(ADF_401XXIOV_PCI_DEVICE_ID), 28 { 29 0, 30 } }; 31 32 static int 33 adf_probe(device_t dev) 34 { 35 const struct pci_device_id *id; 36 37 for (id = adf_pci_tbl; id->vendor != 0; id++) { 38 if (pci_get_vendor(dev) == id->vendor && 39 pci_get_device(dev) == id->device) { 40 device_set_desc(dev, 41 "Intel " ADF_4XXXVF_DEVICE_NAME 42 " QuickAssist"); 43 return BUS_PROBE_GENERIC; 44 } 45 } 46 return ENXIO; 47 } 48 49 static void 50 adf_cleanup_accel(struct adf_accel_dev *accel_dev) 51 { 52 struct adf_accel_pci *accel_pci_dev = &accel_dev->accel_pci_dev; 53 struct adf_accel_dev *pf; 54 int i; 55 56 if (accel_dev->dma_tag) 57 bus_dma_tag_destroy(accel_dev->dma_tag); 58 59 for (i = 0; i < ADF_PCI_MAX_BARS; i++) { 60 struct adf_bar *bar = &accel_pci_dev->pci_bars[i]; 61 62 if (bar->virt_addr) 63 bus_free_resource(accel_pci_dev->pci_dev, 64 SYS_RES_MEMORY, 65 bar->virt_addr); 66 } 67 68 /* 69 * As adf_clean_hw_data_4xxxiov() will update class index, before 70 * index is updated, vf must be remove from accel_table. 71 */ 72 pf = adf_devmgr_pci_to_accel_dev(pci_find_pf(accel_pci_dev->pci_dev)); 73 adf_devmgr_rm_dev(accel_dev, pf); 74 75 if (accel_dev->hw_device) { 76 switch (pci_get_device(accel_pci_dev->pci_dev)) { 77 case ADF_4XXXIOV_PCI_DEVICE_ID: 78 case ADF_401XXIOV_PCI_DEVICE_ID: 79 adf_clean_hw_data_4xxxiov(accel_dev->hw_device); 80 break; 81 default: 82 break; 83 } 84 free(accel_dev->hw_device, M_QAT_4XXXVF); 85 accel_dev->hw_device = NULL; 86 } 87 adf_dbgfs_exit(accel_dev); 88 adf_cfg_dev_remove(accel_dev); 89 } 90 91 static int 92 adf_attach(device_t dev) 93 { 94 struct adf_accel_dev *accel_dev; 95 struct adf_accel_dev *pf; 96 struct adf_accel_pci *accel_pci_dev; 97 struct adf_hw_device_data *hw_data; 98 unsigned int bar_nr; 99 int ret = 0; 100 int rid; 101 struct adf_cfg_device *cfg_dev = NULL; 102 103 accel_dev = device_get_softc(dev); 104 mutex_init(&accel_dev->lock); 105 accel_dev->is_vf = true; 106 pf = adf_devmgr_pci_to_accel_dev(pci_find_pf(dev)); 107 108 INIT_LIST_HEAD(&accel_dev->crypto_list); 109 accel_pci_dev = &accel_dev->accel_pci_dev; 110 accel_pci_dev->pci_dev = dev; 111 112 if (bus_get_domain(dev, &accel_pci_dev->node) != 0) 113 accel_pci_dev->node = 0; 114 115 /* Add accel device to accel table */ 116 ret = adf_devmgr_add_dev(accel_dev, pf); 117 if (ret) { 118 device_printf(GET_DEV(accel_dev), 119 "Failed to add new accelerator device.\n"); 120 goto out_err_lock; 121 } 122 123 /* Allocate and configure device configuration structure */ 124 hw_data = malloc(sizeof(*hw_data), M_QAT_4XXXVF, M_WAITOK | M_ZERO); 125 accel_dev->hw_device = hw_data; 126 adf_init_hw_data_4xxxiov(accel_dev->hw_device); 127 accel_pci_dev->revid = pci_get_revid(dev); 128 129 hw_data->fuses = pci_read_config(dev, ADF_4XXXIOV_VFFUSECTL4_OFFSET, 4); 130 131 /* Get Accelerators and Accelerators Engines masks */ 132 hw_data->accel_mask = hw_data->get_accel_mask(accel_dev); 133 hw_data->ae_mask = hw_data->get_ae_mask(accel_dev); 134 hw_data->admin_ae_mask = hw_data->ae_mask; 135 accel_pci_dev->sku = hw_data->get_sku(hw_data); 136 137 /* Create device configuration table */ 138 ret = adf_cfg_dev_add(accel_dev); 139 if (ret) 140 goto out_err; 141 142 pci_set_max_read_req(dev, 1024); 143 144 ret = bus_dma_tag_create(bus_get_dma_tag(dev), 145 1, 146 0, 147 BUS_SPACE_MAXADDR, 148 BUS_SPACE_MAXADDR, 149 NULL, 150 NULL, 151 BUS_SPACE_MAXSIZE, 152 /* BUS_SPACE_UNRESTRICTED */ 1, 153 BUS_SPACE_MAXSIZE, 154 0, 155 NULL, 156 NULL, 157 &accel_dev->dma_tag); 158 if (ret) 159 goto out_err; 160 161 hw_data->accel_capabilities_mask = adf_4xxxvf_get_hw_cap(accel_dev); 162 163 /* Find and map all the device's BARS */ 164 /* Logical BARs configuration for 64bit BARs: 165 bar 0 and 1 - logical BAR0 166 bar 2 and 3 - logical BAR1 167 bar 4 and 5 - logical BAR3 168 */ 169 for (bar_nr = 0; 170 bar_nr < (ADF_PCI_MAX_BARS * 2) && bar_nr < PCIR_MAX_BAR_0; 171 bar_nr += 2) { 172 struct adf_bar *bar; 173 174 rid = PCIR_BAR(bar_nr); 175 bar = &accel_pci_dev->pci_bars[bar_nr / 2]; 176 bar->virt_addr = bus_alloc_resource_any(dev, 177 SYS_RES_MEMORY, 178 &rid, 179 RF_ACTIVE); 180 if (!bar->virt_addr) { 181 device_printf(dev, "Failed to map BAR %d\n", bar_nr); 182 ret = ENXIO; 183 goto out_err; 184 } 185 bar->base_addr = rman_get_start(bar->virt_addr); 186 bar->size = rman_get_size(bar->virt_addr); 187 } 188 ret = pci_enable_busmaster(dev); 189 if (ret) 190 goto out_err; 191 192 adf_dbgfs_init(accel_dev); 193 194 /* Completion for VF2PF request/response message exchange */ 195 init_completion(&accel_dev->u1.vf.msg_received); 196 mutex_init(&accel_dev->u1.vf.rpreset_lock); 197 198 ret = hw_data->config_device(accel_dev); 199 if (ret) 200 goto out_err_disable; 201 202 ret = adf_dev_init(accel_dev); 203 if (!ret) 204 ret = adf_dev_start(accel_dev); 205 206 if (ret) { 207 device_printf( 208 GET_DEV(accel_dev), 209 "Failed to start - make sure PF enabled services match VF configuration.\n"); 210 adf_dev_stop(accel_dev); 211 adf_dev_shutdown(accel_dev); 212 return 0; 213 } 214 215 cfg_dev = accel_dev->cfg->dev; 216 adf_cfg_device_clear(cfg_dev, accel_dev); 217 free(cfg_dev, M_QAT); 218 accel_dev->cfg->dev = NULL; 219 220 return ret; 221 222 out_err_disable: 223 pci_disable_busmaster(dev); 224 out_err: 225 adf_cleanup_accel(accel_dev); 226 out_err_lock: 227 mutex_destroy(&accel_dev->lock); 228 229 return ret; 230 } 231 232 static int 233 adf_detach(device_t dev) 234 { 235 struct adf_accel_dev *accel_dev = device_get_softc(dev); 236 237 if (!accel_dev) { 238 printf("QAT: Driver removal failed\n"); 239 return EFAULT; 240 } 241 242 adf_flush_vf_wq(accel_dev); 243 clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status); 244 adf_dev_stop(accel_dev); 245 adf_dev_shutdown(accel_dev); 246 pci_disable_busmaster(dev); 247 adf_cleanup_accel(accel_dev); 248 mutex_destroy(&accel_dev->lock); 249 return 0; 250 } 251 252 static int 253 adf_modevent(module_t mod, int type, void *data) 254 { 255 256 switch (type) { 257 case MOD_UNLOAD: 258 adf_clean_vf_map(true); 259 return 0; 260 default: 261 return EOPNOTSUPP; 262 } 263 } 264 265 static device_method_t adf_methods[] = { DEVMETHOD(device_probe, adf_probe), 266 DEVMETHOD(device_attach, adf_attach), 267 DEVMETHOD(device_detach, adf_detach), 268 269 DEVMETHOD_END }; 270 271 static driver_t adf_driver = { "qat", 272 adf_methods, 273 sizeof(struct adf_accel_dev) }; 274 275 DRIVER_MODULE_ORDERED(qat_4xxxvf, 276 pci, 277 adf_driver, 278 adf_modevent, 279 NULL, 280 SI_ORDER_THIRD); 281 MODULE_VERSION(qat_4xxxvf, 1); 282 MODULE_DEPEND(qat_4xxxvf, qat_common, 1, 1, 1); 283 MODULE_DEPEND(qat_4xxxvf, qat_api, 1, 1, 1); 284 MODULE_DEPEND(qat_4xxxvf, linuxkpi, 1, 1, 1); 285