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