All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Cédric Le Goater" <clg@fr.ibm.com>
To: Greg Kurz <gkurz@linux.vnet.ibm.com>
Cc: Corey Minyard <cminyard@mvista.com>,
	qemu-devel@nongnu.org,
	Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,
	"Michael S. Tsirkin" <mst@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v2 4/9] ipmi: introduce a struct ipmi_sdr_compact
Date: Fri, 22 Jan 2016 12:10:46 +0100	[thread overview]
Message-ID: <56A20E36.8000208@fr.ibm.com> (raw)
In-Reply-To: <20160122114932.4d76fb7d@bahia.huguette.org>

On 01/22/2016 11:49 AM, Greg Kurz wrote:
> On Thu, 21 Jan 2016 18:18:49 +0100
> Cédric Le Goater <clg@fr.ibm.com> wrote:
> 
>> Currently, sdr attributes are identified using byte offsets and this
>> can be a bit confusing.
>>
>> This patch adds a struct ipmi_sdr_compact conforming to the IPMI specs
>> and replaces byte offsets with names. It also introduces and uses a
>> struct ipmi_sdr_header in sections of the code where no assumption is
>> made on the type of SDR. This leave rooms to potential usage of other
>> types in the future.
>>
> 
> Turning all these magic numbers into understandable names is definitely a
> great idea !
> 
> See comments below.
> 
>> Signed-off-by: Cédric Le Goater <clg@fr.ibm.com>
>> ---
>>  hw/ipmi/ipmi_bmc_sim.c | 65 +++++++++++++++++++++++++++++++-------------------
>>  include/hw/ipmi/ipmi.h | 44 ++++++++++++++++++++++++++++++++++
>>  2 files changed, 84 insertions(+), 25 deletions(-)
>>
>> diff --git a/hw/ipmi/ipmi_bmc_sim.c b/hw/ipmi/ipmi_bmc_sim.c
>> index fc596a548df7..31f990199154 100644
>> --- a/hw/ipmi/ipmi_bmc_sim.c
>> +++ b/hw/ipmi/ipmi_bmc_sim.c
>> @@ -323,11 +323,15 @@ static void sdr_inc_reservation(IPMISdr *sdr)
>>  static int sdr_add_entry(IPMIBmcSim *ibs, const uint8_t *entry,
>>                           unsigned int len, uint16_t *recid)
>>  {
>> +    struct ipmi_sdr_header *sdrh_entry = (struct ipmi_sdr_header *) entry;
> 
> This looks like the entry argument should be struct ipmi_sdr_header * and
> you would not need sdrh_entry.

Indeed and it improves readability a little more. I will send a fix in next
patchset. 

>> +    struct ipmi_sdr_header *sdrh =
>> +        (struct ipmi_sdr_header *) &ibs->sdr.sdr[ibs->sdr.next_free];
>> +
>>      if ((len < 5) || (len > 255)) {
>>          return 1;
>>      }
>>
>> -    if (entry[4] != len - 5) {
>> +    if (sdrh_entry->rec_length != len - 5) {
>>          return 1;
>>      }
>>
>> @@ -336,10 +340,10 @@ static int sdr_add_entry(IPMIBmcSim *ibs, const uint8_t *entry,
>>          return 1;
>>      }
>>
>> -    memcpy(ibs->sdr.sdr + ibs->sdr.next_free, entry, len);
>> -    ibs->sdr.sdr[ibs->sdr.next_free] = ibs->sdr.next_rec_id & 0xff;
>> -    ibs->sdr.sdr[ibs->sdr.next_free+1] = (ibs->sdr.next_rec_id >> 8) & 0xff;
>> -    ibs->sdr.sdr[ibs->sdr.next_free+2] = 0x51; /* Conform to IPMI 1.5 spec */
>> +    memcpy(sdrh, entry, len);
>> +    sdrh->rec_id[0] = ibs->sdr.next_rec_id & 0xff;
>> +    sdrh->rec_id[1] = (ibs->sdr.next_rec_id >> 8) & 0xff;
>> +    sdrh->sdr_version = 0x51; /* Conform to IPMI 1.5 spec */
>>
>>      if (recid) {
>>          *recid = ibs->sdr.next_rec_id;
>> @@ -357,8 +361,10 @@ static int sdr_find_entry(IPMISdr *sdr, uint16_t recid,
>>      unsigned int pos = *retpos;
>>
>>      while (pos < sdr->next_free) {
>> -        uint16_t trec = sdr->sdr[pos] | (sdr->sdr[pos + 1] << 8);
>> -        unsigned int nextpos = pos + sdr->sdr[pos + 4];
>> +        struct ipmi_sdr_header *sdrh =
>> +            (struct ipmi_sdr_header *) &sdr->sdr[pos];
>> +        uint16_t trec = ipmi_sdr_recid(sdrh);
>> +        unsigned int nextpos = pos + sdrh->rec_length;
>>
>>          if (trec == recid) {
>>              if (nextrec) {
>> @@ -507,29 +513,32 @@ static void ipmi_init_sensors_from_sdrs(IPMIBmcSim *s)
>>
>>      pos = 0;
>>      for (i = 0; !sdr_find_entry(&s->sdr, i, &pos, NULL); i++) {
>> -        uint8_t *sdr = s->sdr.sdr + pos;
>> -        unsigned int len = sdr[4];
>> +        struct ipmi_sdr_compact *sdr =
>> +            (struct ipmi_sdr_compact *) &s->sdr.sdr[pos];
>> +        unsigned int len = sdr->header.rec_length;
>>
>>          if (len < 20) {
>>              continue;
>>          }
>> -        if ((sdr[3] < 1) || (sdr[3] > 2)) {
>> +        if (sdr->header.rec_type != IPMI_SDR_COMPACT_TYPE) {
>>              continue; /* Not a sensor SDR we set from */
>>          }
>>
>> -        if (sdr[7] > MAX_SENSORS) {
>> +        if (sdr->sensor_owner_number > MAX_SENSORS) {
>>              continue;
>>          }
>> -        sens = s->sensors + sdr[7];
>> +        sens = s->sensors + sdr->sensor_owner_number;
>>
>>          IPMI_SENSOR_SET_PRESENT(sens, 1);
>> -        IPMI_SENSOR_SET_SCAN_ON(sens, (sdr[10] >> 6) & 1);
>> -        IPMI_SENSOR_SET_EVENTS_ON(sens, (sdr[10] >> 5) & 1);
>> -        sens->assert_suppt = sdr[14] | (sdr[15] << 8);
>> -        sens->deassert_suppt = sdr[16] | (sdr[17] << 8);
>> -        sens->states_suppt = sdr[18] | (sdr[19] << 8);
>> -        sens->sensor_type = sdr[12];
>> -        sens->evt_reading_type_code = sdr[13] & 0x7f;
>> +        IPMI_SENSOR_SET_SCAN_ON(sens, (sdr->sensor_init >> 6) & 1);
>> +        IPMI_SENSOR_SET_EVENTS_ON(sens, (sdr->sensor_init >> 5) & 1);
>> +        sens->assert_suppt = sdr->assert_mask[0] | (sdr->assert_mask[1] << 8);
>> +        sens->deassert_suppt =
>> +            sdr->deassert_mask[0] | (sdr->deassert_mask[1] << 8);
>> +        sens->states_suppt =
>> +            sdr->discrete_mask[0] | (sdr->discrete_mask[1] << 8);
>> +        sens->sensor_type = sdr->sensor_type;
>> +        sens->evt_reading_type_code = sdr->reading_type & 0x7f;
>>
>>          /* Enable all the events that are supported. */
>>          sens->assert_enable = sens->assert_suppt;
>> @@ -1155,6 +1164,7 @@ static void get_sdr(IPMIBmcSim *ibs,
>>  {
>>      unsigned int pos;
>>      uint16_t nextrec;
>> +    struct ipmi_sdr_header *sdrh;
>>
>>      IPMI_CHECK_CMD_LEN(8);
>>      if (cmd[6]) {
>> @@ -1166,7 +1176,10 @@ static void get_sdr(IPMIBmcSim *ibs,
>>          rsp[2] = IPMI_CC_REQ_ENTRY_NOT_PRESENT;
>>          return;
>>      }
>> -    if (cmd[6] > (ibs->sdr.sdr[pos + 4])) {
>> +
>> +    sdrh = (struct ipmi_sdr_header *) &ibs->sdr.sdr[pos];
>> +
>> +    if (cmd[6] > sdrh->rec_length) {
>>          rsp[2] = IPMI_CC_PARM_OUT_OF_RANGE;
>>          return;
>>      }
>> @@ -1175,7 +1188,7 @@ static void get_sdr(IPMIBmcSim *ibs,
>>      IPMI_ADD_RSP_DATA((nextrec >> 8) & 0xff);
>>
>>      if (cmd[7] == 0xff) {
>> -        cmd[7] = ibs->sdr.sdr[pos + 4] - cmd[6];
>> +        cmd[7] = sdrh->rec_length - cmd[6];
>>      }
>>
>>      if ((cmd[7] + *rsp_len) > max_rsp_len) {
>> @@ -1644,18 +1657,20 @@ static void ipmi_sim_init(Object *obj)
>>      }
>>
>>      for (i = 0;;) {
>> +        struct ipmi_sdr_header *sdrh;
>>          int len;
>>          if ((i + 5) > sizeof(init_sdrs)) {
>> -            error_report("Problem with recid 0x%4.4x: \n", i);
>> +            error_report("Problem with recid 0x%4.4x", i);
> 
> Unrelated change.

OK. I will pull these changes in a preliminary patch.

>>              return;
>>          }
>> -        len = init_sdrs[i + 4];
>> -        recid = init_sdrs[i] | (init_sdrs[i + 1] << 8);
>> +        sdrh = (struct ipmi_sdr_header *) &init_sdrs[i];
> 
> Maybe init_sdrs could be turned into something which already has explicit
> struct ipmi_sdr_header fields ?

I don't necessarily agree here but may be, the name is confusing 
and should be something like 'sdr_blob', which will make more sense
when we load sdrs from a file. 

init_sdrs contains raw data and this routine loops over it to find 
enough bytes for a minimal ipmi sdr header, and then enough bytes 
to add a sdr entry in the BMC table. So we are manipulating bytes
all the time until the end when we :

	sdr_add_entry(ibs, init_sdrs + i, len, NULL);

Your suggestion on changing sdr_add_entry() API improves the code 
a little.

Thanks !

C.


> 
>> +        len = sdrh->rec_length;
>> +        recid = ipmi_sdr_recid(sdrh);
>>          if (recid == 0xffff) {
>>              break;
>>          }
>>          if ((i + len + 5) > sizeof(init_sdrs)) {
>> -            error_report("Problem with recid 0x%4.4x\n", i);
>> +            error_report("Problem with recid 0x%4.4x", i);
> 
> Unrelated change.
> 
>>              return;
>>          }
>>          sdr_add_entry(ibs, init_sdrs + i, len, NULL);
>> diff --git a/include/hw/ipmi/ipmi.h b/include/hw/ipmi/ipmi.h
>> index 32bac0fa8877..7e142e241dcb 100644
>> --- a/include/hw/ipmi/ipmi.h
>> +++ b/include/hw/ipmi/ipmi.h
>> @@ -210,4 +210,48 @@ IPMIFwInfo *ipmi_next_fwinfo(IPMIFwInfo *current);
>>  #define ipmi_debug(fs, ...)
>>  #endif
>>
>> +struct ipmi_sdr_header {
>> +    uint8_t  rec_id[2];
>> +    uint8_t  sdr_version;               /* 0x51 */
>> +    uint8_t  rec_type;
>> +    uint8_t  rec_length;
>> +};
>> +#define IPMI_SDR_HEADER_SIZE     sizeof(struct ipmi_sdr_header)
>> +
>> +#define ipmi_sdr_recid(sdr) ((sdr)->rec_id[0] | ((sdr)->rec_id[1] << 8))
>> +
>> +/*
>> + * 43.2 SDR Type 02h. Compact Sensor Record
>> + */
>> +#define IPMI_SDR_COMPACT_TYPE    2
>> +
>> +struct ipmi_sdr_compact {
>> +    struct ipmi_sdr_header header;
>> +
>> +    uint8_t  sensor_owner_id;
>> +    uint8_t  sensor_owner_lun;
>> +    uint8_t  sensor_owner_number;       /* byte 8 */
>> +    uint8_t  entity_id;
>> +    uint8_t  entity_instance;
>> +    uint8_t  sensor_init;
>> +    uint8_t  sensor_caps;
>> +    uint8_t  sensor_type;
>> +    uint8_t  reading_type;
>> +    uint8_t  assert_mask[2];            /* byte 16 */
>> +    uint8_t  deassert_mask[2];
>> +    uint8_t  discrete_mask[2];
>> +    uint8_t  sensor_unit1;
>> +    uint8_t  sensor_unit2;
>> +    uint8_t  sensor_unit3;
>> +    uint8_t  sensor_direction[2];       /* byte 24 */
>> +    uint8_t  positive_threshold;
>> +    uint8_t  negative_threshold;
>> +    uint8_t  reserved[3];
>> +    uint8_t  oem;
>> +    uint8_t  id_str_len;                /* byte 32 */
>> +    uint8_t  id_string[16];
>> +};
>> +
>> +typedef uint8_t ipmi_sdr_compact_buffer[sizeof(struct ipmi_sdr_compact)];
>> +
>>  #endif
> 

  reply	other threads:[~2016-01-22 11:11 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-21 17:18 [Qemu-devel] [PATCH v2 0/9] ipmi: a couple of enhancements to the BMC simulator Cédric Le Goater
2016-01-21 17:18 ` [Qemu-devel] [PATCH v2 1/9] ppc: add IPMI support Cédric Le Goater
2016-01-21 17:18 ` [Qemu-devel] [PATCH v2 2/9] ipmi: replace goto by a return statement Cédric Le Goater
2016-01-22  5:49   ` Marcel Apfelbaum
2016-01-22  6:28   ` Greg Kurz
2016-01-22 12:56   ` Corey Minyard
2016-01-21 17:18 ` [Qemu-devel] [PATCH v2 3/9] ipmi: replace *_MAXCMD defines Cédric Le Goater
2016-01-22  8:05   ` Greg Kurz
2016-01-21 17:18 ` [Qemu-devel] [PATCH v2 4/9] ipmi: introduce a struct ipmi_sdr_compact Cédric Le Goater
2016-01-22 10:49   ` Greg Kurz
2016-01-22 11:10     ` Cédric Le Goater [this message]
2016-01-21 17:18 ` [Qemu-devel] [PATCH v2 5/9] ipmi: fix SDR length value Cédric Le Goater
2016-01-22 10:56   ` Greg Kurz
2016-01-22 11:15     ` Cédric Le Goater
2016-01-21 17:18 ` [Qemu-devel] [PATCH v2 6/9] ipmi: add get and set SENSOR_TYPE commands Cédric Le Goater
2016-01-22 11:07   ` Greg Kurz
2016-01-22 11:13     ` Cédric Le Goater
2016-01-21 17:18 ` [Qemu-devel] [PATCH v2 7/9] ipmi: add GET_SYS_RESTART_CAUSE chassis command Cédric Le Goater
2016-01-22 11:09   ` Greg Kurz
2016-01-21 17:18 ` [Qemu-devel] [PATCH v2 8/9] ipmi: add ACPI power and GUID commands Cédric Le Goater
2016-01-22 11:24   ` Greg Kurz
2016-01-22 11:58     ` Cédric Le Goater
2016-01-22 13:04   ` Corey Minyard
2016-01-21 17:18 ` [Qemu-devel] [PATCH v2 9/9] ipmi: add SET_SENSOR_READING command (tentative try) Cédric Le Goater
2016-01-22 11:28   ` Greg Kurz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56A20E36.8000208@fr.ibm.com \
    --to=clg@fr.ibm.com \
    --cc=cminyard@mvista.com \
    --cc=gkurz@linux.vnet.ibm.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.