socdiag.c (8077B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * socdiag: low-level diagnostics shell for Orion (SOC) driver. 8 */ 9 10 #include <unistd.h> 11 #include <stdlib.h> 12 13 #include <sal/core/boot.h> 14 #include <sal/appl/sal.h> 15 #include <sal/appl/config.h> 16 #include <sal/appl/pci.h> 17 #include <soc/debug.h> 18 #include <soc/drv.h> 19 #include <shared/shr_bprof.h> 20 21 #include <appl/diag/system.h> 22 23 #include <linux-bde.h> 24 25 #if defined(MEMLOG_SUPPORT) && defined(__GNUC__) 26 #include <sal/core/memlog.h> 27 #include <sys/stat.h> 28 #include <fcntl.h> 29 int memlog_fd = 0; 30 char memlog_buf[MEM_LOG_BUF_SIZE]; 31 sal_mutex_t memlog_lock = NULL; 32 #endif 33 34 #ifdef LINUX_PLI_COMBO_BDE 35 #include <bde/pli/plibde.h> 36 #endif 37 38 ibde_t *bde; 39 40 /* The bus properties are (currently) the only system specific 41 * settings required. 42 * These must be defined beforehand 43 */ 44 45 #ifndef SYS_BE_PIO 46 #error "SYS_BE_PIO must be defined for the target platform" 47 #endif 48 #ifndef SYS_BE_PACKET 49 #error "SYS_BE_PACKET must be defined for the target platform" 50 #endif 51 #ifndef SYS_BE_OTHER 52 #error "SYS_BE_OTHER must be defined for the target platform" 53 #endif 54 55 #if !defined(SYS_BE_PIO) || !defined(SYS_BE_PACKET) || !defined(SYS_BE_OTHER) 56 #error "platform bus properties not defined." 57 #endif 58 59 /* Function defined in linux-user-bde.c */ 60 extern int 61 bde_icid_get(int d, uint8 *data, int len); 62 63 static soc_chip_info_vectors_t chip_info_vect = { 64 bde_icid_get 65 }; 66 67 static void 68 chip_info_vect_config(void) 69 { 70 soc_chip_info_vect_config(&chip_info_vect); 71 } 72 73 #ifdef INCLUDE_KNET 74 75 #include <sys/types.h> 76 #include <sys/stat.h> 77 #include <fcntl.h> 78 79 #include <soc/knet.h> 80 #include <uk-proxy-kcom.h> 81 #include <bcm-knet-kcom.h> 82 83 /* Function defined in linux-user-bde.c */ 84 extern int 85 bde_irq_mask_set(int unit, uint32 addr, uint32 mask); 86 extern int 87 bde_hw_unit_get(int unit, int inverse); 88 89 static soc_knet_vectors_t knet_vect_uk_proxy = { 90 { 91 uk_proxy_kcom_open, 92 uk_proxy_kcom_close, 93 uk_proxy_kcom_msg_send, 94 uk_proxy_kcom_msg_recv 95 }, 96 bde_irq_mask_set, 97 bde_hw_unit_get 98 }; 99 100 static soc_knet_vectors_t knet_vect_bcm_knet = { 101 { 102 bcm_knet_kcom_open, 103 bcm_knet_kcom_close, 104 bcm_knet_kcom_msg_send, 105 bcm_knet_kcom_msg_recv 106 }, 107 bde_irq_mask_set, 108 bde_hw_unit_get 109 }; 110 111 static void 112 knet_kcom_config(void) 113 { 114 soc_knet_vectors_t *knet_vect; 115 char *kcom_name; 116 int procfd; 117 char procbuf[128]; 118 119 /* Direct IOCTL by default */ 120 knet_vect = &knet_vect_bcm_knet; 121 kcom_name = "bcm-knet"; 122 123 if ((procfd = open("/proc/linux-uk-proxy", O_RDONLY)) >= 0) { 124 if ((read(procfd, procbuf, sizeof(procbuf))) > 0 && 125 strstr(procbuf, "KCOM_KNET : ACTIVE") != NULL) { 126 /* Proxy loaded and active */ 127 knet_vect = &knet_vect_uk_proxy; 128 kcom_name = "uk-proxy"; 129 } 130 close(procfd); 131 } 132 133 soc_knet_config(knet_vect); 134 var_set("kcom", kcom_name, 0, 0); 135 } 136 137 #endif /* INCLUDE_KNET */ 138 139 140 #ifdef BCM_INSTANCE_SUPPORT 141 static int 142 _instance_config(const char *inst) 143 { 144 const char *ptr; 145 #ifndef NO_SAL_APPL 146 char *estr; 147 #endif 148 unsigned int dev_mask, dma_size; 149 150 if (inst == NULL) { 151 #ifndef NO_SAL_APPL 152 estr = "./bcm.user -i <dev_mask>[:dma_size_in_mb] \n"; 153 sal_console_write(estr, sal_strlen(estr) + 1); 154 #endif 155 return -1; 156 } 157 dev_mask = strtol(inst, NULL, 0); 158 if ((ptr = strchr(inst,':')) == NULL) { 159 dma_size = 4; 160 } else { 161 ptr++; 162 dma_size = strtol(ptr, NULL, 0); 163 } 164 165 if (dma_size < 4) { 166 #ifndef NO_SAL_APPL 167 estr = "dmasize must be > 4M and a power of 2 (4M, 8M etc.)\n"; 168 sal_console_write(estr, sal_strlen(estr) + 1); 169 #endif 170 return -1; 171 } else { 172 if ( (dma_size >> 2) & ((dma_size >> 2 )-1)) { 173 #ifndef NO_SAL_APPL 174 estr = "dmasize must be a power of 2 (4M, 8M etc.)\n"; 175 sal_console_write(estr, sal_strlen(estr) + 1); 176 #endif 177 return -1; 178 } 179 } 180 181 return linux_bde_instance_attach(dev_mask, dma_size); 182 } 183 #endif 184 185 #ifdef LINUX_PLI_COMBO_BDE 186 static int sim_path = 0; 187 188 int 189 bcm_sim_path_get(void) 190 { 191 return sim_path; 192 } 193 194 int intr_int_context(void) 195 { 196 extern int pli_intr_int_context() __attribute__((weak)); 197 extern int linux_intr_int_context() __attribute__((weak)); 198 199 if (sim_path) { 200 return pli_intr_int_context(); 201 } 202 return linux_intr_int_context(); 203 } 204 #endif 205 206 int 207 bde_create(void) 208 { 209 linux_bde_bus_t bus; 210 bus.be_pio = SYS_BE_PIO; 211 bus.be_packet = SYS_BE_PACKET; 212 bus.be_other = SYS_BE_OTHER; 213 214 #ifdef LINUX_PLI_COMBO_BDE 215 sim_path = sal_ctoi(getenv("BCM_SIM_PATH"), 0); 216 if (sim_path) { 217 return plibde_create(&bde); 218 } 219 #endif 220 221 return linux_bde_create(&bus, &bde); 222 223 } 224 225 /* 226 * Main loop. 227 */ 228 int main(int argc, char *argv[]) 229 { 230 int i, len; 231 char *envstr; 232 char *config_file, *config_temp; 233 #ifdef BCM_INSTANCE_SUPPORT 234 const char *inst = NULL; 235 #endif 236 237 #ifdef BCM_BPROF_STATS 238 shr_bprof_stats_time_init(); 239 #endif 240 241 if ((envstr = getenv("BCM_CONFIG_FILE")) != NULL) { 242 config_file = envstr; 243 len = sal_strlen(config_file); 244 if ((config_temp = sal_alloc(len+5, NULL)) != NULL) { 245 sal_strcpy(config_temp, config_file); 246 sal_strcpy(&config_temp[len], ".tmp"); 247 #ifndef NO_SAL_APPL 248 sal_config_file_set(config_file, config_temp); 249 #endif 250 sal_free(config_temp); 251 } 252 } 253 254 #ifdef BCM_INSTANCE_SUPPORT 255 for (i = 1; i < argc; i++) { 256 if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "--instance")) { 257 inst = argv[i+1]; 258 /* 259 * specify the dev_mask and its dma_size (optional,default:4MB) 260 * bcm.user -i 0x1[:8] 261 */ 262 if (_instance_config(inst) < 0){ 263 #ifndef NO_SAL_APPL 264 char *estr = "config error!\n"; 265 sal_console_write(estr, sal_strlen(estr) + 1); 266 #endif 267 exit(1); 268 } 269 } 270 } 271 #endif 272 273 if (sal_core_init() < 0 274 #ifndef NO_SAL_APPL 275 || sal_appl_init() < 0 276 #endif 277 ) { 278 /* 279 * If SAL initialization fails then printf will most 280 * likely assert or fail. Try direct console access 281 * instead to get the error message out. 282 */ 283 #ifndef NO_SAL_APPL 284 char *estr = "SAL Initialization failed\r\n"; 285 sal_console_write(estr, sal_strlen(estr) + 1); 286 #endif 287 exit(1); 288 } 289 290 for (i = 1; i < argc; i++) { 291 if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--reload")) { 292 sal_boot_flags_set(sal_boot_flags_get() | BOOT_F_RELOAD); 293 } 294 } 295 296 #ifdef MEMLOG_SUPPORT 297 for (i = 1; i < argc; i++) { 298 if (!strcmp(argv[i], "-memlog") && argv[i+1] != NULL) { 299 memlog_fd = creat(argv[i+1], S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 300 } 301 } 302 if (memlog_fd > 0) { 303 memlog_lock = sal_mutex_create("memlog lock"); 304 } 305 #endif 306 #ifdef INCLUDE_KNET 307 knet_kcom_config(); 308 #endif 309 chip_info_vect_config(); 310 311 diag_shell(); 312 313 linux_bde_destroy(bde); 314 #ifdef MEMLOG_SUPPORT 315 if (memlog_lock) { 316 sal_mutex_destroy(memlog_lock); 317 } 318 #endif 319 return 0; 320 } 321 322 #ifndef NO_SAL_APPL 323 /* 324 * This function is required for the pci_print_all function defined in 325 * sal/appl/pci_common.c. The pci_print_all function is a legacy 326 * function used only by the CLI 'show pci' command. 327 */ 328 329 #include <sal/appl/pci.h> 330 331 uint32 pci_config_getw(pci_dev_t *dev, uint32 addr) 332 { 333 #ifdef LINUX_PLI_COMBO_BDE 334 extern uint32 pli_pci_config_getw(pci_dev_t *dev, uint32 addr); 335 if (sim_path) { 336 return pli_pci_config_getw(dev, addr); 337 } 338 #endif 339 /* We only support our own devices */ 340 if (dev->busNo != 0) { 341 return 0xffffffff; 342 } 343 344 if (dev->devNo >= bde->num_devices(BDE_SWITCH_DEVICES)) { 345 return 0xffffffff; 346 } 347 348 return bde->pci_conf_read(dev->devNo, addr); 349 } 350 #endif /* NO_SAL_APPL */