Switchtec Userspace  PROJECT_NUMBER = PROJECT_NUMBER=PROJECT_NUMBER = 2.2
platform.c
Go to the documentation of this file.
1 /*
2  * Microsemi Switchtec(tm) PCIe Management Library
3  * Copyright (c) 2017, Microsemi Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
30 #include "../switchtec_priv.h"
31 #include "switchtec/switchtec.h"
32 #include "switchtec/gas.h"
33 #include "switchtec/gas_mrpc.h"
34 #include "switchtec/errors.h"
35 
36 #include <errno.h>
37 
44 struct switchtec_dev *switchtec_open_by_path(const char *path);
45 
55 struct switchtec_dev *switchtec_open_by_index(int index);
56 
66 struct switchtec_dev *switchtec_open_by_pci_addr(int domain, int bus,
67  int device, int func);
68 
76 struct switchtec_dev *switchtec_open_i2c(const char *path, int i2c_addr);
77 
84 struct switchtec_dev *switchtec_open_uart(int fd);
85 
91 void switchtec_close(struct switchtec_dev *dev)
92 {
93  if (!dev)
94  return;
95 
96  dev->ops->close(dev);
97 }
98 
106 int switchtec_list(struct switchtec_device_info **devlist);
107 
115 int switchtec_get_fw_version(struct switchtec_dev *dev, char *buf,
116  size_t buflen)
117 {
118  return dev->ops->get_fw_version(dev, buf, buflen);
119 }
120 
132 int switchtec_cmd(struct switchtec_dev *dev, uint32_t cmd,
133  const void *payload, size_t payload_len, void *resp,
134  size_t resp_len)
135 {
136  int ret;
137 
138  cmd &= SWITCHTEC_CMD_MASK;
139  cmd |= dev->pax_id << SWITCHTEC_PAX_ID_SHIFT;
140 
141  ret = dev->ops->cmd(dev, cmd, payload, payload_len, resp, resp_len);
142  if (ret > 0) {
143  mrpc_error_cmd = cmd & SWITCHTEC_CMD_MASK;
144  errno |= SWITCHTEC_ERRNO_MRPC_FLAG_BIT;
145  }
146 
147  return ret;
148 }
149 
162 int switchtec_get_devices(struct switchtec_dev *dev,
163  struct switchtec_status *status,
164  int ports)
165 {
166  if (!dev->ops->get_devices)
167  return 0;
168 
169  return dev->ops->get_devices(dev, status, ports);
170 }
171 
181 int switchtec_pff_to_port(struct switchtec_dev *dev, int pff,
182  int *partition, int *port)
183 {
184  return dev->ops->pff_to_port(dev, pff, partition, port);
185 }
186 
196 int switchtec_port_to_pff(struct switchtec_dev *dev, int partition,
197  int port, int *pff)
198 {
199  return dev->ops->port_to_pff(dev, partition, port, pff);
200 }
201 
223 gasptr_t switchtec_gas_map(struct switchtec_dev *dev, int writeable,
224  size_t *map_size)
225 {
226  return dev->ops->gas_map(dev, writeable, map_size);
227 }
228 
235 void switchtec_gas_unmap(struct switchtec_dev *dev, gasptr_t map)
236 {
237  if (!dev->ops->gas_unmap)
238  return;
239 
240  dev->ops->gas_unmap(dev, map);
241 }
242 
251 int switchtec_flash_part(struct switchtec_dev *dev,
252  struct switchtec_fw_image_info *info,
253  enum switchtec_fw_image_part_id_gen3 part)
254 {
255  return dev->ops->flash_part(dev, info, part);
256 }
257 
265 int switchtec_event_summary(struct switchtec_dev *dev,
266  struct switchtec_event_summary *sum)
267 {
268  return dev->ops->event_summary(dev, sum);
269 }
270 
281 int switchtec_event_ctl(struct switchtec_dev *dev,
282  enum switchtec_event_id e,
283  int index, int flags,
284  uint32_t data[5])
285 {
286  return dev->ops->event_ctl(dev, e, index, flags, data);
287 }
288 
297 int switchtec_event_wait(struct switchtec_dev *dev, int timeout_ms)
298 {
299  if (!dev->ops->event_wait) {
300  errno = ENOTSUP;
301  return -errno;
302  }
303 
304  return dev->ops->event_wait(dev, timeout_ms);
305 }
306 
313 uint8_t gas_read8(struct switchtec_dev *dev, uint8_t __gas *addr)
314 {
315  if (dev->pax_id != dev->local_pax_id)
316  return gas_mrpc_read8(dev, addr);
317 
318  return __gas_read8(dev, addr);
319 }
320 
327 uint16_t gas_read16(struct switchtec_dev *dev, uint16_t __gas *addr)
328 {
329  if (dev->pax_id != dev->local_pax_id)
330  return gas_mrpc_read16(dev, addr);
331 
332  return __gas_read16(dev, addr);
333 }
334 
341 uint32_t gas_read32(struct switchtec_dev *dev, uint32_t __gas *addr)
342 {
343  if (dev->pax_id != dev->local_pax_id)
344  return gas_mrpc_read32(dev, addr);
345 
346  return __gas_read32(dev, addr);
347 }
348 
355 uint64_t gas_read64(struct switchtec_dev *dev, uint64_t __gas *addr)
356 {
357  if (dev->pax_id != dev->local_pax_id)
358  return gas_mrpc_read64(dev, addr);
359 
360  return __gas_read64(dev, addr);
361 }
362 
369 void gas_write8(struct switchtec_dev *dev, uint8_t val, uint8_t __gas *addr)
370 {
371  if (dev->pax_id != dev->local_pax_id)
372  gas_mrpc_write8(dev, val, addr);
373 
374  __gas_write8(dev, val, addr);
375 }
376 
383 void gas_write16(struct switchtec_dev *dev, uint16_t val, uint16_t __gas *addr)
384 {
385  if (dev->pax_id != dev->local_pax_id)
386  gas_mrpc_write16(dev, val, addr);
387 
388  __gas_write16(dev, val, addr);
389 }
390 
397 void gas_write32(struct switchtec_dev *dev, uint32_t val, uint32_t __gas *addr)
398 {
399  if (dev->pax_id != dev->local_pax_id)
400  gas_mrpc_write32(dev, val, addr);
401 
402  __gas_write32(dev, val, addr);
403 }
404 
411 void gas_write64(struct switchtec_dev *dev, uint64_t val, uint64_t __gas *addr)
412 {
413  if (dev->pax_id != dev->local_pax_id)
414  gas_mrpc_write64(dev, val, addr);
415 
416  __gas_write64(dev, val, addr);
417 }
418 
426 void memcpy_to_gas(struct switchtec_dev *dev, void __gas *dest,
427  const void *src, size_t n)
428 {
429  if (dev->pax_id != dev->local_pax_id)
430  gas_mrpc_memcpy_to_gas(dev, dest, src, n);
431 
432  __memcpy_to_gas(dev, dest, src, n);
433 }
434 
442 void memcpy_from_gas(struct switchtec_dev *dev, void *dest,
443  const void __gas *src, size_t n)
444 {
445  if (dev->pax_id != dev->local_pax_id)
446  gas_mrpc_memcpy_from_gas(dev, dest, src, n);
447 
448  __memcpy_from_gas(dev, dest, src, n);
449 }
450 
458 ssize_t write_from_gas(struct switchtec_dev *dev, int fd,
459  const void __gas *src, size_t n)
460 {
461  if (dev->pax_id != dev->local_pax_id)
462  gas_mrpc_write_from_gas(dev, fd, src, n);
463 
464  return __write_from_gas(dev, fd, src, n);
465 }
uint32_t gas_read32(struct switchtec_dev *dev, uint32_t __gas *addr)
Read a uint32_t from the GAS.
Definition: platform.c:341
int switchtec_pff_to_port(struct switchtec_dev *dev, int pff, int *partition, int *port)
Convert a port function index to a partition and port number.
Definition: platform.c:181
int switchtec_port_to_pff(struct switchtec_dev *dev, int partition, int port, int *pff)
Convert a partition and port number to a port function index.
Definition: platform.c:196
void gas_mrpc_memcpy_from_gas(struct switchtec_dev *dev, void *dest, const void __gas *src, size_t n)
Copy data from the GAS using MRPC commands.
Definition: gas_mrpc.c:90
void gas_write32(struct switchtec_dev *dev, uint32_t val, uint32_t __gas *addr)
Write a uint32_t to the GAS.
Definition: platform.c:397
void gas_write8(struct switchtec_dev *dev, uint8_t val, uint8_t __gas *addr)
Write a uint8_t to the GAS.
Definition: platform.c:369
gasptr_t switchtec_gas_map(struct switchtec_dev *dev, int writeable, size_t *map_size)
Map the GAS and return a pointer to access the gas.
Definition: platform.c:223
switchtec_event_id
Enumeration of all possible events.
Definition: switchtec.h:265
uint16_t gas_read16(struct switchtec_dev *dev, uint16_t __gas *addr)
Read a uint16_t from the GAS.
Definition: platform.c:327
Information about a firmware image or partition.
Definition: switchtec.h:213
uint64_t gas_read64(struct switchtec_dev *dev, uint64_t __gas *addr)
Read a uint64_t from the GAS.
Definition: platform.c:355
void switchtec_gas_unmap(struct switchtec_dev *dev, gasptr_t map)
Unmap the GAS region mapped with.
Definition: platform.c:235
__gas struct switchtec_gas * gasptr_t
Shortform for a pointer to the GAS register space.
Definition: switchtec.h:77
int switchtec_list(struct switchtec_device_info **devlist)
List all the switchtec devices in the system.
void memcpy_to_gas(struct switchtec_dev *dev, void __gas *dest, const void *src, size_t n)
Copy data to the GAS.
Definition: platform.c:426
int switchtec_get_devices(struct switchtec_dev *dev, struct switchtec_status *status, int ports)
Populate an already retrieved switchtec_status structure list with information about the devices plug...
Definition: platform.c:162
Event summary bitmaps.
Definition: switchtec.h:250
GAS Accessor functions.
Main Switchtec header.
uint8_t gas_read8(struct switchtec_dev *dev, uint8_t __gas *addr)
Read a uint8_t from the GAS.
Definition: platform.c:313
ssize_t gas_mrpc_write_from_gas(struct switchtec_dev *dev, int fd, const void __gas *src, size_t n)
Call write() with data from the GAS using an MRPC command.
Definition: gas_mrpc.c:120
int switchtec_event_wait(struct switchtec_dev *dev, int timeout_ms)
Wait for any event to occur (typically just an interrupt)
Definition: platform.c:297
void gas_write16(struct switchtec_dev *dev, uint16_t val, uint16_t __gas *addr)
Write a uint16_t to the GAS.
Definition: platform.c:383
int switchtec_flash_part(struct switchtec_dev *dev, struct switchtec_fw_image_info *info, enum switchtec_fw_image_part_id_gen3 part)
Retrieve information about a flash partition.
Definition: platform.c:251
Port status structure.
Definition: switchtec.h:153
void gas_mrpc_memcpy_to_gas(struct switchtec_dev *dev, void __gas *dest, const void *src, size_t n)
Copy data to the GAS using MRPC commands.
Definition: gas_mrpc.c:57
int switchtec_get_fw_version(struct switchtec_dev *dev, char *buf, size_t buflen)
Get the firmware version as a user readable string.
Definition: platform.c:115
struct switchtec_dev * switchtec_open_uart(int fd)
Open a switchtec device behind a uart device.
struct switchtec_dev * switchtec_open_by_path(const char *path)
Open a switchtec device by path.
void gas_write64(struct switchtec_dev *dev, uint64_t val, uint64_t __gas *addr)
Write a uint64_t to the GAS.
Definition: platform.c:411
ssize_t write_from_gas(struct switchtec_dev *dev, int fd, const void __gas *src, size_t n)
Call write() with data from the GAS.
Definition: platform.c:458
int mrpc_error_cmd
The MRPC command ID when errno is set.
Definition: switchtec.c:497
int switchtec_cmd(struct switchtec_dev *dev, uint32_t cmd, const void *payload, size_t payload_len, void *resp, size_t resp_len)
Execute an MRPC command.
Definition: platform.c:132
Represents a Switchtec device in the switchtec_list() function.
Definition: switchtec.h:124
struct switchtec_dev * switchtec_open_i2c(const char *path, int i2c_addr)
Open a switchtec device behind an I2C device.
struct switchtec_dev * switchtec_open_by_index(int index)
Open a switchtec device by index.
void memcpy_from_gas(struct switchtec_dev *dev, void *dest, const void __gas *src, size_t n)
Copy data from the GAS.
Definition: platform.c:442
int switchtec_event_ctl(struct switchtec_dev *dev, enum switchtec_event_id e, int index, int flags, uint32_t data[5])
Enable, disable and clear events or retrieve event data.
Definition: platform.c:281
int switchtec_event_summary(struct switchtec_dev *dev, struct switchtec_event_summary *sum)
Retrieve a summary of all the events that have occurred in the switch.
Definition: platform.c:265
struct switchtec_dev * switchtec_open_by_pci_addr(int domain, int bus, int device, int func)
Open a switchtec device by PCI address (BDF)
void switchtec_close(struct switchtec_dev *dev)
Close a Switchtec device handle.
Definition: platform.c:91