1*3530Srb144127 /* 2*3530Srb144127 * CDDL HEADER START 3*3530Srb144127 * 4*3530Srb144127 * The contents of this file are subject to the terms of the 5*3530Srb144127 * Common Development and Distribution License (the "License"). 6*3530Srb144127 * You may not use this file except in compliance with the License. 7*3530Srb144127 * 8*3530Srb144127 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*3530Srb144127 * or http://www.opensolaris.org/os/licensing. 10*3530Srb144127 * See the License for the specific language governing permissions 11*3530Srb144127 * and limitations under the License. 12*3530Srb144127 * 13*3530Srb144127 * When distributing Covered Code, include this CDDL HEADER in each 14*3530Srb144127 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*3530Srb144127 * If applicable, add the following below this CDDL HEADER, with the 16*3530Srb144127 * fields enclosed by brackets "[]" replaced with your own identifying 17*3530Srb144127 * information: Portions Copyright [yyyy] [name of copyright owner] 18*3530Srb144127 * 19*3530Srb144127 * CDDL HEADER END 20*3530Srb144127 */ 21*3530Srb144127 /* 22*3530Srb144127 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23*3530Srb144127 * Use is subject to license terms. 24*3530Srb144127 */ 25*3530Srb144127 26*3530Srb144127 #pragma ident "%Z%%M% %I% %E% SMI" 27*3530Srb144127 28*3530Srb144127 #include <stdio.h> 29*3530Srb144127 #include <sys/param.h> 30*3530Srb144127 #include <fcntl.h> 31*3530Srb144127 #include <poll.h> 32*3530Srb144127 #include <string.h> 33*3530Srb144127 #include <unistd.h> 34*3530Srb144127 #include <errno.h> 35*3530Srb144127 36*3530Srb144127 #include "sys/ds_pri.h" 37*3530Srb144127 #include "pri.h" 38*3530Srb144127 39*3530Srb144127 /* 40*3530Srb144127 * Library init function - currently no-op. 41*3530Srb144127 * Returns: Success (0), Failure (-1) 42*3530Srb144127 */ 43*3530Srb144127 int 44*3530Srb144127 pri_init(void) 45*3530Srb144127 { 46*3530Srb144127 return (0); 47*3530Srb144127 } 48*3530Srb144127 49*3530Srb144127 /* 50*3530Srb144127 * Library fini function - currently no-op. 51*3530Srb144127 * Returns: N/A 52*3530Srb144127 */ 53*3530Srb144127 void 54*3530Srb144127 pri_fini(void) 55*3530Srb144127 { 56*3530Srb144127 } 57*3530Srb144127 58*3530Srb144127 /* 59*3530Srb144127 * PRI retrieval function. 60*3530Srb144127 * Description: 61*3530Srb144127 * - Library routine to retrieve the Physical Resource Inventory (PRI) 62*3530Srb144127 * - Utilized by sun4v platforms which support Logical Domains 63*3530Srb144127 * - Interacts with the ds_pri pseudo driver to retrieve the 64*3530Srb144127 * PRI. ds_pri driver in turn gets the PRI from the 65*3530Srb144127 * Domain Services kernel module. Domain Services gets the 66*3530Srb144127 * PRI from the Service Processor via LDC (Logical Domain 67*3530Srb144127 * Channel). 68*3530Srb144127 * - Consumers of this api include FMA, Zeus, and picld 69*3530Srb144127 * - MT-Safe, Stateless 70*3530Srb144127 * 71*3530Srb144127 * Imports: 72*3530Srb144127 * - ds_pri driver interfaces 73*3530Srb144127 * 74*3530Srb144127 * Arguments: 75*3530Srb144127 * - wait: specifies whether caller wants to wait for a new PRI, 76*3530Srb144127 * PRI_GET is no-wait, PRI_WAITGET is wait-forever 77*3530Srb144127 * - token: opaque PRI token, accepted from and/or returned to caller, 78*3530Srb144127 * see write-only or read-write semantics below 79*3530Srb144127 * - buf: PRI buffer received from ds_pri driver, returned to caller 80*3530Srb144127 * - allocp: caller provided pointer to memory allocator function 81*3530Srb144127 * - freep: caller provided pointer to memory free function 82*3530Srb144127 * 83*3530Srb144127 * Calling Semantics: 84*3530Srb144127 * - PRI_GET call ignores the token passed in, and returns 85*3530Srb144127 * immediately with current PRI and its token (if any) 86*3530Srb144127 * - PRI_WAITGET call returns only upon the receipt of a new PRI 87*3530Srb144127 * whose token differs from the token passed in by the caller; 88*3530Srb144127 * the passed in token should come from a previous pri_get() 89*3530Srb144127 * call with return value >= 0; the new PRI buffer and its token 90*3530Srb144127 * are returned to the caller 91*3530Srb144127 * - If wait time must be bounded, the caller can spawn a thread 92*3530Srb144127 * which makes a PRI_WAITGET call; caller can choose to kill the 93*3530Srb144127 * spawned thread after a finite time 94*3530Srb144127 * 95*3530Srb144127 * Usage Semantics: 96*3530Srb144127 * - Caller can use the returned PRI buffer as an argument to 97*3530Srb144127 * to md_init_intern() to process it into a machine 98*3530Srb144127 * descriptor (md_t) format 99*3530Srb144127 * - Caller can choose to supply the same allocator and free 100*3530Srb144127 * functions to the md_init_intern() call 101*3530Srb144127 * - Once the caller is done using these data structures, 102*3530Srb144127 * the following actions need to be performed by the caller: 103*3530Srb144127 * - md_fini(mdp) if called md_init_intern() 104*3530Srb144127 * - freep(bufp, size) 105*3530Srb144127 * 106*3530Srb144127 * Returns: 107*3530Srb144127 * >0 if PRI is returned successfully (size of PRI buffer) 108*3530Srb144127 * 0 if no PRI is available 109*3530Srb144127 * -1 if there is an error (errno contains the error code) 110*3530Srb144127 * 111*3530Srb144127 */ 112*3530Srb144127 ssize_t 113*3530Srb144127 pri_get(uint8_t wait, uint64_t *token, uint64_t **buf, 114*3530Srb144127 void *(*allocp)(size_t), void (*freep)(void *, size_t)) 115*3530Srb144127 { 116*3530Srb144127 int fd; /* for device open */ 117*3530Srb144127 uint64_t *bufp; /* buf holding PRI */ 118*3530Srb144127 size_t size; /* sizeof PRI */ 119*3530Srb144127 struct dspri_info pri_info; /* info about PRI */ 120*3530Srb144127 struct dspri_info pri_info2; /* for PRI delta check */ 121*3530Srb144127 122*3530Srb144127 if ((fd = open(DS_PRI_DRIVER, O_RDONLY)) < 0) 123*3530Srb144127 return (-1); 124*3530Srb144127 125*3530Srb144127 if (wait == PRI_WAITGET) { 126*3530Srb144127 /* wait until have new PRI with different token */ 127*3530Srb144127 if (ioctl(fd, DSPRI_WAIT, token) < 0) { 128*3530Srb144127 (void) close(fd); 129*3530Srb144127 return (-1); 130*3530Srb144127 } 131*3530Srb144127 } 132*3530Srb144127 133*3530Srb144127 do { 134*3530Srb144127 /* get info on current PRI */ 135*3530Srb144127 if (ioctl(fd, DSPRI_GETINFO, &pri_info) < 0) { 136*3530Srb144127 (void) close(fd); 137*3530Srb144127 return (-1); 138*3530Srb144127 } 139*3530Srb144127 140*3530Srb144127 size = (size_t)pri_info.size; 141*3530Srb144127 142*3530Srb144127 /* check to see if no PRI available yet */ 143*3530Srb144127 if (size == 0) { 144*3530Srb144127 *token = pri_info.token; 145*3530Srb144127 (void) close(fd); 146*3530Srb144127 return (0); 147*3530Srb144127 } 148*3530Srb144127 149*3530Srb144127 /* allocate a buffer and read the PRI into it */ 150*3530Srb144127 if ((bufp = (uint64_t *)allocp(size)) == NULL) { 151*3530Srb144127 (void) close(fd); 152*3530Srb144127 return (-1); 153*3530Srb144127 } 154*3530Srb144127 if (read(fd, bufp, size) < 0) { 155*3530Srb144127 freep(bufp, size); 156*3530Srb144127 (void) close(fd); 157*3530Srb144127 return (-1); 158*3530Srb144127 } 159*3530Srb144127 160*3530Srb144127 /* 161*3530Srb144127 * Check whether PRI token changed between the time 162*3530Srb144127 * we did the DSPRI_GETINFO ioctl() and the actual 163*3530Srb144127 * read() from the ds_pri driver. The token delta check 164*3530Srb144127 * tries to catch the above race condition; be sure 165*3530Srb144127 * to not leak memory on retries. 166*3530Srb144127 */ 167*3530Srb144127 if (ioctl(fd, DSPRI_GETINFO, &pri_info2) < 0) { 168*3530Srb144127 freep(bufp, size); 169*3530Srb144127 (void) close(fd); 170*3530Srb144127 return (-1); 171*3530Srb144127 } 172*3530Srb144127 if (pri_info2.token != pri_info.token) 173*3530Srb144127 freep(bufp, size); 174*3530Srb144127 175*3530Srb144127 } while (pri_info2.token != pri_info.token); 176*3530Srb144127 177*3530Srb144127 /* return the PRI, its token, and its size to the caller */ 178*3530Srb144127 *buf = bufp; 179*3530Srb144127 *token = pri_info.token; 180*3530Srb144127 (void) close(fd); 181*3530Srb144127 return ((ssize_t)size); 182*3530Srb144127 } 183