1 /* 2 * Copyright (c) 2018, Mellanox Technologies. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 */ 32 33 #include "mlx4.h" 34 35 #define BAD_ACCESS 0xBADACCE5 36 #define HEALTH_BUFFER_SIZE 0x40 37 #define CR_ENABLE_BIT swab32(BIT(6)) 38 #define CR_ENABLE_BIT_OFFSET 0xF3F04 39 #define MAX_NUM_OF_DUMPS_TO_STORE (8) 40 41 static const char *region_cr_space_str = "cr-space"; 42 static const char *region_fw_health_str = "fw-health"; 43 44 /* Set to true in case cr enable bit was set to true before crdump */ 45 static bool crdump_enbale_bit_set; 46 47 static void crdump_enable_crspace_access(struct mlx4_dev *dev, 48 u8 __iomem *cr_space) 49 { 50 /* Get current enable bit value */ 51 crdump_enbale_bit_set = 52 readl(cr_space + CR_ENABLE_BIT_OFFSET) & CR_ENABLE_BIT; 53 54 /* Enable FW CR filter (set bit6 to 0) */ 55 if (crdump_enbale_bit_set) 56 writel(readl(cr_space + CR_ENABLE_BIT_OFFSET) & ~CR_ENABLE_BIT, 57 cr_space + CR_ENABLE_BIT_OFFSET); 58 59 /* Enable block volatile crspace accesses */ 60 writel(swab32(1), cr_space + dev->caps.health_buffer_addrs + 61 HEALTH_BUFFER_SIZE); 62 } 63 64 static void crdump_disable_crspace_access(struct mlx4_dev *dev, 65 u8 __iomem *cr_space) 66 { 67 /* Disable block volatile crspace accesses */ 68 writel(0, cr_space + dev->caps.health_buffer_addrs + 69 HEALTH_BUFFER_SIZE); 70 71 /* Restore FW CR filter value (set bit6 to original value) */ 72 if (crdump_enbale_bit_set) 73 writel(readl(cr_space + CR_ENABLE_BIT_OFFSET) | CR_ENABLE_BIT, 74 cr_space + CR_ENABLE_BIT_OFFSET); 75 } 76 77 static void mlx4_crdump_collect_crspace(struct mlx4_dev *dev, 78 u8 __iomem *cr_space, 79 u32 id) 80 { 81 struct mlx4_fw_crdump *crdump = &dev->persist->crdump; 82 struct pci_dev *pdev = dev->persist->pdev; 83 unsigned long cr_res_size; 84 u8 *crspace_data; 85 int offset; 86 int err; 87 88 if (!crdump->region_crspace) { 89 mlx4_err(dev, "crdump: cr-space region is NULL\n"); 90 return; 91 } 92 93 /* Try to collect CR space */ 94 cr_res_size = pci_resource_len(pdev, 0); 95 crspace_data = kvmalloc(cr_res_size, GFP_KERNEL); 96 if (crspace_data) { 97 for (offset = 0; offset < cr_res_size; offset += 4) 98 *(u32 *)(crspace_data + offset) = 99 readl(cr_space + offset); 100 101 err = devlink_region_snapshot_create(crdump->region_crspace, 102 cr_res_size, crspace_data, 103 id, &kvfree); 104 if (err) { 105 kvfree(crspace_data); 106 mlx4_warn(dev, "crdump: devlink create %s snapshot id %d err %d\n", 107 region_cr_space_str, id, err); 108 } else { 109 mlx4_info(dev, "crdump: added snapshot %d to devlink region %s\n", 110 id, region_cr_space_str); 111 } 112 } else { 113 mlx4_err(dev, "crdump: Failed to allocate crspace buffer\n"); 114 } 115 } 116 117 static void mlx4_crdump_collect_fw_health(struct mlx4_dev *dev, 118 u8 __iomem *cr_space, 119 u32 id) 120 { 121 struct mlx4_fw_crdump *crdump = &dev->persist->crdump; 122 u8 *health_data; 123 int offset; 124 int err; 125 126 if (!crdump->region_fw_health) { 127 mlx4_err(dev, "crdump: fw-health region is NULL\n"); 128 return; 129 } 130 131 /* Try to collect health buffer */ 132 health_data = kvmalloc(HEALTH_BUFFER_SIZE, GFP_KERNEL); 133 if (health_data) { 134 u8 __iomem *health_buf_start = 135 cr_space + dev->caps.health_buffer_addrs; 136 137 for (offset = 0; offset < HEALTH_BUFFER_SIZE; offset += 4) 138 *(u32 *)(health_data + offset) = 139 readl(health_buf_start + offset); 140 141 err = devlink_region_snapshot_create(crdump->region_fw_health, 142 HEALTH_BUFFER_SIZE, 143 health_data, 144 id, &kvfree); 145 if (err) { 146 kvfree(health_data); 147 mlx4_warn(dev, "crdump: devlink create %s snapshot id %d err %d\n", 148 region_fw_health_str, id, err); 149 } else { 150 mlx4_info(dev, "crdump: added snapshot %d to devlink region %s\n", 151 id, region_fw_health_str); 152 } 153 } else { 154 mlx4_err(dev, "crdump: Failed to allocate health buffer\n"); 155 } 156 } 157 158 int mlx4_crdump_collect(struct mlx4_dev *dev) 159 { 160 struct devlink *devlink = priv_to_devlink(mlx4_priv(dev)); 161 struct mlx4_fw_crdump *crdump = &dev->persist->crdump; 162 struct pci_dev *pdev = dev->persist->pdev; 163 unsigned long cr_res_size; 164 u8 __iomem *cr_space; 165 u32 id; 166 167 if (!dev->caps.health_buffer_addrs) { 168 mlx4_info(dev, "crdump: FW doesn't support health buffer access, skipping\n"); 169 return 0; 170 } 171 172 if (!crdump->snapshot_enable) { 173 mlx4_info(dev, "crdump: devlink snapshot disabled, skipping\n"); 174 return 0; 175 } 176 177 cr_res_size = pci_resource_len(pdev, 0); 178 179 cr_space = ioremap(pci_resource_start(pdev, 0), cr_res_size); 180 if (!cr_space) { 181 mlx4_err(dev, "crdump: Failed to map pci cr region\n"); 182 return -ENODEV; 183 } 184 185 crdump_enable_crspace_access(dev, cr_space); 186 187 /* Get the available snapshot ID for the dumps */ 188 id = devlink_region_shapshot_id_get(devlink); 189 190 /* Try to capture dumps */ 191 mlx4_crdump_collect_crspace(dev, cr_space, id); 192 mlx4_crdump_collect_fw_health(dev, cr_space, id); 193 194 crdump_disable_crspace_access(dev, cr_space); 195 196 iounmap(cr_space); 197 return 0; 198 } 199 200 int mlx4_crdump_init(struct mlx4_dev *dev) 201 { 202 struct devlink *devlink = priv_to_devlink(mlx4_priv(dev)); 203 struct mlx4_fw_crdump *crdump = &dev->persist->crdump; 204 struct pci_dev *pdev = dev->persist->pdev; 205 206 crdump->snapshot_enable = false; 207 208 /* Create cr-space region */ 209 crdump->region_crspace = 210 devlink_region_create(devlink, 211 region_cr_space_str, 212 MAX_NUM_OF_DUMPS_TO_STORE, 213 pci_resource_len(pdev, 0)); 214 if (IS_ERR(crdump->region_crspace)) 215 mlx4_warn(dev, "crdump: create devlink region %s err %ld\n", 216 region_cr_space_str, 217 PTR_ERR(crdump->region_crspace)); 218 219 /* Create fw-health region */ 220 crdump->region_fw_health = 221 devlink_region_create(devlink, 222 region_fw_health_str, 223 MAX_NUM_OF_DUMPS_TO_STORE, 224 HEALTH_BUFFER_SIZE); 225 if (IS_ERR(crdump->region_fw_health)) 226 mlx4_warn(dev, "crdump: create devlink region %s err %ld\n", 227 region_fw_health_str, 228 PTR_ERR(crdump->region_fw_health)); 229 230 return 0; 231 } 232 233 void mlx4_crdump_end(struct mlx4_dev *dev) 234 { 235 struct mlx4_fw_crdump *crdump = &dev->persist->crdump; 236 237 devlink_region_destroy(crdump->region_fw_health); 238 devlink_region_destroy(crdump->region_crspace); 239 } 240