Print this page
5218 posix definition of NULL
correct unistd.h and iso/stddef_iso.h
update gate source affected
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/cfgadm_plugins/shp/common/shp.c
+++ new/usr/src/lib/cfgadm_plugins/shp/common/shp.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /*
27 27 * Plugin library for PCI Express and PCI (SHPC) hotplug controller
28 28 */
29 29
30 30 #include <stddef.h>
31 31 #include <locale.h>
32 32 #include <ctype.h>
33 33 #include <stdio.h>
34 34 #include <stdlib.h>
35 35 #include <string.h>
36 36 #include <fcntl.h>
37 37 #include <unistd.h>
38 38 #include <errno.h>
39 39 #include <locale.h>
40 40 #include <langinfo.h>
41 41 #include <time.h>
42 42 #include <sys/param.h>
43 43 #include <stdarg.h>
44 44 #include <libdevinfo.h>
45 45 #include <libdevice.h>
46 46
47 47 #define CFGA_PLUGIN_LIB
48 48
49 49 #include <config_admin.h>
50 50
51 51 #include <assert.h>
52 52 #include <sys/types.h>
53 53 #include <sys/stat.h>
54 54 #include <sys/dditypes.h>
55 55 #include <sys/pci.h>
56 56 #include <libintl.h>
57 57
58 58 #include <dirent.h>
59 59 #include <limits.h>
60 60 #include <sys/mkdev.h>
61 61 #include "../../../../uts/common/sys/hotplug/pci/pcie_hp.h"
62 62 #include "../../../../common/pci/pci_strings.h"
63 63 #include <libhotplug.h>
64 64
65 65 extern const struct pci_class_strings_s class_pci[];
66 66 extern int class_pci_items;
67 67
68 68 #define MSG_HOTPLUG_DISABLED \
69 69 "Error: hotplug service is probably not running, " \
70 70 "please use 'svcadm enable hotplug' to enable the service. " \
71 71 "See cfgadm_shp(1M) for more details."
72 72
73 73 #define DEVICES_DIR "/devices"
74 74 #define SLASH "/"
75 75 #define GET_DYN(a) (strstr((a), CFGA_DYN_SEP))
76 76
77 77 /*
78 78 * Set the version number
79 79 */
80 80 int cfga_version = CFGA_HSL_V2;
81 81
82 82 #ifdef DEBUG
83 83 #define SHP_DBG 1
84 84 #endif
85 85
86 86 #if !defined(TEXT_DOMAIN)
87 87 #define TEXT_DOMAIN "SYS_TEST"
88 88 #endif
89 89
90 90 /*
91 91 * DEBUGING LEVEL
92 92 *
93 93 * External routines: 1 - 2
94 94 * Internal routines: 3 - 4
95 95 */
96 96 #ifdef SHP_DBG
97 97 int shp_debug = 1;
98 98 #define DBG(level, args) \
99 99 { if (shp_debug >= (level)) printf args; }
100 100 #define DBG_F(level, args) \
101 101 { if (shp_debug >= (level)) fprintf args; }
102 102 #else
103 103 #define DBG(level, args) /* nothing */
104 104 #define DBG_F(level, args) /* nothing */
105 105 #endif
106 106
107 107 #define CMD_ACQUIRE 0
108 108 #define CMD_GETSTAT 1
109 109 #define CMD_LIST 2
110 110 #define CMD_SLOT_CONNECT 3
111 111 #define CMD_SLOT_DISCONNECT 4
112 112 #define CMD_SLOT_CONFIGURE 5
113 113 #define CMD_SLOT_UNCONFIGURE 6
114 114 #define CMD_SLOT_INSERT 7
115 115 #define CMD_SLOT_REMOVE 8
116 116 #define CMD_OPEN 9
117 117 #define CMD_FSTAT 10
118 118 #define ERR_CMD_INVAL 11
119 119 #define ERR_AP_INVAL 12
120 120 #define ERR_AP_ERR 13
121 121 #define ERR_OPT_INVAL 14
122 122
123 123 static char *
124 124 cfga_errstrs[] = {
125 125 /* n */ "acquire ",
126 126 /* n */ "get-status ",
127 127 /* n */ "list ",
128 128 /* n */ "connect ",
129 129 /* n */ "disconnect ",
130 130 /* n */ "configure ",
131 131 /* n */ "unconfigure ",
132 132 /* n */ "insert ",
133 133 /* n */ "remove ",
134 134 /* n */ "open ",
135 135 /* n */ "fstat ",
136 136 /* y */ "invalid command ",
137 137 /* y */ "invalid attachment point ",
138 138 /* y */ "invalid transition ",
139 139 /* y */ "invalid option ",
140 140 NULL
141 141 };
142 142
143 143 #define HELP_HEADER 1
144 144 #define HELP_CONFIG 2
145 145 #define HELP_ENABLE_SLOT 3
146 146 #define HELP_DISABLE_SLOT 4
147 147 #define HELP_ENABLE_AUTOCONF 5
148 148 #define HELP_DISABLE_AUTOCONF 6
149 149 #define HELP_LED_CNTRL 7
150 150 #define HELP_UNKNOWN 8
151 151 #define SUCCESS 9
152 152 #define FAILED 10
153 153 #define UNKNOWN 11
154 154
155 155 #define MAXLINE 256
156 156
157 157 extern int errno;
158 158
159 159 static void cfga_err(char **errstring, ...);
160 160 static cfga_err_t fix_ap_name(char *ap_log_id, const char *ap_id,
161 161 char *slot_name, char **errstring);
162 162 static cfga_err_t check_options(const char *options);
163 163 static void cfga_msg(struct cfga_msg *msgp, const char *str);
164 164 static char *findlink(char *ap_phys_id);
165 165
166 166 static char *
167 167 cfga_strs[] = {
168 168 NULL,
169 169 "\nPCI hotplug specific commands:",
170 170 "\t-c [connect|disconnect|configure|unconfigure|insert|remove] "
171 171 "ap_id [ap_id...]",
172 172 "\t-x enable_slot ap_id [ap_id...]",
173 173 "\t-x disable_slot ap_id [ap_id...]",
174 174 "\t-x enable_autoconfig ap_id [ap_id...]",
175 175 "\t-x disable_autoconfig ap_id [ap_id...]",
176 176 "\t-x led[=[fault|power|active|attn],mode=[on|off|blink]] ap_id [ap_id...]",
177 177 "\tunknown command or option: ",
178 178 "success ",
179 179 "failed ",
180 180 "unknown",
181 181 NULL
182 182 };
183 183
184 184 #define MAX_FORMAT 80
185 185
186 186 #define ENABLE_SLOT 0
187 187 #define DISABLE_SLOT 1
188 188 #define ENABLE_AUTOCNF 2
189 189 #define DISABLE_AUTOCNF 3
190 190 #define LED 4
191 191 #define MODE 5
192 192
193 193 typedef enum { PCIEHPC_FAULT_LED, PCIEHPC_POWER_LED, PCIEHPC_ATTN_LED,
194 194 PCIEHPC_ACTIVE_LED} pciehpc_led_t;
195 195
196 196 typedef enum { PCIEHPC_BOARD_UNKNOWN, PCIEHPC_BOARD_PCI_HOTPLUG }
197 197 pciehpc_board_type_t;
198 198
199 199 /*
200 200 * Board Type
201 201 */
202 202 static char *
203 203 board_strs[] = {
204 204 /* n */ "???", /* PCIEHPC_BOARD_UNKNOWN */
205 205 /* n */ "hp", /* PCIEHPC_BOARD_PCI_HOTPLUG */
206 206 /* n */ NULL
207 207 };
208 208
209 209 /*
210 210 * HW functions
211 211 */
212 212 static char *
213 213 func_strs[] = {
214 214 /* n */ "enable_slot",
215 215 /* n */ "disable_slot",
216 216 /* n */ "enable_autoconfig",
217 217 /* n */ "disable_autoconfig",
218 218 /* n */ "led",
219 219 /* n */ "mode",
220 220 /* n */ NULL
221 221 };
222 222
223 223 /*
224 224 * LED strings
225 225 */
226 226 static char *
227 227 led_strs[] = {
228 228 /* n */ "fault", /* PCIEHPC_FAULT_LED */
229 229 /* n */ "power", /* PCIEHPC_POWER_LED */
230 230 /* n */ "attn", /* PCIEHPC_ATTN_LED */
231 231 /* n */ "active", /* PCIEHPC_ACTIVE_LED */
232 232 /* n */ NULL
233 233 };
234 234
235 235 static char *
236 236 led_strs2[] = {
237 237 /* n */ PCIEHPC_PROP_LED_FAULT, /* PCIEHPC_FAULT_LED */
238 238 /* n */ PCIEHPC_PROP_LED_POWER, /* PCIEHPC_POWER_LED */
239 239 /* n */ PCIEHPC_PROP_LED_ATTN, /* PCIEHPC_ATTN_LED */
240 240 /* n */ PCIEHPC_PROP_LED_ACTIVE, /* PCIEHPC_ACTIVE_LED */
241 241 /* n */ NULL
242 242 };
243 243
244 244 #define FAULT 0
245 245 #define POWER 1
246 246 #define ATTN 2
247 247 #define ACTIVE 3
248 248
249 249 static char *
250 250 mode_strs[] = {
251 251 /* n */ "off", /* OFF */
252 252 /* n */ "on", /* ON */
253 253 /* n */ "blink", /* BLINK */
254 254 /* n */ NULL
255 255 };
256 256
257 257 #define OFF 0
258 258 #define ON 1
259 259 #define BLINK 2
260 260
261 261 #define cfga_errstrs(i) cfga_errstrs[(i)]
262 262
263 263 #define cfga_eid(a, b) (((a) << 8) + (b))
264 264 #define MAXDEVS 32
265 265
266 266 typedef enum {
267 267 SOLARIS_SLT_NAME,
268 268 PROM_SLT_NAME
269 269 } slt_name_src_t;
270 270
271 271 struct searcharg {
272 272 char *devpath;
273 273 char slotnames[MAXDEVS][MAXNAMELEN];
274 274 int minor;
275 275 di_prom_handle_t promp;
276 276 slt_name_src_t slt_name_src;
277 277 };
278 278
279 279 static void *private_check;
280 280
281 281 /*
282 282 * Return the corresponding hp node for a given ap_id, it is the caller's
283 283 * responsibility to call hp_fini() to free the snapshot.
284 284 */
285 285 static cfga_err_t
286 286 physpath2node(const char *physpath, char **errstring, hp_node_t *nodep)
287 287 {
288 288 char *rpath;
289 289 char *cp;
290 290 hp_node_t node;
291 291 size_t len;
292 292 char *errmsg;
293 293
294 294 if (getuid() != 0 && geteuid() != 0)
295 295 return (CFGA_ERROR);
296 296
297 297 if ((rpath = malloc(strlen(physpath) + 1)) == NULL)
298 298 return (CFGA_ERROR);
299 299
300 300 (void) strcpy(rpath, physpath);
301 301
302 302 /* Remove devices prefix (if any) */
303 303 len = strlen(DEVICES_DIR);
304 304 if (strncmp(rpath, DEVICES_DIR SLASH, len + strlen(SLASH)) == 0) {
305 305 (void) memmove(rpath, rpath + len,
306 306 strlen(rpath + len) + 1);
307 307 }
308 308
309 309 /* Remove dynamic component if any */
310 310 if ((cp = GET_DYN(rpath)) != NULL) {
311 311 *cp = '\0';
312 312 }
313 313
314 314 /* Remove minor name (if any) */
315 315 if ((cp = strrchr(rpath, ':')) == NULL) {
316 316 free(rpath);
317 317 return (CFGA_INVAL);
318 318 }
319 319
320 320 *cp = '\0';
321 321 cp++;
322 322
323 323 DBG(1, ("rpath=%s,cp=%s\n", rpath, cp));
324 324 if ((node = hp_init(rpath, cp, 0)) == NULL) {
325 325 if (errno == EBADF) {
326 326 /* No reponse to operations on the door file. */
327 327 assert(errstring != NULL);
328 328 *errstring = strdup(MSG_HOTPLUG_DISABLED);
329 329 free(rpath);
330 330 return (CFGA_NOTSUPP);
331 331 }
332 332 free(rpath);
333 333 return (CFGA_ERROR);
334 334 }
335 335
336 336 free(rpath);
337 337
338 338 *nodep = node;
339 339 return (CFGA_OK);
340 340 }
341 341
342 342 typedef struct error_size_cb_arg {
343 343 size_t rsrc_width;
344 344 size_t info_width;
345 345 int cnt;
346 346 } error_size_cb_arg_t;
347 347
348 348 /*
349 349 * Callback function for hp_traverse(), to sum up the
350 350 * maximum length for error message display.
351 351 */
352 352 static int
353 353 error_sizeup_cb(hp_node_t node, void *arg)
354 354 {
355 355 error_size_cb_arg_t *sizearg = (error_size_cb_arg_t *)arg;
356 356 size_t len;
357 357
358 358 /* Only process USAGE nodes */
359 359 if (hp_type(node) != HP_NODE_USAGE)
360 360 return (HP_WALK_CONTINUE);
361 361
362 362 sizearg->cnt++;
363 363
364 364 /* size up resource name */
365 365 len = strlen(hp_name(node));
366 366 if (sizearg->rsrc_width < len)
367 367 sizearg->rsrc_width = len;
368 368
369 369 /* size up usage description */
370 370 len = strlen(hp_usage(node));
371 371 if (sizearg->info_width < len)
372 372 sizearg->info_width = len;
373 373
374 374 return (HP_WALK_CONTINUE);
375 375 }
376 376
377 377 typedef struct error_sum_cb_arg {
378 378 char **table;
379 379 char *format;
380 380 } error_sum_cb_arg_t;
381 381
382 382 /*
383 383 * Callback function for hp_traverse(), to add the error
384 384 * message to he table.
385 385 */
386 386 static int
387 387 error_sumup_cb(hp_node_t node, void *arg)
388 388 {
389 389 error_sum_cb_arg_t *sumarg = (error_sum_cb_arg_t *)arg;
390 390 char **table = sumarg->table;
391 391 char *format = sumarg->format;
392 392
393 393 /* Only process USAGE nodes */
394 394 if (hp_type(node) != HP_NODE_USAGE)
395 395 return (HP_WALK_CONTINUE);
396 396
397 397 (void) strcat(*table, "\n");
398 398 (void) sprintf(&((*table)[strlen(*table)]),
399 399 format, hp_name(node), hp_usage(node));
400 400
401 401 return (HP_WALK_CONTINUE);
402 402 }
403 403
404 404 /*
405 405 * Takes an opaque rcm_info_t pointer and a character pointer, and appends
406 406 * the rcm_info_t data in the form of a table to the given character pointer.
407 407 */
408 408 static void
409 409 pci_rcm_info_table(hp_node_t node, char **table)
410 410 {
411 411 int i;
412 412 size_t w;
413 413 size_t width = 0;
414 414 size_t w_rsrc = 0;
415 415 size_t w_info = 0;
416 416 size_t table_size = 0;
417 417 uint_t tuples = 0;
418 418 char *rsrc;
419 419 char *info;
420 420 char *newtable;
421 421 static char format[MAX_FORMAT];
422 422 const char *infostr;
423 423 error_size_cb_arg_t sizearg;
424 424 error_sum_cb_arg_t sumarg;
425 425
426 426 /* Protect against invalid arguments */
427 427 if (table == NULL)
428 428 return;
429 429
430 430 /* Set localized table header strings */
431 431 rsrc = dgettext(TEXT_DOMAIN, "Resource");
432 432 info = dgettext(TEXT_DOMAIN, "Information");
433 433
434 434 /* A first pass, to size up the RCM information */
435 435 sizearg.rsrc_width = strlen(rsrc);
436 436 sizearg.info_width = strlen(info);
437 437 sizearg.cnt = 0;
438 438 (void) hp_traverse(node, &sizearg, error_sizeup_cb);
439 439
440 440 /* If nothing was sized up above, stop early */
441 441 if (sizearg.cnt == 0)
442 442 return;
443 443
444 444 w_rsrc = sizearg.rsrc_width;
445 445 w_info = sizearg.info_width;
446 446 tuples = sizearg.cnt;
447 447
448 448 /* Adjust column widths for column headings */
449 449 if ((w = strlen(rsrc)) > w_rsrc)
450 450 w_rsrc = w;
451 451 else if ((w_rsrc - w) % 2)
452 452 w_rsrc++;
453 453 if ((w = strlen(info)) > w_info)
454 454 w_info = w;
455 455 else if ((w_info - w) % 2)
456 456 w_info++;
457 457
458 458 /*
459 459 * Compute the total line width of each line,
460 460 * accounting for intercolumn spacing.
461 461 */
462 462 width = w_info + w_rsrc + 4;
463 463
464 464 /* Allocate space for the table */
465 465 table_size = (2 + tuples) * (width + 1) + 2;
466 466 if (*table == NULL) {
467 467 /* zero fill for the strcat() call below */
468 468 *table = calloc(table_size, sizeof (char));
469 469 if (*table == NULL)
470 470 return;
471 471 } else {
472 472 newtable = realloc(*table, strlen(*table) + table_size);
473 473 if (newtable == NULL)
474 474 return;
475 475 else
476 476 *table = newtable;
477 477 }
478 478
479 479 /* Place a table header into the string */
480 480
481 481 /* The resource header */
482 482 (void) strcat(*table, "\n");
483 483 w = strlen(rsrc);
484 484 for (i = 0; i < ((w_rsrc - w) / 2); i++)
485 485 (void) strcat(*table, " ");
486 486 (void) strcat(*table, rsrc);
487 487 for (i = 0; i < ((w_rsrc - w) / 2); i++)
488 488 (void) strcat(*table, " ");
489 489
490 490 /* The information header */
491 491 (void) strcat(*table, " ");
492 492 w = strlen(info);
493 493 for (i = 0; i < ((w_info - w) / 2); i++)
494 494 (void) strcat(*table, " ");
495 495 (void) strcat(*table, info);
496 496 for (i = 0; i < ((w_info - w) / 2); i++)
497 497 (void) strcat(*table, " ");
498 498 /* Underline the headers */
499 499 (void) strcat(*table, "\n");
500 500 for (i = 0; i < w_rsrc; i++)
501 501 (void) strcat(*table, "-");
502 502 (void) strcat(*table, " ");
503 503 for (i = 0; i < w_info; i++)
504 504 (void) strcat(*table, "-");
505 505
506 506 /* Construct the format string */
507 507 (void) snprintf(format, MAX_FORMAT, "%%-%ds %%-%ds",
508 508 (int)w_rsrc, (int)w_info);
509 509
510 510 /* Add the tuples to the table string */
511 511 sumarg.table = table;
512 512 sumarg.format = format;
513 513 (void) hp_traverse(node, &sumarg, error_sumup_cb);
514 514 }
515 515
516 516 /*
517 517 * Figure out the target kernel state for a given cfgadm
518 518 * change-state operation.
519 519 */
520 520 static cfga_err_t
521 521 cfga_target_state(cfga_cmd_t state_change_cmd, int *state)
522 522 {
523 523 switch (state_change_cmd) {
524 524 case CFGA_CMD_CONNECT:
525 525 *state = DDI_HP_CN_STATE_POWERED;
526 526 break;
527 527 case CFGA_CMD_DISCONNECT:
528 528 *state = DDI_HP_CN_STATE_PRESENT;
529 529 break;
530 530 case CFGA_CMD_CONFIGURE:
531 531 *state = DDI_HP_CN_STATE_ENABLED;
532 532 break;
533 533 case CFGA_CMD_UNCONFIGURE:
534 534 *state = DDI_HP_CN_STATE_POWERED;
535 535 break;
536 536 default:
537 537 return (CFGA_ERROR);
538 538 }
539 539
540 540 return (CFGA_OK);
541 541 }
542 542
543 543 /*
544 544 * Translate kernel state to cfgadm receptacle state and occupant state.
545 545 */
546 546 static cfga_err_t
547 547 cfga_get_state(hp_node_t connector, ap_rstate_t *rs, ap_ostate_t *os)
548 548 {
549 549 int state;
550 550 hp_node_t port;
551 551
552 552 state = hp_state(connector);
553 553
554 554 /* Receptacle state */
555 555 switch (state) {
556 556 case DDI_HP_CN_STATE_EMPTY:
557 557 *rs = AP_RSTATE_EMPTY;
558 558 break;
559 559 case DDI_HP_CN_STATE_PRESENT:
560 560 *rs = AP_RSTATE_DISCONNECTED;
561 561 break;
562 562 case DDI_HP_CN_STATE_POWERED:
563 563 case DDI_HP_CN_STATE_ENABLED:
564 564 *rs = AP_RSTATE_CONNECTED;
565 565 break;
566 566 /*
567 567 * Connector state can only be one of
568 568 * Empty, Present, Powered, Enabled.
569 569 */
570 570 default:
571 571 return (CFGA_ERROR);
572 572 }
573 573
574 574 /*
575 575 * Occupant state
576 576 */
577 577 port = hp_child(connector);
578 578 while (port != NULL) {
579 579 DBG(1, ("cfga_get_state:(%x)\n", hp_state(port)));
580 580
581 581 /*
582 582 * Mark occupant state as "configured" if at least one of the
583 583 * associated ports is at state "offline" or above. Driver
584 584 * attach ("online" state) is not necessary here.
585 585 */
586 586 if (hp_state(port) >= DDI_HP_CN_STATE_OFFLINE)
587 587 break;
588 588
589 589 port = hp_sibling(port);
590 590 }
591 591
592 592 if (port != NULL)
593 593 *os = AP_OSTATE_CONFIGURED;
594 594 else
595 595 *os = AP_OSTATE_UNCONFIGURED;
596 596
597 597 return (CFGA_OK);
598 598 }
599 599
600 600 /*
601 601 * Transitional Diagram:
602 602 *
603 603 * empty unconfigure
604 604 * (remove) ^| (physically insert card)
605 605 * |V
606 606 * disconnect configure
607 607 * "-c DISCONNECT" ^| "-c CONNECT"
608 608 * |V "-c CONFIGURE"
609 609 * connect unconfigure -> connect configure
610 610 * <-
611 611 * "-c UNCONFIGURE"
612 612 *
613 613 */
614 614 /*ARGSUSED*/
615 615 cfga_err_t
616 616 cfga_change_state(cfga_cmd_t state_change_cmd, const char *ap_id,
617 617 const char *options, struct cfga_confirm *confp,
618 618 struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
619 619 {
620 620 int rv, state, new_state;
621 621 uint_t hpflags = 0;
622 622 hp_node_t node;
623 623 hp_node_t results = NULL;
624 624
625 625 if ((rv = check_options(options)) != CFGA_OK) {
626 626 return (rv);
627 627 }
628 628
629 629 if (errstring != NULL)
630 630 *errstring = NULL;
631 631
632 632 rv = CFGA_OK;
633 633 DBG(1, ("cfga_change_state:(%s)\n", ap_id));
634 634
635 635 rv = physpath2node(ap_id, errstring, &node);
636 636 if (rv != CFGA_OK)
637 637 return (rv);
638 638
639 639 /*
640 640 * Check for the FORCE flag. It is only used
641 641 * for DISCONNECT or UNCONFIGURE state changes.
642 642 */
643 643 if (flags & CFGA_FLAG_FORCE)
644 644 hpflags |= HPFORCE;
645 645
646 646 state = hp_state(node);
647 647
648 648 /*
649 649 * Which state should we drive to ?
650 650 */
651 651 if ((state_change_cmd != CFGA_CMD_LOAD) &&
652 652 (state_change_cmd != CFGA_CMD_UNLOAD)) {
653 653 if (cfga_target_state(state_change_cmd,
654 654 &new_state) != CFGA_OK) {
655 655 hp_fini(node);
656 656 return (CFGA_ERROR);
657 657 }
658 658 }
659 659
660 660 DBG(1, ("cfga_change_state: state is %d\n", state));
661 661 switch (state_change_cmd) {
662 662 case CFGA_CMD_CONNECT:
663 663 DBG(1, ("connect\n"));
664 664 if (state == DDI_HP_CN_STATE_EMPTY) {
665 665 cfga_err(errstring, ERR_AP_ERR, 0);
666 666 rv = CFGA_INVAL;
667 667 } else if (state == DDI_HP_CN_STATE_PRESENT) {
668 668 /* Connect the slot */
669 669 if (hp_set_state(node, 0, new_state, &results) != 0) {
670 670 rv = CFGA_ERROR;
671 671 cfga_err(errstring, CMD_SLOT_CONNECT, 0);
672 672 }
673 673 }
674 674 break;
675 675
676 676 case CFGA_CMD_DISCONNECT:
677 677 DBG(1, ("disconnect\n"));
678 678 if (state == DDI_HP_CN_STATE_EMPTY) {
679 679 cfga_err(errstring, ERR_AP_ERR, 0);
680 680 rv = CFGA_INVAL;
681 681 } else if (state > DDI_HP_CN_STATE_PRESENT) {
682 682 /* Disconnect the slot */
683 683 rv = hp_set_state(node, hpflags, new_state, &results);
684 684 if (rv != 0) {
685 685 if (rv == EBUSY)
686 686 rv = CFGA_BUSY;
687 687 else
688 688 rv = CFGA_ERROR;
689 689
690 690 if (results) {
691 691 pci_rcm_info_table(results, errstring);
692 692 hp_fini(results);
693 693 } else {
694 694 cfga_err(errstring,
695 695 CMD_SLOT_DISCONNECT, 0);
696 696 }
697 697 }
698 698 }
699 699 break;
700 700
701 701 case CFGA_CMD_CONFIGURE:
702 702 /*
703 703 * for multi-func device we allow multiple
704 704 * configure on the same slot because one
705 705 * func can be configured and other one won't
706 706 */
707 707 DBG(1, ("configure\n"));
708 708 if (state == DDI_HP_CN_STATE_EMPTY) {
709 709 cfga_err(errstring, ERR_AP_ERR, 0);
710 710 rv = CFGA_INVAL;
711 711 } else if (hp_set_state(node, 0, new_state, &results) != 0) {
712 712 rv = CFGA_ERROR;
713 713 cfga_err(errstring, CMD_SLOT_CONFIGURE, 0);
714 714 }
715 715 break;
716 716
717 717 case CFGA_CMD_UNCONFIGURE:
718 718 DBG(1, ("unconfigure\n"));
719 719 if (state == DDI_HP_CN_STATE_EMPTY) {
720 720 cfga_err(errstring, ERR_AP_ERR, 0);
721 721 rv = CFGA_INVAL;
722 722 } else if (state >= DDI_HP_CN_STATE_ENABLED) {
723 723 rv = hp_set_state(node, hpflags, new_state, &results);
724 724 if (rv != 0) {
725 725 if (rv == EBUSY)
726 726 rv = CFGA_BUSY;
727 727 else
728 728 rv = CFGA_ERROR;
729 729
730 730 if (results) {
731 731 pci_rcm_info_table(results, errstring);
732 732 hp_fini(results);
733 733 } else {
734 734 cfga_err(errstring,
735 735 CMD_SLOT_UNCONFIGURE, 0);
736 736 }
737 737 }
738 738 }
739 739 DBG(1, ("unconfigure rv:(%i)\n", rv));
740 740 break;
741 741
742 742 case CFGA_CMD_LOAD:
743 743 /* do nothing, just produce error msg as is */
744 744 if (state < DDI_HP_CN_STATE_POWERED) {
745 745 rv = CFGA_ERROR;
746 746 cfga_err(errstring, CMD_SLOT_INSERT, 0);
747 747 } else {
748 748 cfga_err(errstring, ERR_AP_ERR, 0);
749 749 rv = CFGA_INVAL;
750 750 }
751 751 break;
752 752
753 753 case CFGA_CMD_UNLOAD:
754 754 /* do nothing, just produce error msg as is */
755 755 if (state < DDI_HP_CN_STATE_POWERED) {
756 756 rv = CFGA_ERROR;
757 757 cfga_err(errstring, CMD_SLOT_REMOVE, 0);
758 758 } else {
759 759 cfga_err(errstring, ERR_AP_ERR, 0);
760 760 rv = CFGA_INVAL;
761 761 }
762 762 break;
763 763
764 764 default:
765 765 rv = CFGA_OPNOTSUPP;
766 766 break;
767 767 }
768 768
769 769 hp_fini(node);
770 770 return (rv);
771 771 }
772 772
773 773 char *
774 774 get_val_from_result(char *result)
775 775 {
776 776 char *tmp;
777 777
778 778 tmp = strchr(result, '=');
779 779 if (tmp == NULL)
780 780 return (NULL);
781 781
782 782 tmp++;
783 783 return (tmp);
784 784 }
785 785
786 786 static cfga_err_t
787 787 prt_led_mode(const char *ap_id, int repeat, char **errstring,
788 788 struct cfga_msg *msgp)
789 789 {
790 790 pciehpc_led_t led;
791 791 hp_node_t node;
792 792 char *buff;
793 793 char *buf;
794 794 char *cp, line[MAXLINE];
795 795 char *tmp;
796 796 char *format;
797 797 char *result;
798 798 int i, n, rv;
799 799 int len = MAXLINE;
800 800
801 801 pciehpc_led_t states[] = {
802 802 PCIEHPC_POWER_LED,
803 803 PCIEHPC_FAULT_LED,
804 804 PCIEHPC_ATTN_LED,
805 805 PCIEHPC_ACTIVE_LED
806 806 };
807 807
808 808 DBG(1, ("prt_led_mod function\n"));
809 809 if (!repeat)
810 810 cfga_msg(msgp, "Ap_Id\t\t\tLed");
811 811
812 812 rv = physpath2node(ap_id, errstring, &node);
813 813 if (rv != CFGA_OK)
814 814 return (rv);
815 815
816 816 if ((buff = malloc(MAXPATHLEN)) == NULL) {
817 817 hp_fini(node);
818 818 cfga_err(errstring, "malloc ", 0);
819 819 return (CFGA_ERROR);
820 820 }
821 821
822 822 (void) memset(buff, 0, MAXPATHLEN);
823 823
824 824 if (fix_ap_name(buff, ap_id, hp_name(node),
825 825 errstring) != CFGA_OK) {
826 826 hp_fini(node);
827 827 free(buff);
828 828 return (CFGA_ERROR);
829 829 }
830 830
831 831 cp = line;
832 832 (void) snprintf(cp, len, "%s\t\t", buff);
833 833 len -= strlen(cp);
834 834 cp += strlen(cp);
835 835
836 836 free(buff);
837 837
838 838 n = sizeof (states)/sizeof (pciehpc_led_t);
839 839 for (i = 0; i < n; i++) {
840 840 led = states[i];
841 841
842 842 format = (i == n - 1) ? "%s=%s" : "%s=%s,";
843 843 if (hp_get_private(node, led_strs2[led], &result) != 0) {
844 844 (void) snprintf(cp, len, format,
845 845 led_strs[led], cfga_strs[UNKNOWN]);
846 846 len -= strlen(cp);
847 847 cp += strlen(cp);
848 848 DBG(1, ("%s:%s\n", led_strs[led], cfga_strs[UNKNOWN]));
849 849 } else {
850 850 /*
851 851 * hp_get_private() will return back things like
852 852 * "led_fault=off", transform it to cfgadm desired
853 853 * format.
854 854 */
855 855 tmp = get_val_from_result(result);
856 856 if (tmp == NULL) {
857 857 free(result);
858 858 hp_fini(node);
859 859 return (CFGA_ERROR);
860 860 }
861 861
862 862 (void) snprintf(cp, len, format,
863 863 led_strs[led], tmp);
864 864 len -= strlen(cp);
865 865 cp += strlen(cp);
866 866 DBG(1, ("%s:%s\n", led_strs[led], tmp));
867 867 free(result);
868 868 }
869 869 }
870 870
871 871 cfga_msg(msgp, line); /* print the message */
872 872
873 873 hp_fini(node);
874 874
875 875 return (CFGA_OK);
876 876 }
877 877
878 878 /*ARGSUSED*/
879 879 cfga_err_t
880 880 cfga_private_func(const char *function, const char *ap_id,
881 881 const char *options, struct cfga_confirm *confp,
882 882 struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
883 883 {
884 884 char *str;
885 885 int len, fd, i = 0, repeat = 0;
886 886 char buf[MAXNAMELEN];
887 887 char ptr;
888 888 cfga_err_t rv;
889 889 char *led, *mode;
890 890 hp_node_t node;
891 891 char *result;
892 892
893 893 DBG(1, ("cfgadm_private_func: ap_id:%s\n", ap_id));
894 894 DBG(2, (" options: %s\n", (options == NULL)?"null":options));
895 895 DBG(2, (" confp: %x\n", confp));
896 896 DBG(2, (" cfga_msg: %x\n", cfga_msg));
897 897 DBG(2, (" flag: %d\n", flags));
898 898
899 899 if ((rv = check_options(options)) != CFGA_OK) {
900 900 return (rv);
901 901 }
902 902
903 903 if (private_check == confp)
904 904 repeat = 1;
905 905 else
906 906 private_check = (void*)confp;
907 907
908 908 for (i = 0, str = func_strs[i], len = strlen(str);
909 909 func_strs[i] != NULL; i++) {
910 910 str = func_strs[i];
911 911 len = strlen(str);
912 912 if (strncmp(function, str, len) == 0)
913 913 break;
914 914 }
915 915
916 916 switch (i) {
917 917 case ENABLE_SLOT:
918 918 case DISABLE_SLOT:
919 919 /* pass through */
920 920 case ENABLE_AUTOCNF:
921 921 case DISABLE_AUTOCNF:
922 922 /* no action needed */
923 923 return (CFGA_OK);
924 924 break;
925 925 case LED:
926 926 /* set mode */
927 927 ptr = function[len++];
928 928 if (ptr == '=') {
929 929 str = (char *)function;
930 930 for (str = (str+len++), i = 0; *str != ',';
931 931 i++, str++) {
932 932 if (i == (MAXNAMELEN - 1))
933 933 break;
934 934
935 935 buf[i] = *str;
936 936 DBG_F(2, (stdout, "%c\n", buf[i]));
937 937 }
938 938 buf[i] = '\0'; str++;
939 939 DBG(2, ("buf = %s\n", buf));
940 940
941 941 /* ACTIVE=3,ATTN=2,POWER=1,FAULT=0 */
942 942 if (strcmp(buf, led_strs[POWER]) == 0)
943 943 led = PCIEHPC_PROP_LED_POWER;
944 944 else if (strcmp(buf, led_strs[FAULT]) == 0)
945 945 led = PCIEHPC_PROP_LED_FAULT;
↓ open down ↓ |
945 lines elided |
↑ open up ↑ |
946 946 else if (strcmp(buf, led_strs[ATTN]) == 0)
947 947 led = PCIEHPC_PROP_LED_ATTN;
948 948 else if (strcmp(buf, led_strs[ACTIVE]) == 0)
949 949 led = PCIEHPC_PROP_LED_ACTIVE;
950 950 else return (CFGA_INVAL);
951 951
952 952 len = strlen(func_strs[MODE]);
953 953 if ((strncmp(str, func_strs[MODE], len) == 0) &&
954 954 (*(str+(len)) == '=')) {
955 955 for (str = (str+(++len)), i = 0;
956 - *str != NULL; i++, str++) {
956 + *str != '\0'; i++, str++) {
957 957 buf[i] = *str;
958 958 }
959 959 }
960 960 buf[i] = '\0';
961 961 DBG(2, ("buf_mode= %s\n", buf));
962 962
963 963 /* ON = 1, OFF = 0 */
964 964 if (strcmp(buf, mode_strs[ON]) == 0)
965 965 mode = PCIEHPC_PROP_VALUE_ON;
966 966 else if (strcmp(buf, mode_strs[OFF]) == 0)
967 967 mode = PCIEHPC_PROP_VALUE_OFF;
968 968 else if (strcmp(buf, mode_strs[BLINK]) == 0)
969 969 mode = PCIEHPC_PROP_VALUE_BLINK;
970 970 else return (CFGA_INVAL);
971 971
972 972 /* sendin */
973 973 memset(buf, 0, sizeof (buf));
974 974 snprintf(buf, sizeof (buf), "%s=%s",
975 975 led, mode);
976 976 buf[MAXNAMELEN - 1] = '\0';
977 977
978 978 break;
979 979 } else if (ptr == '\0') {
980 980 /* print mode */
981 981 DBG(1, ("Print mode\n"));
982 982 return (prt_led_mode(ap_id, repeat, errstring,
983 983 msgp));
984 984 }
985 985 default:
986 986 DBG(1, ("default\n"));
987 987 errno = EINVAL;
988 988 return (CFGA_INVAL);
989 989 }
990 990
991 991 rv = physpath2node(ap_id, errstring, &node);
992 992 if (rv != CFGA_OK)
993 993 return (rv);
994 994
995 995 if (hp_set_private(node, buf, &result) != 0) {
996 996 hp_fini(node);
997 997 return (CFGA_ERROR);
998 998 }
999 999
1000 1000 hp_fini(node);
1001 1001 return (CFGA_OK);
1002 1002 }
1003 1003
1004 1004 /*ARGSUSED*/
1005 1005 cfga_err_t cfga_test(const char *ap_id, const char *options,
1006 1006 struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
1007 1007 {
1008 1008 cfga_err_t rv;
1009 1009 if (errstring != NULL)
1010 1010 *errstring = NULL;
1011 1011
1012 1012 if ((rv = check_options(options)) != CFGA_OK) {
1013 1013 return (rv);
1014 1014 }
1015 1015
1016 1016 DBG(1, ("cfga_test:(%s)\n", ap_id));
1017 1017 /* will need to implement pci CTRL command */
1018 1018 return (CFGA_NOTSUPP);
1019 1019 }
1020 1020
1021 1021 /*
1022 1022 * The slot-names property describes the external labeling of add-in slots.
1023 1023 * This property is an encoded array, an integer followed by a list of
1024 1024 * strings. The return value from di_prop_lookup_ints for slot-names is -1.
1025 1025 * The expected return value should be the number of elements.
1026 1026 * Di_prop_decode_common does not decode encoded data from software,
1027 1027 * such as the solaris device tree, unlike from the prom.
1028 1028 * Di_prop_decode_common takes the size of the encoded data and mods
1029 1029 * it with the size of int. The size of the encoded data for slot-names is 9
1030 1030 * and the size of int is 4, yielding a non zero result. A value of -1 is used
1031 1031 * to indicate that the number of elements can not be determined.
1032 1032 * Di_prop_decode_common can be modified to decode encoded data from the solaris
1033 1033 * device tree.
1034 1034 */
1035 1035 static int
1036 1036 fixup_slotname(int rval, int *intp, struct searcharg *slotarg)
1037 1037 {
1038 1038 if ((slotarg->slt_name_src == PROM_SLT_NAME) && (rval == -1)) {
1039 1039 return (DI_WALK_TERMINATE);
1040 1040 } else {
1041 1041 int i;
1042 1042 char *tmptr = (char *)(intp+1);
1043 1043 DBG(1, ("slot-bitmask: %x \n", *intp));
1044 1044
1045 1045 rval = (rval -1) * 4;
1046 1046
1047 1047 for (i = 0; i <= slotarg->minor; i++) {
1048 1048 DBG(2, ("curr slot-name: %s \n", tmptr));
1049 1049
1050 1050 if (i >= MAXDEVS)
1051 1051 return (DI_WALK_TERMINATE);
1052 1052
1053 1053 if ((*intp >> i) & 1) {
1054 1054 /* assign tmptr */
1055 1055 DBG(2, ("slot-name: %s \n", tmptr));
1056 1056 if (i == slotarg->minor)
1057 1057 (void) strcpy(slotarg->slotnames[i],
1058 1058 tmptr);
1059 1059 /* wind tmptr to next \0 */
1060 1060 while (*tmptr != '\0') {
1061 1061 tmptr++;
1062 1062 }
1063 1063 tmptr++;
1064 1064 } else {
1065 1065 /* point at unknown string */
1066 1066 if (i == slotarg->minor)
1067 1067 (void) strcpy(slotarg->slotnames[i],
1068 1068 "unknown");
1069 1069 }
1070 1070 }
1071 1071 }
1072 1072 return (DI_WALK_TERMINATE);
1073 1073 }
1074 1074
1075 1075 static int
1076 1076 find_slotname(di_node_t din, di_minor_t dim, void *arg)
1077 1077 {
1078 1078 struct searcharg *slotarg = (struct searcharg *)arg;
1079 1079 di_prom_handle_t ph = (di_prom_handle_t)slotarg->promp;
1080 1080 di_prom_prop_t prom_prop;
1081 1081 di_prop_t solaris_prop;
1082 1082 int *intp, rval;
1083 1083 char *devname;
1084 1084 char fulldevname[MAXNAMELEN];
1085 1085
1086 1086 slotarg->minor = dim->dev_minor % 256;
1087 1087
1088 1088 DBG(2, ("minor number:(%i)\n", slotarg->minor));
1089 1089 DBG(2, ("hot plug slots found so far:(%i)\n", 0));
1090 1090
1091 1091 if ((devname = di_devfs_path(din)) != NULL) {
1092 1092 (void) snprintf(fulldevname, MAXNAMELEN,
1093 1093 "/devices%s:%s", devname, di_minor_name(dim));
1094 1094 di_devfs_path_free(devname);
1095 1095 }
1096 1096
1097 1097 if (strcmp(fulldevname, slotarg->devpath) == 0) {
1098 1098
1099 1099 /*
1100 1100 * Check the Solaris device tree first
1101 1101 * in the case of a DR operation
1102 1102 */
1103 1103 solaris_prop = di_prop_hw_next(din, DI_PROP_NIL);
1104 1104 while (solaris_prop != DI_PROP_NIL) {
1105 1105 if (strcmp("slot-names", di_prop_name(solaris_prop))
1106 1106 == 0) {
1107 1107 rval = di_prop_lookup_ints(DDI_DEV_T_ANY,
1108 1108 din, di_prop_name(solaris_prop), &intp);
1109 1109 slotarg->slt_name_src = SOLARIS_SLT_NAME;
1110 1110
1111 1111 return (fixup_slotname(rval, intp, slotarg));
1112 1112 }
1113 1113 solaris_prop = di_prop_hw_next(din, solaris_prop);
1114 1114 }
1115 1115
1116 1116 /*
1117 1117 * Check the prom device tree which is populated at boot.
1118 1118 * If this fails, give up and set the slot name to null.
1119 1119 */
1120 1120 prom_prop = di_prom_prop_next(ph, din, DI_PROM_PROP_NIL);
1121 1121 while (prom_prop != DI_PROM_PROP_NIL) {
1122 1122 if (strcmp("slot-names", di_prom_prop_name(prom_prop))
1123 1123 == 0) {
1124 1124 rval = di_prom_prop_lookup_ints(ph,
1125 1125 din, di_prom_prop_name(prom_prop), &intp);
1126 1126 slotarg->slt_name_src = PROM_SLT_NAME;
1127 1127
1128 1128 return (fixup_slotname(rval, intp, slotarg));
1129 1129 }
1130 1130 prom_prop = di_prom_prop_next(ph, din, prom_prop);
1131 1131 }
1132 1132 *slotarg->slotnames[slotarg->minor] = '\0';
1133 1133 return (DI_WALK_TERMINATE);
1134 1134 } else
1135 1135 return (DI_WALK_CONTINUE);
1136 1136 }
1137 1137
1138 1138 static int
1139 1139 find_physical_slot_names(const char *devcomp, struct searcharg *slotarg)
1140 1140 {
1141 1141 di_node_t root_node;
1142 1142
1143 1143 DBG(1, ("find_physical_slot_names\n"));
1144 1144
1145 1145 if ((root_node = di_init("/", DINFOCPYALL|DINFOPATH))
1146 1146 == DI_NODE_NIL) {
1147 1147 DBG(1, ("di_init() failed\n"));
1148 1148 return (-1);
1149 1149 }
1150 1150
1151 1151 slotarg->devpath = (char *)devcomp;
1152 1152
1153 1153 if ((slotarg->promp = di_prom_init()) == DI_PROM_HANDLE_NIL) {
1154 1154 DBG(1, ("di_prom_init() failed\n"));
1155 1155 di_fini(root_node);
1156 1156 return (-1);
1157 1157 }
1158 1158
1159 1159 (void) di_walk_minor(root_node, "ddi_ctl:attachment_point:pci",
1160 1160 0, (void *)slotarg, find_slotname);
1161 1161
1162 1162 di_prom_fini(slotarg->promp);
1163 1163 di_fini(root_node);
1164 1164 if (slotarg->slotnames[0] != NULL)
1165 1165 return (0);
1166 1166 else
1167 1167 return (-1);
1168 1168 }
1169 1169
1170 1170 static void
1171 1171 get_type(const char *boardtype, const char *cardtype, char *buf)
1172 1172 {
1173 1173 /* for type string assembly in get_type() */
1174 1174 #define TPCT(s) (void) strlcat(buf, (s), CFGA_TYPE_LEN)
1175 1175
1176 1176 int i;
1177 1177
1178 1178 if (strcmp(cardtype, "unknown") == 0) {
1179 1179 TPCT("unknown");
1180 1180 return;
1181 1181 }
1182 1182
1183 1183 TPCT(cardtype);
1184 1184 TPCT("/");
1185 1185
1186 1186 if (strcmp(boardtype, PCIEHPC_PROP_VALUE_PCIHOTPLUG) == 0)
1187 1187 TPCT(board_strs[PCIEHPC_BOARD_PCI_HOTPLUG]);
1188 1188 else
1189 1189 TPCT(board_strs[PCIEHPC_BOARD_UNKNOWN]);
1190 1190 }
1191 1191
1192 1192 /*
1193 1193 * call-back function for di_devlink_walk
1194 1194 * if the link lives in /dev/cfg copy its name
1195 1195 */
1196 1196 static int
1197 1197 found_devlink(di_devlink_t link, void *ap_log_id)
1198 1198 {
1199 1199 if (strncmp("/dev/cfg/", di_devlink_path(link), 9) == 0) {
1200 1200 /* copy everything but /dev/cfg/ */
1201 1201 (void) strcpy((char *)ap_log_id, di_devlink_path(link) + 9);
1202 1202 DBG(1, ("found_devlink: %s\n", (char *)ap_log_id));
1203 1203 return (DI_WALK_TERMINATE);
1204 1204 }
1205 1205 return (DI_WALK_CONTINUE);
1206 1206 }
1207 1207
1208 1208 /*
1209 1209 * Walk throught the cached /dev link tree looking for links to the ap
1210 1210 * if none are found return an error
1211 1211 */
1212 1212 static cfga_err_t
1213 1213 check_devlinks(char *ap_log_id, const char *ap_id)
1214 1214 {
1215 1215 di_devlink_handle_t hdl;
1216 1216
1217 1217 DBG(1, ("check_devlinks: %s\n", ap_id));
1218 1218
1219 1219 hdl = di_devlink_init(NULL, 0);
1220 1220
1221 1221 if (strncmp("/devices/", ap_id, 9) == 0) {
1222 1222 /* ap_id is a valid minor_path with /devices prepended */
1223 1223 (void) di_devlink_walk(hdl, NULL, ap_id + 8, DI_PRIMARY_LINK,
1224 1224 (void *)ap_log_id, found_devlink);
1225 1225 } else {
1226 1226 DBG(1, ("check_devlinks: invalid ap_id: %s\n", ap_id));
1227 1227 return (CFGA_ERROR);
1228 1228 }
1229 1229
1230 1230 (void) di_devlink_fini(&hdl);
1231 1231
1232 1232 if (ap_log_id[0] != '\0')
1233 1233 return (CFGA_OK);
1234 1234 else
1235 1235 return (CFGA_ERROR);
1236 1236 }
1237 1237
1238 1238 /*
1239 1239 * most of this is needed to compensate for
1240 1240 * differences between various platforms
1241 1241 */
1242 1242 static cfga_err_t
1243 1243 fix_ap_name(char *ap_log_id, const char *ap_id, char *slot_name,
1244 1244 char **errstring)
1245 1245 {
1246 1246 char *buf;
1247 1247 char *tmp;
1248 1248 char *ptr;
1249 1249
1250 1250 di_node_t ap_node;
1251 1251
1252 1252 ap_log_id[0] = '\0';
1253 1253
1254 1254 if (check_devlinks(ap_log_id, ap_id) == CFGA_OK)
1255 1255 return (CFGA_OK);
1256 1256
1257 1257 DBG(1, ("fix_ap_name: %s\n", ap_id));
1258 1258
1259 1259 if ((buf = malloc(strlen(ap_id) + 1)) == NULL) {
1260 1260 DBG(1, ("malloc failed\n"));
1261 1261 return (CFGA_ERROR);
1262 1262 }
1263 1263 (void) strcpy(buf, ap_id);
1264 1264 tmp = buf + sizeof ("/devices") - 1;
1265 1265
1266 1266 ptr = strchr(tmp, ':');
1267 1267 ptr[0] = '\0';
1268 1268
1269 1269 DBG(1, ("fix_ap_name: %s\n", tmp));
1270 1270
1271 1271 ap_node = di_init(tmp, DINFOMINOR);
1272 1272 if (ap_node == DI_NODE_NIL) {
1273 1273 cfga_err(errstring, "di_init ", 0);
1274 1274 DBG(1, ("fix_ap_name: failed to snapshot node\n"));
1275 1275 return (CFGA_ERROR);
1276 1276 }
1277 1277
1278 1278 (void) snprintf(ap_log_id, strlen(ap_id) + 1, "%s%i:%s",
1279 1279 di_driver_name(ap_node), di_instance(ap_node), slot_name);
1280 1280
1281 1281 DBG(1, ("fix_ap_name: %s\n", ap_log_id));
1282 1282
1283 1283 di_fini(ap_node);
1284 1284
1285 1285 free(buf);
1286 1286 return (CFGA_OK);
1287 1287 }
1288 1288
1289 1289
1290 1290 static int
1291 1291 findlink_cb(di_devlink_t devlink, void *arg)
1292 1292 {
1293 1293 (*(char **)arg) = strdup(di_devlink_path(devlink));
1294 1294
1295 1295 return (DI_WALK_TERMINATE);
1296 1296 }
1297 1297
1298 1298 /*
1299 1299 * returns an allocated string containing the full path to the devlink for
1300 1300 * <ap_phys_id> in the devlink database; we expect only one devlink per
1301 1301 * <ap_phys_id> so we return the first encountered
1302 1302 */
1303 1303 static char *
1304 1304 findlink(char *ap_phys_id)
1305 1305 {
1306 1306 di_devlink_handle_t hdl;
1307 1307 char *path = NULL;
1308 1308
1309 1309 hdl = di_devlink_init(NULL, 0);
1310 1310
1311 1311 if (strncmp("/devices/", ap_phys_id, 9) == 0)
1312 1312 ap_phys_id += 8;
1313 1313
1314 1314 (void) di_devlink_walk(hdl, "^cfg/.+$", ap_phys_id, DI_PRIMARY_LINK,
1315 1315 (void *)&path, findlink_cb);
1316 1316
1317 1317 (void) di_devlink_fini(&hdl);
1318 1318 return (path);
1319 1319 }
1320 1320
1321 1321
1322 1322 /*
1323 1323 * returns CFGA_OK if it can succesfully retrieve the devlink info associated
1324 1324 * with devlink for <ap_phys_id> which will be returned through <ap_info>
1325 1325 */
1326 1326 cfga_err_t
1327 1327 get_dli(char *dlpath, char *ap_info, int ap_info_sz)
1328 1328 {
1329 1329 int fd;
1330 1330
1331 1331 fd = di_dli_openr(dlpath);
1332 1332 if (fd < 0)
1333 1333 return (CFGA_ERROR);
1334 1334
1335 1335 (void) read(fd, ap_info, ap_info_sz);
1336 1336 ap_info[ap_info_sz - 1] = '\0';
1337 1337
1338 1338 di_dli_close(fd);
1339 1339 return (CFGA_OK);
1340 1340 }
1341 1341
1342 1342 static cfga_err_t
1343 1343 cfga_get_condition(hp_node_t node, ap_condition_t *cond)
1344 1344 {
1345 1345 char *condition;
1346 1346
1347 1347 /* "condition" bus specific commands */
1348 1348 if (hp_get_private(node, PCIEHPC_PROP_SLOT_CONDITION,
1349 1349 &condition) != 0) {
1350 1350 *cond = AP_COND_UNKNOWN;
1351 1351 return (CFGA_ERROR);
1352 1352 }
1353 1353
1354 1354 condition = get_val_from_result(condition);
1355 1355
1356 1356 if (strcmp(condition, PCIEHPC_PROP_COND_OK) == 0)
1357 1357 *cond = AP_COND_OK;
1358 1358 else if (strcmp(condition, PCIEHPC_PROP_COND_FAILING) == 0)
1359 1359 *cond = AP_COND_FAILING;
1360 1360 else if (strcmp(condition, PCIEHPC_PROP_COND_FAILED) == 0)
1361 1361 *cond = AP_COND_FAILED;
1362 1362 else if (strcmp(condition, PCIEHPC_PROP_COND_UNUSABLE) == 0)
1363 1363 *cond = AP_COND_UNUSABLE;
1364 1364 else if (strcmp(condition, PCIEHPC_PROP_COND_UNKNOWN) == 0)
1365 1365 *cond = AP_COND_UNKNOWN;
1366 1366 else
1367 1367 return (CFGA_ERROR);
1368 1368
1369 1369 return (CFGA_OK);
1370 1370 }
1371 1371
1372 1372 /*ARGSUSED*/
1373 1373 cfga_err_t
1374 1374 cfga_list_ext(const char *ap_id, cfga_list_data_t **cs,
1375 1375 int *nlist, const char *options, const char *listopts, char **errstring,
1376 1376 cfga_flags_t flags)
1377 1377 {
1378 1378 char *boardtype;
1379 1379 char *cardtype;
1380 1380 struct searcharg slotname_arg;
1381 1381 int fd;
1382 1382 int rv = CFGA_OK;
1383 1383 char *dlpath = NULL;
1384 1384 hp_node_t node;
1385 1385 ap_rstate_t rs;
1386 1386 ap_ostate_t os;
1387 1387 ap_condition_t cond;
1388 1388
1389 1389 if ((rv = check_options(options)) != CFGA_OK) {
1390 1390 return (rv);
1391 1391 }
1392 1392
1393 1393 if (errstring != NULL)
1394 1394 *errstring = NULL;
1395 1395
1396 1396 DBG(1, ("cfga_list_ext:(%s)\n", ap_id));
1397 1397
1398 1398 if (cs == NULL || nlist == NULL) {
1399 1399 rv = CFGA_ERROR;
1400 1400 return (rv);
1401 1401 }
1402 1402
1403 1403 *nlist = 1;
1404 1404
1405 1405 if ((*cs = malloc(sizeof (cfga_list_data_t))) == NULL) {
1406 1406 cfga_err(errstring, "malloc ", 0);
1407 1407 DBG(1, ("malloc failed\n"));
1408 1408 rv = CFGA_ERROR;
1409 1409 return (rv);
1410 1410 }
1411 1411 (void) memset(*cs, 0, sizeof (cfga_list_data_t));
1412 1412
1413 1413 rv = physpath2node(ap_id, errstring, &node);
1414 1414 if (rv != CFGA_OK) {
1415 1415 DBG(1, ("physpath2node failed\n"));
1416 1416 return (rv);
1417 1417 }
1418 1418
1419 1419 if (cfga_get_state(node, &rs, &os) != CFGA_OK) {
1420 1420 DBG(1, ("cfga_get_state failed\n"));
1421 1421 hp_fini(node);
1422 1422 return (CFGA_ERROR);
1423 1423 }
1424 1424
1425 1425 switch (rs) {
1426 1426 case AP_RSTATE_EMPTY:
1427 1427 (*cs)->ap_r_state = CFGA_STAT_EMPTY;
1428 1428 DBG(2, ("ap_rstate = CFGA_STAT_EMPTY\n"));
1429 1429 break;
1430 1430 case AP_RSTATE_DISCONNECTED:
1431 1431 (*cs)->ap_r_state = CFGA_STAT_DISCONNECTED;
1432 1432 DBG(2, ("ap_rstate = CFGA_STAT_DISCONNECTED\n"));
1433 1433 break;
1434 1434 case AP_RSTATE_CONNECTED:
1435 1435 (*cs)->ap_r_state = CFGA_STAT_CONNECTED;
1436 1436 DBG(2, ("ap_rstate = CFGA_STAT_CONNECTED\n"));
1437 1437 break;
1438 1438 default:
1439 1439 cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1440 1440 rv = CFGA_ERROR;
1441 1441 hp_fini(node);
1442 1442 return (rv);
1443 1443 }
1444 1444
1445 1445 switch (os) {
1446 1446 case AP_OSTATE_CONFIGURED:
1447 1447 (*cs)->ap_o_state = CFGA_STAT_CONFIGURED;
1448 1448 DBG(2, ("ap_ostate = CFGA_STAT_CONFIGURED\n"));
1449 1449 break;
1450 1450 case AP_OSTATE_UNCONFIGURED:
1451 1451 (*cs)->ap_o_state = CFGA_STAT_UNCONFIGURED;
1452 1452 DBG(2, ("ap_ostate = CFGA_STAT_UNCONFIGURED\n"));
1453 1453 break;
1454 1454 default:
1455 1455 cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1456 1456 rv = CFGA_ERROR;
1457 1457 hp_fini(node);
1458 1458 return (rv);
1459 1459 }
1460 1460
1461 1461 (void) cfga_get_condition(node, &cond);
1462 1462
1463 1463 switch (cond) {
1464 1464 case AP_COND_OK:
1465 1465 (*cs)->ap_cond = CFGA_COND_OK;
1466 1466 DBG(2, ("ap_cond = CFGA_COND_OK\n"));
1467 1467 break;
1468 1468 case AP_COND_FAILING:
1469 1469 (*cs)->ap_cond = CFGA_COND_FAILING;
1470 1470 DBG(2, ("ap_cond = CFGA_COND_FAILING\n"));
1471 1471 break;
1472 1472 case AP_COND_FAILED:
1473 1473 (*cs)->ap_cond = CFGA_COND_FAILED;
1474 1474 DBG(2, ("ap_cond = CFGA_COND_FAILED\n"));
1475 1475 break;
1476 1476 case AP_COND_UNUSABLE:
1477 1477 (*cs)->ap_cond = CFGA_COND_UNUSABLE;
1478 1478 DBG(2, ("ap_cond = CFGA_COND_UNUSABLE\n"));
1479 1479 break;
1480 1480 case AP_COND_UNKNOWN:
1481 1481 (*cs)->ap_cond = CFGA_COND_UNKNOWN;
1482 1482 DBG(2, ("ap_cond = CFGA_COND_UNKNOW\n"));
1483 1483 break;
1484 1484 default:
1485 1485 cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1486 1486 rv = CFGA_ERROR;
1487 1487 hp_fini(node);
1488 1488 return (rv);
1489 1489 }
1490 1490 /*
1491 1491 * We're not busy since the entrance into the kernel has been
1492 1492 * sync'ed via libhotplug.
1493 1493 */
1494 1494 (*cs)->ap_busy = 0;
1495 1495
1496 1496 /* last change */
1497 1497 (*cs)->ap_status_time = hp_last_change(node);
1498 1498
1499 1499 /* board type */
1500 1500 if (hp_get_private(node, PCIEHPC_PROP_BOARD_TYPE, &boardtype) != 0)
1501 1501 boardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1502 1502 else
1503 1503 boardtype = get_val_from_result(boardtype);
1504 1504
1505 1505 /* card type */
1506 1506 if (hp_get_private(node, PCIEHPC_PROP_CARD_TYPE, &cardtype) != 0)
1507 1507 cardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1508 1508 else
1509 1509 cardtype = get_val_from_result(cardtype);
1510 1510
1511 1511 /* logical ap_id */
1512 1512 rv = fix_ap_name((*cs)->ap_log_id, ap_id,
1513 1513 hp_name(node), errstring);
1514 1514 DBG(1, ("logical id: %s\n", (*cs)->ap_log_id));
1515 1515 /* physical ap_id */
1516 1516 (void) strcpy((*cs)->ap_phys_id, ap_id); /* physical path of AP */
1517 1517
1518 1518 /* information */
1519 1519 dlpath = findlink((*cs)->ap_phys_id);
1520 1520 if (dlpath != NULL) {
1521 1521 if (get_dli(dlpath, (*cs)->ap_info,
1522 1522 sizeof ((*cs)->ap_info)) != CFGA_OK)
1523 1523 (*cs)->ap_info[0] = '\0';
1524 1524 free(dlpath);
1525 1525 }
1526 1526
1527 1527 if ((*cs)->ap_log_id[0] == '\0')
1528 1528 (void) strcpy((*cs)->ap_log_id, hp_name(node));
1529 1529
1530 1530 if ((*cs)->ap_info[0] == '\0') {
1531 1531 /* slot_names of bus node */
1532 1532 if (find_physical_slot_names(ap_id, &slotname_arg) != -1)
1533 1533 (void) strcpy((*cs)->ap_info,
1534 1534 slotname_arg.slotnames[slotname_arg.minor]);
1535 1535 }
1536 1536
1537 1537 /* class_code/subclass/boardtype */
1538 1538 get_type(boardtype, cardtype, (*cs)->ap_type);
1539 1539
1540 1540 DBG(1, ("cfga_list_ext return success\n"));
1541 1541 rv = CFGA_OK;
1542 1542
1543 1543 hp_fini(node);
1544 1544 return (rv);
1545 1545 }
1546 1546
1547 1547 /*
1548 1548 * This routine prints a single line of help message
1549 1549 */
1550 1550 static void
1551 1551 cfga_msg(struct cfga_msg *msgp, const char *str)
1552 1552 {
1553 1553 DBG(2, ("<%s>", str));
1554 1554
1555 1555 if (msgp == NULL || msgp->message_routine == NULL)
1556 1556 return;
1557 1557
1558 1558 (*msgp->message_routine)(msgp->appdata_ptr, str);
1559 1559 (*msgp->message_routine)(msgp->appdata_ptr, "\n");
1560 1560 }
1561 1561
1562 1562 static cfga_err_t
1563 1563 check_options(const char *options)
1564 1564 {
1565 1565 struct cfga_msg *msgp = NULL;
1566 1566
1567 1567 if (options) {
1568 1568 cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1569 1569 cfga_msg(msgp, options);
1570 1570 return (CFGA_INVAL);
1571 1571 }
1572 1572 return (CFGA_OK);
1573 1573 }
1574 1574
1575 1575 /*ARGSUSED*/
1576 1576 cfga_err_t
1577 1577 cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
1578 1578 {
1579 1579 if (options) {
1580 1580 cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1581 1581 cfga_msg(msgp, options);
1582 1582 }
1583 1583 DBG(1, ("cfga_help\n"));
1584 1584
1585 1585 cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_HEADER]));
1586 1586 cfga_msg(msgp, cfga_strs[HELP_CONFIG]);
1587 1587 cfga_msg(msgp, cfga_strs[HELP_ENABLE_SLOT]);
1588 1588 cfga_msg(msgp, cfga_strs[HELP_DISABLE_SLOT]);
1589 1589 cfga_msg(msgp, cfga_strs[HELP_ENABLE_AUTOCONF]);
1590 1590 cfga_msg(msgp, cfga_strs[HELP_DISABLE_AUTOCONF]);
1591 1591 cfga_msg(msgp, cfga_strs[HELP_LED_CNTRL]);
1592 1592 return (CFGA_OK);
1593 1593 }
1594 1594
1595 1595 /*
1596 1596 * cfga_err() accepts a variable number of message IDs and constructs
1597 1597 * a corresponding error string which is returned via the errstring argument.
1598 1598 * cfga_err() calls gettext() to internationalize proper messages.
1599 1599 */
1600 1600 static void
1601 1601 cfga_err(char **errstring, ...)
1602 1602 {
1603 1603 int a;
1604 1604 int i;
1605 1605 int n;
1606 1606 int len;
1607 1607 int flen;
1608 1608 char *p;
1609 1609 char *q;
1610 1610 char *s[32];
1611 1611 char *failed;
1612 1612 va_list ap;
1613 1613
1614 1614 /*
1615 1615 * If errstring is null it means user is not interested in getting
1616 1616 * error status. So we don't do all the work
1617 1617 */
1618 1618 if (errstring == NULL) {
1619 1619 return;
1620 1620 }
1621 1621 va_start(ap, errstring);
1622 1622
1623 1623 failed = dgettext(TEXT_DOMAIN, cfga_strs[FAILED]);
1624 1624 flen = strlen(failed);
1625 1625
1626 1626 for (n = len = 0; (a = va_arg(ap, int)) != 0; n++) {
1627 1627 switch (a) {
1628 1628 case CMD_GETSTAT:
1629 1629 case CMD_LIST:
1630 1630 case CMD_SLOT_CONNECT:
1631 1631 case CMD_SLOT_DISCONNECT:
1632 1632 case CMD_SLOT_CONFIGURE:
1633 1633 case CMD_SLOT_UNCONFIGURE:
1634 1634 p = cfga_errstrs(a);
1635 1635 len += (strlen(p) + flen);
1636 1636 s[n] = p;
1637 1637 s[++n] = cfga_strs[FAILED];
1638 1638
1639 1639 DBG(2, ("<%s>", p));
1640 1640 DBG(2, (cfga_strs[FAILED]));
1641 1641 break;
1642 1642
1643 1643 case ERR_CMD_INVAL:
1644 1644 case ERR_AP_INVAL:
1645 1645 case ERR_OPT_INVAL:
1646 1646 case ERR_AP_ERR:
1647 1647 switch (a) {
1648 1648 case ERR_CMD_INVAL:
1649 1649 p = dgettext(TEXT_DOMAIN,
1650 1650 cfga_errstrs[ERR_CMD_INVAL]);
1651 1651 break;
1652 1652 case ERR_AP_INVAL:
1653 1653 p = dgettext(TEXT_DOMAIN,
1654 1654 cfga_errstrs[ERR_AP_INVAL]);
1655 1655 break;
1656 1656 case ERR_OPT_INVAL:
1657 1657 p = dgettext(TEXT_DOMAIN,
1658 1658 cfga_errstrs[ERR_OPT_INVAL]);
1659 1659 break;
1660 1660 case ERR_AP_ERR:
1661 1661 p = dgettext(TEXT_DOMAIN,
1662 1662 cfga_errstrs[ERR_AP_ERR]);
1663 1663 break;
1664 1664 }
1665 1665
1666 1666 if ((q = va_arg(ap, char *)) != NULL) {
1667 1667 len += (strlen(p) + strlen(q));
1668 1668 s[n] = p;
1669 1669 s[++n] = q;
1670 1670 DBG(2, ("<%s>", p));
1671 1671 DBG(2, ("<%s>", q));
1672 1672 break;
1673 1673 } else {
1674 1674 len += strlen(p);
1675 1675 s[n] = p;
1676 1676
1677 1677 }
1678 1678 DBG(2, ("<%s>", p));
1679 1679 break;
1680 1680
1681 1681 default:
1682 1682 n--;
1683 1683 break;
1684 1684 }
1685 1685 }
1686 1686
1687 1687 DBG(2, ("\n"));
1688 1688 va_end(ap);
1689 1689
1690 1690 if ((p = calloc(len + 1, 1)) == NULL)
1691 1691 return;
1692 1692
1693 1693 for (i = 0; i < n; i++) {
1694 1694 (void) strlcat(p, s[i], len + 1);
1695 1695 DBG(2, ("i:%d, %s\n", i, s[i]));
1696 1696 }
1697 1697
1698 1698 *errstring = p;
1699 1699 DBG(2, ("%s\n", *errstring));
1700 1700 }
1701 1701
1702 1702 /*
1703 1703 * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
1704 1704 */
↓ open down ↓ |
738 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX