Emulex Logo
OneCoreā„¢ Storage SDK Release 11.2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
elxu_bsd.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015, Emulex
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include <stdio.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/sysctl.h>
40 #include <sys/ioctl.h>
41 #include <sys/pciio.h>
42 #include <fcntl.h>
43 #include <dirent.h>
44 #include <unistd.h>
45 
46 #include <elxu_device.h>
47 #include <elxu_sli4.h>
48 #include "elxu_common.h"
49 
50 /*
51  * This file contains functions specific to BSD.
52  * Any non-static functions here should:
53  * - be declared in elxu_common.h
54  * - have an equivalent in elxu_linux.c, even if it's a stub
55  */
56 
57 #define OCS_DEV_TEMPLATE "/dev/ocs"
58 
59 typedef struct elxu_bsd_command_s {
60  uint32_t magic;
61  uint32_t size; // size of this structure
62  uint8_t payload[256];
63  uint64_t in_addr; // user space address of input buffer
64  uint64_t in_bytes;
65  uint64_t out_addr; // user space address of output buffer
66  uint64_t out_bytes;
68 
69 #define ELXU_BSD_COMMAND _IOWR('Q', 0, struct elxu_bsd_command_s)
70 
72 {
73  struct pci_conf_io pc;
74  struct pci_conf conf[1024];
75  int fd;
76  int i;
77  int index;
78  elxu_device_t *new_device;
79  elxu_device_t *list;
80 
81  fd = open("/dev/pci", O_RDONLY);
82 
83  if (fd < 0) {
84  return NULL;
85  }
86 
87  bzero(&pc, sizeof(struct pci_conf_io));
88  pc.match_buf_len = sizeof(conf);
89  pc.matches = conf;
90 
91  list = NULL;
92  index = 0;
93 
94  ioctl(fd, PCIOCGETCONF, &pc);
95  for (i=0; i<pc.num_matches; i++) {
96  if (conf[i].pc_vendor != PCI_VENDOR_ID_EMULEX) {
97  continue;
98  }
99  /* we support only devices claimed by ocs_fc driver */
100  if (!((strcmp(conf[i].pd_name, "ocs_fc_ramd") == 0) ||
101  (strcmp(conf[i].pd_name, "ocs_fc_cam") == 0) ||
102  (strcmp(conf[i].pd_name, "ocs_fc") == 0))) {
103  continue;
104  }
105 
106  new_device = malloc(sizeof(elxu_device_t));
107  bzero(new_device, sizeof(*new_device));
108  /* Should check for malloc failure here */
109  new_device->next = list;
110  list = new_device;
111  new_device->vendor = conf[i].pc_vendor;
112  new_device->device = conf[i].pc_device;
113  new_device->driver = DEVICE_DRIVER_OCS;
114  new_device->deviceIndex = index++;
115  }
116 
117  close(fd);
118 
119  return list;
120 
121 }
122 
124 {
125  /* User space driver not supported on BSD */
126  return NULL;
127 }
128 
129 /**
130  * @brief Open a device file.
131  *
132  * @par Description
133  * This function opens a device file on BSD.
134  * The input is an elxu_device_name_t which contains a device
135  * index and a hostname. If the hostname is non-NULL then the
136  * request is for a userspace driver, and the hostname is the
137  * host one which the driver is running. This is not supported
138  * on BSD. If the hostname is NULL then the index field
139  * specifies which device file to open.
140  *
141  *
142  * @param devname An elxu_device_name_t structure that specifies
143  * the device to be opened.
144  *
145  * @return Returns a pointer to an elxu_device_t on success;
146  * NULL on failure.
147  */
149 {
150  elxu_device_t *device = NULL;
151  char path[FILENAME_MAX];
152 
153  if (devname->hostname) {
154  printf("%s: user space driver not supported for FreeBSD\n", __func__);
155  return NULL;
156  }
157 
158  snprintf(path, sizeof(path), "%s%d", OCS_DEV_TEMPLATE, devname->devidx);
159 
160  device = elxu_device_by_index(devname->devidx);
161 
162  if (device == NULL) {
163  return NULL;
164  }
165 
166  device->fd = open(path, O_RDWR);
167  if (device->fd < 0) {
168  return NULL;
169  }
170 
171  strcpy(device->device_file_name, path);
172 
173  return device;
174 }
175 
176 /**
177  * @brief Close a device file.
178  *
179  * @par Description
180  * This function closes an open device file.
181  *
182  *
183  * @param device A pointer to an elxu_device_t to be closed.
184  *
185  * @return Returns nothing.
186  */
188 {
189  close(device->fd);
190 }
191 
192 
194 {
195  //Driver owns the device in FreeBSD.
196  return 0;
197 }
198 
199 /**
200  * @brief Send an ioctl to an open device.
201  *
202  * @par Description
203  * This function sends an ioctl to an open device.
204  *
205  * @param device A pointer to the device.
206  * @param req The request to be sent to the device.
207  * @param arg The argument to be sent to the device.
208  *
209  * @return Returns 0 on success; non-zero on failure.
210  */
211 int os_ioctl_device(elxu_device_t *device, int req, void *arg)
212 {
213  unsigned long ul_req = (unsigned long)req & 0xffffffff;
214 
215  return ioctl(device->fd, ul_req, arg);
216 }
217 
218 /**
219  * @brief Print iSCSI connection info for the given device.
220  *
221  * @par Description
222  * This function prints iSCSI connection information for the
223  * given device. BSD does not support iSCSI connection info, so
224  * this function prints an error message and returns.
225  *
226  * @param device The device for which to print connection info.
227  *
228  * @return Returns nothing.
229  */
231 {
232  printf("iSCSI connection info is not supported on BSD\n");
233 }
234 
235 
236 
237 
238 /* vim: set expandtab tabstop=4 shiftwidth=4: */
uint32_t magic
Definition: elxu_bsd.c:60
uint64_t in_addr
Definition: elxu_bsd.c:63
elxu_device_t * os_build_device_list(char *path)
Definition: elxu_bsd.c:71
#define PCI_VENDOR_ID_EMULEX
Definition: elxu_device.h:39
uint16_t device
Definition: elxu_device.h:82
void os_close_device(elxu_device_t *device)
Close a device file.
Definition: elxu_bsd.c:187
int os_ioctl_device(elxu_device_t *device, int req, void *arg)
Send an ioctl to an open device.
Definition: elxu_bsd.c:211
uint16_t vendor
Definition: elxu_device.h:81
struct elxu_device_struct * next
Definition: elxu_device.h:79
device name specifier This structure is used to describe the type and name of the device ioctl connec...
Definition: elxu_device.h:113
uint64_t out_bytes
Definition: elxu_bsd.c:66
uint32_t size
Definition: elxu_bsd.c:61
uint32_t deviceIndex
Definition: elxu_device.h:85
void elxu_get_connection_info(elxu_device_t *device)
Print iSCSI connection info for the given device.
Definition: elxu_bsd.c:230
elxu_device_t * elxu_device_by_index(int index)
Return a pointer to a device given an index.
Definition: elxu_device.c:730
elxu_device_t * os_open_device(elxu_device_name_t *devname)
Open a device file.
Definition: elxu_bsd.c:148
uint64_t in_bytes
Definition: elxu_bsd.c:64
elxu_device_t * os_build_device_list_uspace(char *hostname)
Definition: elxu_bsd.c:123
elxu_device_driver_e driver
Definition: elxu_device.h:90
uint64_t out_addr
Definition: elxu_bsd.c:65
char device_file_name[1024]
Definition: elxu_device.h:102
#define OCS_DEV_TEMPLATE
Definition: elxu_bsd.c:57
int os_remove_device(elxu_device_t *device)
Definition: elxu_bsd.c:193