xref: /freebsd/sys/dev/ocs_fc/ocs_ioctl.c (revision b1c5f60ce87cc2f179dfb81de507d9b7bf59564c)
1 /*-
2  * Copyright (c) 2017 Broadcom. All rights reserved.
3  * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries.
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  * $FreeBSD$
32  */
33 
34 #include "ocs.h"
35 #include "ocs_utils.h"
36 
37 #include <sys/conf.h>
38 #include <sys/sysctl.h>
39 #include <sys/ioccom.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/linker.h>
43 #include <sys/firmware.h>
44 
45 static d_open_t		ocs_open;
46 static d_close_t	ocs_close;
47 static d_ioctl_t	ocs_ioctl;
48 
49 static struct cdevsw ocs_cdevsw = {
50 	.d_version =	D_VERSION,
51 	.d_open =	ocs_open,
52 	.d_close =	ocs_close,
53 	.d_ioctl =	ocs_ioctl,
54 	.d_name =	"ocs_fc"
55 };
56 
57 int
58 ocs_firmware_write(ocs_t *ocs, const uint8_t *buf, size_t buf_len, uint8_t *change_status);
59 
60 static int
61 ocs_open(struct cdev *cdev, int flags, int fmt, struct thread *td)
62 {
63 	return 0;
64 }
65 
66 static int
67 ocs_close(struct cdev *cdev, int flag, int fmt, struct thread *td)
68 {
69 	return 0;
70 }
71 
72 static int32_t
73 __ocs_ioctl_mbox_cb(ocs_hw_t *hw, int32_t status, uint8_t *mqe, void *arg)
74 {
75 	struct ocs_softc *ocs = arg;
76 
77 	/* wait for the ioctl to sleep before calling wakeup */
78 	mtx_lock(&ocs->dbg_lock);
79 
80 	mtx_unlock(&ocs->dbg_lock);
81 
82 	wakeup(arg);
83 
84 	return 0;
85 }
86 
87 static int
88 ocs_process_sli_config (ocs_t *ocs, ocs_ioctl_elxu_mbox_t *mcmd, ocs_dma_t *dma)
89 {
90 	sli4_cmd_sli_config_t *sli_config = (sli4_cmd_sli_config_t *)mcmd->payload;
91 
92 	if (sli_config->emb) {
93 		sli4_req_hdr_t	*req = (sli4_req_hdr_t *)sli_config->payload.embed;
94 
95 		switch (req->opcode) {
96 		case SLI4_OPC_COMMON_READ_OBJECT:
97 			if (mcmd->out_bytes) {
98 				sli4_req_common_read_object_t *rdobj =
99 					(sli4_req_common_read_object_t *)sli_config->payload.embed;
100 
101 				if (ocs_dma_alloc(ocs, dma, mcmd->out_bytes, 4096)) {
102 					device_printf(ocs->dev, "%s: COMMON_READ_OBJECT - %lld allocation failed\n",
103 							__func__, (unsigned long long)mcmd->out_bytes);
104 					return ENXIO;
105 				}
106 
107 				memset(dma->virt, 0, mcmd->out_bytes);
108 
109 				rdobj->host_buffer_descriptor[0].bde_type = SLI4_BDE_TYPE_BDE_64;
110 				rdobj->host_buffer_descriptor[0].buffer_length = mcmd->out_bytes;
111 				rdobj->host_buffer_descriptor[0].u.data.buffer_address_low = ocs_addr32_lo(dma->phys);
112 				rdobj->host_buffer_descriptor[0].u.data.buffer_address_high = ocs_addr32_hi(dma->phys);
113 			}
114 			break;
115 		case SLI4_OPC_COMMON_WRITE_OBJECT:
116 		{
117 			sli4_req_common_write_object_t *wrobj =
118 				(sli4_req_common_write_object_t *)sli_config->payload.embed;
119 
120 			if (ocs_dma_alloc(ocs, dma, wrobj->desired_write_length, 4096)) {
121 				device_printf(ocs->dev, "%s: COMMON_WRITE_OBJECT - %d allocation failed\n",
122 						__func__, wrobj->desired_write_length);
123 				return ENXIO;
124 			}
125 			/* setup the descriptor */
126 			wrobj->host_buffer_descriptor[0].bde_type = SLI4_BDE_TYPE_BDE_64;
127 			wrobj->host_buffer_descriptor[0].buffer_length = wrobj->desired_write_length;
128 			wrobj->host_buffer_descriptor[0].u.data.buffer_address_low = ocs_addr32_lo(dma->phys);
129 			wrobj->host_buffer_descriptor[0].u.data.buffer_address_high = ocs_addr32_hi(dma->phys);
130 
131 			/* copy the data into the DMA buffer */
132 			copyin((void *)(uintptr_t)mcmd->in_addr, dma->virt, mcmd->in_bytes);
133 		}
134 			break;
135 		case SLI4_OPC_COMMON_DELETE_OBJECT:
136 			break;
137 		case SLI4_OPC_COMMON_READ_OBJECT_LIST:
138 			if (mcmd->out_bytes) {
139 				sli4_req_common_read_object_list_t *rdobj =
140 					(sli4_req_common_read_object_list_t *)sli_config->payload.embed;
141 
142 				if (ocs_dma_alloc(ocs, dma, mcmd->out_bytes, 4096)) {
143 					device_printf(ocs->dev, "%s: COMMON_READ_OBJECT_LIST - %lld allocation failed\n",
144 							__func__,(unsigned long long) mcmd->out_bytes);
145 					return ENXIO;
146 				}
147 
148 				memset(dma->virt, 0, mcmd->out_bytes);
149 
150 				rdobj->host_buffer_descriptor[0].bde_type = SLI4_BDE_TYPE_BDE_64;
151 				rdobj->host_buffer_descriptor[0].buffer_length = mcmd->out_bytes;
152 				rdobj->host_buffer_descriptor[0].u.data.buffer_address_low = ocs_addr32_lo(dma->phys);
153 				rdobj->host_buffer_descriptor[0].u.data.buffer_address_high = ocs_addr32_hi(dma->phys);
154 			}
155 			break;
156 		case SLI4_OPC_COMMON_READ_TRANSCEIVER_DATA:
157 			break;
158 		default:
159 			device_printf(ocs->dev, "%s: in=%p (%lld) out=%p (%lld)\n", __func__,
160 					(void *)(uintptr_t)mcmd->in_addr, (unsigned long long)mcmd->in_bytes,
161 					(void *)(uintptr_t)mcmd->out_addr, (unsigned long long)mcmd->out_bytes);
162 			device_printf(ocs->dev, "%s: unknown (opc=%#x)\n", __func__,
163 					req->opcode);
164 			hexdump(mcmd, mcmd->size, NULL, 0);
165 			break;
166 		}
167 	} else {
168 		uint32_t max_bytes = max(mcmd->in_bytes, mcmd->out_bytes);
169 		if (ocs_dma_alloc(ocs, dma, max_bytes, 4096)) {
170 			device_printf(ocs->dev, "%s: non-embedded - %u allocation failed\n",
171 					__func__, max_bytes);
172 			return ENXIO;
173 		}
174 
175 		copyin((void *)(uintptr_t)mcmd->in_addr, dma->virt, mcmd->in_bytes);
176 
177 		sli_config->payload.mem.address_low  = ocs_addr32_lo(dma->phys);
178 		sli_config->payload.mem.address_high = ocs_addr32_hi(dma->phys);
179 		sli_config->payload.mem.length       = max_bytes;
180 	}
181 
182 	return 0;
183 }
184 
185 static int
186 ocs_process_mbx_ioctl(ocs_t *ocs, ocs_ioctl_elxu_mbox_t *mcmd)
187 {
188 	ocs_dma_t	dma = { 0 };
189 
190 	if ((ELXU_BSD_MAGIC != mcmd->magic) ||
191 			(sizeof(ocs_ioctl_elxu_mbox_t) != mcmd->size)) {
192 		device_printf(ocs->dev, "%s: malformed command m=%08x s=%08x\n",
193 				__func__, mcmd->magic, mcmd->size);
194 		return EINVAL;
195 	}
196 
197 	switch(((sli4_mbox_command_header_t *)mcmd->payload)->command) {
198 	case SLI4_MBOX_COMMAND_SLI_CONFIG:
199 		if (ENXIO == ocs_process_sli_config(ocs, mcmd, &dma))
200 			return ENXIO;
201 		break;
202 
203 	case SLI4_MBOX_COMMAND_READ_REV:
204 	case SLI4_MBOX_COMMAND_READ_STATUS:
205 	case SLI4_MBOX_COMMAND_READ_LNK_STAT:
206 		break;
207 
208 	default:
209 		device_printf(ocs->dev, "command %d\n",((sli4_mbox_command_header_t *)mcmd->payload)->command);
210 		device_printf(ocs->dev, "%s, command not support\n", __func__);
211 		goto no_support;
212 		break;
213 	}
214 
215 	/*
216 	 * The dbg_lock usage here insures the command completion code
217 	 * (__ocs_ioctl_mbox_cb), which calls wakeup(), does not run until
218 	 * after first calling msleep()
219 	 *
220 	 *  1. ioctl grabs dbg_lock
221 	 *  2. ioctl issues command
222 	 *       if the command completes before msleep(), the
223 	 *       command completion code (__ocs_ioctl_mbox_cb) will spin
224 	 *       on dbg_lock before calling wakeup()
225 	 *  3. ioctl calls msleep which releases dbg_lock before sleeping
226 	 *     and reacquires it before waking
227 	 *  4. command completion handler acquires the dbg_lock, immediately
228 	 *     releases it, and calls wakeup
229 	 *  5. msleep returns, re-acquiring the lock
230 	 *  6. ioctl code releases the lock
231 	 */
232 	mtx_lock(&ocs->dbg_lock);
233 	if (ocs_hw_command(&ocs->hw, mcmd->payload, OCS_CMD_NOWAIT,
234 			__ocs_ioctl_mbox_cb, ocs)) {
235 		device_printf(ocs->dev, "%s: command- %x failed\n", __func__,
236 			((sli4_mbox_command_header_t *)mcmd->payload)->command);
237 	}
238 	msleep(ocs, &ocs->dbg_lock, 0, "ocsmbx", 0);
239 	mtx_unlock(&ocs->dbg_lock);
240 
241 	if( SLI4_MBOX_COMMAND_SLI_CONFIG == ((sli4_mbox_command_header_t *)mcmd->payload)->command
242 	  		&& mcmd->out_bytes && dma.virt) {
243 		copyout(dma.virt, (void *)(uintptr_t)mcmd->out_addr, mcmd->out_bytes);
244 	}
245 
246 no_support:
247 	ocs_dma_free(ocs, &dma);
248 
249 	return 0;
250 }
251 
252 /**
253  * @brief perform requested Elx CoreDump helper function
254  *
255  * The Elx CoreDump facility used for BE3 diagnostics uses the OCS_IOCTL_CMD_ECD_HELPER
256  * ioctl function to execute requested "help" functions
257  *
258  * @param ocs pointer to ocs structure
259  * @param req pointer to helper function request
260  *
261  * @return returns 0 for success, a negative error code value for failure.
262  */
263 
264 static int
265 ocs_process_ecd_helper (ocs_t *ocs, ocs_ioctl_ecd_helper_t *req)
266 {
267 	int32_t rc = 0;
268 	uint8_t v8;
269 	uint16_t v16;
270 	uint32_t v32;
271 
272 	/* Check the BAR read/write commands for valid bar */
273 	switch(req->cmd) {
274 	case OCS_ECD_HELPER_BAR_READ8:
275 	case OCS_ECD_HELPER_BAR_READ16:
276 	case OCS_ECD_HELPER_BAR_READ32:
277 	case OCS_ECD_HELPER_BAR_WRITE8:
278 	case OCS_ECD_HELPER_BAR_WRITE16:
279 	case OCS_ECD_HELPER_BAR_WRITE32:
280 		if (req->bar >= PCI_MAX_BAR) {
281 			device_printf(ocs->dev, "Error: bar %d out of range\n", req->bar);
282 			return -EFAULT;
283 		}
284 		if (ocs->reg[req->bar].res == NULL) {
285 			device_printf(ocs->dev, "Error: bar %d not defined\n", req->bar);
286 			return -EFAULT;
287 		}
288 		break;
289 	default:
290 		break;
291 	}
292 
293 	switch(req->cmd) {
294 	case OCS_ECD_HELPER_CFG_READ8:
295 		v8 = ocs_config_read8(ocs, req->offset);
296 		req->data = v8;
297 		break;
298 	case OCS_ECD_HELPER_CFG_READ16:
299 		v16 = ocs_config_read16(ocs, req->offset);
300 		req->data = v16;
301 		break;
302 	case OCS_ECD_HELPER_CFG_READ32:
303 		v32 = ocs_config_read32(ocs, req->offset);
304 		req->data = v32;
305 		break;
306 	case OCS_ECD_HELPER_CFG_WRITE8:
307 		ocs_config_write8(ocs, req->offset, req->data);
308 		break;
309 	case OCS_ECD_HELPER_CFG_WRITE16:
310 		ocs_config_write16(ocs, req->offset, req->data);
311 		break;
312 	case OCS_ECD_HELPER_CFG_WRITE32:
313 		ocs_config_write32(ocs, req->offset, req->data);
314 		break;
315 	case OCS_ECD_HELPER_BAR_READ8:
316 		req->data = ocs_reg_read8(ocs, req->bar, req->offset);
317 		break;
318 	case OCS_ECD_HELPER_BAR_READ16:
319 		req->data = ocs_reg_read16(ocs, req->bar, req->offset);
320 		break;
321 	case OCS_ECD_HELPER_BAR_READ32:
322 		req->data = ocs_reg_read32(ocs, req->bar, req->offset);
323 		break;
324 	case OCS_ECD_HELPER_BAR_WRITE8:
325 		ocs_reg_write8(ocs, req->bar, req->offset, req->data);
326 		break;
327 	case OCS_ECD_HELPER_BAR_WRITE16:
328 		ocs_reg_write16(ocs, req->bar, req->offset, req->data);
329 		break;
330 	case OCS_ECD_HELPER_BAR_WRITE32:
331 		ocs_reg_write32(ocs, req->bar, req->offset, req->data);
332 		break;
333 	default:
334 		device_printf(ocs->dev, "Invalid helper command=%d\n", req->cmd);
335 		break;
336 	}
337 
338 	return rc;
339 }
340 
341 static int
342 ocs_ioctl(struct cdev *cdev, u_long cmd, caddr_t addr, int flag, struct thread *td)
343 {
344 	int status = 0;
345 	struct ocs_softc *ocs = cdev->si_drv1;
346 	device_t dev = ocs->dev;
347 
348 	switch (cmd) {
349 	case OCS_IOCTL_CMD_ELXU_MBOX: {
350 		/* "copyin" done by kernel; thus, just dereference addr */
351 		ocs_ioctl_elxu_mbox_t *mcmd = (void *)addr;
352 		status = ocs_process_mbx_ioctl(ocs, mcmd);
353 		break;
354 	}
355 	case OCS_IOCTL_CMD_ECD_HELPER: {
356 		/* "copyin" done by kernel; thus, just dereference addr */
357 		ocs_ioctl_ecd_helper_t *req = (void *)addr;
358 		status = ocs_process_ecd_helper(ocs, req);
359 		break;
360 	}
361 
362 	case OCS_IOCTL_CMD_VPORT: {
363 		int32_t rc = 0;
364 		ocs_ioctl_vport_t *req = (ocs_ioctl_vport_t*) addr;
365 		ocs_domain_t *domain;
366 
367 		domain = ocs_domain_get_instance(ocs, req->domain_index);
368 		if (domain == NULL) {
369 			device_printf(ocs->dev, "domain [%d] nod found\n",
370 							req->domain_index);
371 			return -EFAULT;
372 		}
373 
374 		if (req->req_create) {
375 			rc = ocs_sport_vport_new(domain, req->wwpn, req->wwnn,
376 						UINT32_MAX, req->enable_ini,
377 					req->enable_tgt, NULL, NULL, TRUE);
378 		} else {
379 			rc = ocs_sport_vport_del(ocs, domain, req->wwpn, req->wwnn);
380 		}
381 
382 		return rc;
383 	}
384 
385 	case OCS_IOCTL_CMD_GET_DDUMP: {
386 		ocs_ioctl_ddump_t *req = (ocs_ioctl_ddump_t*) addr;
387 		ocs_textbuf_t textbuf;
388 		int x;
389 
390 		/* Build a text buffer */
391 		if (ocs_textbuf_alloc(ocs, &textbuf, req->user_buffer_len)) {
392 			device_printf(ocs->dev, "Error: ocs_textbuf_alloc failed\n");
393 			return -EFAULT;
394 		}
395 
396 		switch (req->args.action) {
397 		case OCS_IOCTL_DDUMP_GET:
398 		case OCS_IOCTL_DDUMP_GET_SAVED: {
399 			uint32_t remaining;
400 			uint32_t written;
401 			uint32_t idx;
402 			int32_t n;
403 			ocs_textbuf_t *ptbuf = NULL;
404 			uint32_t flags = 0;
405 
406 			if (req->args.action == OCS_IOCTL_DDUMP_GET_SAVED) {
407 				if (ocs_textbuf_initialized(&ocs->ddump_saved)) {
408 					ptbuf = &ocs->ddump_saved;
409 				}
410 			} else {
411 				if (ocs_textbuf_alloc(ocs, &textbuf, req->user_buffer_len)) {
412 					ocs_log_err(ocs, "Error: ocs_textbuf_alloc failed\n");
413 					return -EFAULT;
414 				}
415 
416 				/* translate IOCTL ddump flags to ddump flags */
417 				if (req->args.flags & OCS_IOCTL_DDUMP_FLAGS_WQES) {
418 					flags |= OCS_DDUMP_FLAGS_WQES;
419 				}
420 				if (req->args.flags & OCS_IOCTL_DDUMP_FLAGS_CQES) {
421 					flags |= OCS_DDUMP_FLAGS_CQES;
422 				}
423 				if (req->args.flags & OCS_IOCTL_DDUMP_FLAGS_MQES) {
424 					flags |= OCS_DDUMP_FLAGS_MQES;
425 				}
426 				if (req->args.flags & OCS_IOCTL_DDUMP_FLAGS_RQES) {
427 					flags |= OCS_DDUMP_FLAGS_RQES;
428 				}
429 				if (req->args.flags & OCS_IOCTL_DDUMP_FLAGS_EQES) {
430 					flags |= OCS_DDUMP_FLAGS_EQES;
431 				}
432 
433 				/* Try 3 times to get the dump */
434 				for(x=0; x<3; x++) {
435 					if (ocs_ddump(ocs, &textbuf, flags, req->args.q_entries) != 0) {
436 						ocs_textbuf_reset(&textbuf);
437 					} else {
438 						/* Success */
439 						x = 0;
440 						break;
441 					}
442 				}
443 				if (x != 0 ) {
444 					/* Retries failed */
445 					ocs_log_test(ocs, "ocs_ddump failed\n");
446 				} else {
447 					ptbuf = &textbuf;
448 				}
449 			}
450 			written = 0;
451 			if (ptbuf != NULL) {
452 				/* Process each textbuf segment */
453 				remaining = req->user_buffer_len;
454 				for (idx = 0; remaining; idx++) {
455 					n = ocs_textbuf_ext_get_written(ptbuf, idx);
456 					if (n < 0) {
457 						break;
458 					}
459 					if ((uint32_t)n >= remaining) {
460 						n = (int32_t)remaining;
461 					}
462 					if (ocs_copy_to_user(req->user_buffer + written,
463 						ocs_textbuf_ext_get_buffer(ptbuf, idx), n)) {
464 						ocs_log_test(ocs, "Error: (%d) ocs_copy_to_user failed\n", __LINE__);
465 					}
466 					written += n;
467 					remaining -= (uint32_t)n;
468 				}
469 			}
470 			req->bytes_written = written;
471 			if (ptbuf == &textbuf) {
472 				ocs_textbuf_free(ocs, &textbuf);
473 			}
474 
475 			break;
476 		}
477 		case OCS_IOCTL_DDUMP_CLR_SAVED:
478 			ocs_clear_saved_ddump(ocs);
479 			break;
480 		default:
481 			ocs_log_err(ocs, "Error: ocs_textbuf_alloc failed\n");
482 			break;
483 		}
484 		break;
485 	}
486 	case OCS_IOCTL_CMD_DRIVER_INFO: {
487 		ocs_ioctl_driver_info_t *req = (ocs_ioctl_driver_info_t*)addr;
488 
489 		ocs_memset(req, 0, sizeof(*req));
490 
491 		req->pci_vendor = ocs->pci_vendor;
492 		req->pci_device = ocs->pci_device;
493 		ocs_strncpy(req->businfo, ocs->businfo, sizeof(req->businfo));
494 
495 		req->sli_intf = ocs_config_read32(ocs, SLI4_INTF_REG);
496 		ocs_strncpy(req->desc, device_get_desc(dev), sizeof(req->desc));
497 		ocs_strncpy(req->fw_rev, ocs->fwrev, sizeof(req->fw_rev));
498 		if (ocs->domain && ocs->domain->sport) {
499 			*((uint64_t*)req->hw_addr.fc.wwnn) = ocs_htobe64(ocs->domain->sport->wwnn);
500 			*((uint64_t*)req->hw_addr.fc.wwpn) = ocs_htobe64(ocs->domain->sport->wwpn);
501 		}
502 		ocs_strncpy(req->serialnum, ocs->serialnum, sizeof(req->serialnum));
503 		break;
504 	}
505 
506 	case OCS_IOCTL_CMD_MGMT_LIST: {
507 		ocs_ioctl_mgmt_buffer_t* req = (ocs_ioctl_mgmt_buffer_t *)addr;
508 		ocs_textbuf_t textbuf;
509 
510 		/* Build a text buffer */
511 		if (ocs_textbuf_alloc(ocs, &textbuf, req->user_buffer_len)) {
512 			ocs_log_err(ocs, "Error: ocs_textbuf_alloc failed\n");
513 			return -EFAULT;
514 		}
515 
516 		ocs_mgmt_get_list(ocs, &textbuf);
517 
518 		if (ocs_textbuf_get_written(&textbuf)) {
519 			if (ocs_copy_to_user(req->user_buffer,
520 				ocs_textbuf_get_buffer(&textbuf),
521 				ocs_textbuf_get_written(&textbuf))) {
522 				ocs_log_test(ocs, "Error: (%d) ocs_copy_to_user failed\n", __LINE__);
523 			}
524 		}
525 		req->bytes_written = ocs_textbuf_get_written(&textbuf);
526 
527 		ocs_textbuf_free(ocs, &textbuf);
528 
529 		break;
530 	}
531 
532 	case OCS_IOCTL_CMD_MGMT_GET_ALL: {
533 		ocs_ioctl_mgmt_buffer_t* req = (ocs_ioctl_mgmt_buffer_t *)addr;
534 		ocs_textbuf_t textbuf;
535 		int32_t n;
536 		uint32_t idx;
537 		uint32_t copied = 0;
538 
539 		/* Build a text buffer */
540 		if (ocs_textbuf_alloc(ocs, &textbuf, req->user_buffer_len)) {
541 			ocs_log_err(ocs, "Error: ocs_textbuf_alloc failed\n");
542 			return -EFAULT;
543 		}
544 
545 		ocs_mgmt_get_all(ocs, &textbuf);
546 
547 		for (idx = 0; (n = ocs_textbuf_ext_get_written(&textbuf, idx)) > 0; idx++) {
548 			if(ocs_copy_to_user(req->user_buffer + copied,
549 					ocs_textbuf_ext_get_buffer(&textbuf, idx),
550 					ocs_textbuf_ext_get_written(&textbuf, idx))) {
551 					ocs_log_err(ocs, "Error: ocs_textbuf_alloc failed\n");
552 			}
553 			copied += n;
554 		}
555 		req->bytes_written = copied;
556 
557 		ocs_textbuf_free(ocs, &textbuf);
558 
559 		break;
560 	}
561 
562 	case OCS_IOCTL_CMD_MGMT_GET: {
563 		ocs_ioctl_cmd_get_t* req = (ocs_ioctl_cmd_get_t*)addr;
564 		ocs_textbuf_t textbuf;
565 		char name[OCS_MGMT_MAX_NAME];
566 
567 		/* Copy the name value in from user space */
568 		if (ocs_copy_from_user(name, req->name, OCS_MGMT_MAX_NAME)) {
569 			ocs_log_test(ocs, "ocs_copy_from_user failed\n");
570 			ocs_ioctl_free(ocs, req, sizeof(ocs_ioctl_cmd_get_t));
571 			return -EFAULT;
572 		}
573 
574 		/* Build a text buffer */
575 		if (ocs_textbuf_alloc(ocs, &textbuf, req->value_length)) {
576 			ocs_log_err(ocs, "Error: ocs_textbuf_alloc failed\n");
577 			return -EFAULT;
578 		}
579 
580 		ocs_mgmt_get(ocs, name, &textbuf);
581 
582 		if (ocs_textbuf_get_written(&textbuf)) {
583 			if (ocs_copy_to_user(req->value,
584 				ocs_textbuf_get_buffer(&textbuf),
585 				ocs_textbuf_get_written(&textbuf))) {
586 				ocs_log_test(ocs, "Error: (%d) ocs_copy_to_user failed\n", __LINE__);
587 		}
588 		}
589 		req->value_length = ocs_textbuf_get_written(&textbuf);
590 
591 		ocs_textbuf_free(ocs, &textbuf);
592 
593 		break;
594 	}
595 
596 	case OCS_IOCTL_CMD_MGMT_SET: {
597 		char name[OCS_MGMT_MAX_NAME];
598 		char value[OCS_MGMT_MAX_VALUE];
599 		ocs_ioctl_cmd_set_t* req = (ocs_ioctl_cmd_set_t*)addr;
600 
601 		// Copy the name  in from user space
602 		if (ocs_copy_from_user(name, req->name, OCS_MGMT_MAX_NAME)) {
603 			ocs_log_test(ocs, "Error: copy from user failed\n");
604 			ocs_ioctl_free(ocs, req, sizeof(*req));
605 			return -EFAULT;
606 		}
607 
608 		// Copy the  value in from user space
609 		if (ocs_copy_from_user(value, req->value, OCS_MGMT_MAX_VALUE)) {
610 			ocs_log_test(ocs, "Error: copy from user failed\n");
611 			ocs_ioctl_free(ocs, req, sizeof(*req));
612 			return -EFAULT;
613 		}
614 
615 		req->result = ocs_mgmt_set(ocs, name, value);
616 
617 		break;
618 	}
619 
620 	case OCS_IOCTL_CMD_MGMT_EXEC: {
621 		ocs_ioctl_action_t* req = (ocs_ioctl_action_t*) addr;
622 		char action_name[OCS_MGMT_MAX_NAME];
623 
624 		if (ocs_copy_from_user(action_name, req->name, sizeof(action_name))) {
625 			ocs_log_test(ocs, "Error: copy req.name from user failed\n");
626 			ocs_ioctl_free(ocs, req, sizeof(*req));
627 			return -EFAULT;
628 		}
629 
630 		req->result = ocs_mgmt_exec(ocs, action_name, req->arg_in, req->arg_in_length,
631 				req->arg_out, req->arg_out_length);
632 
633 		break;
634 	}
635 
636 	default:
637 		ocs_log_test(ocs, "Error: unknown cmd %#lx\n", cmd);
638 		status = -ENOTTY;
639 		break;
640 	}
641 	return status;
642 }
643 
644 static void
645 ocs_fw_write_cb(int32_t status, uint32_t actual_write_length,
646 					uint32_t change_status, void *arg)
647 {
648         ocs_mgmt_fw_write_result_t *result = arg;
649 
650         result->status = status;
651         result->actual_xfer = actual_write_length;
652         result->change_status = change_status;
653 
654         ocs_sem_v(&(result->semaphore));
655 }
656 
657 int
658 ocs_firmware_write(ocs_t *ocs, const uint8_t *buf, size_t buf_len,
659 						uint8_t *change_status)
660 {
661         int rc = 0;
662         uint32_t bytes_left;
663         uint32_t xfer_size;
664         uint32_t offset;
665         ocs_dma_t dma;
666         int last = 0;
667         ocs_mgmt_fw_write_result_t result;
668 
669         ocs_sem_init(&(result.semaphore), 0, "fw_write");
670 
671         bytes_left = buf_len;
672         offset = 0;
673 
674         if (ocs_dma_alloc(ocs, &dma, FW_WRITE_BUFSIZE, 4096)) {
675                 ocs_log_err(ocs, "ocs_firmware_write: malloc failed\n");
676                 return -ENOMEM;
677         }
678 
679         while (bytes_left > 0) {
680                 if (bytes_left > FW_WRITE_BUFSIZE) {
681                         xfer_size = FW_WRITE_BUFSIZE;
682                 } else {
683                         xfer_size = bytes_left;
684                 }
685 
686                 ocs_memcpy(dma.virt, buf + offset, xfer_size);
687 
688                 if (bytes_left == xfer_size) {
689                         last = 1;
690                 }
691 
692                 ocs_hw_firmware_write(&ocs->hw, &dma, xfer_size, offset,
693 						last, ocs_fw_write_cb, &result);
694 
695                 if (ocs_sem_p(&(result.semaphore), OCS_SEM_FOREVER) != 0) {
696                         rc = -ENXIO;
697                         break;
698                 }
699 
700                 if (result.actual_xfer == 0 || result.status != 0) {
701                         rc = -EFAULT;
702                         break;
703                 }
704 
705                 if (last) {
706                         *change_status = result.change_status;
707                 }
708 
709                 bytes_left -= result.actual_xfer;
710                 offset += result.actual_xfer;
711         }
712 
713         ocs_dma_free(ocs, &dma);
714         return rc;
715 }
716 
717 static int
718 ocs_sys_fwupgrade(SYSCTL_HANDLER_ARGS)
719 {
720 	char file_name[256] = {0};
721 	char fw_change_status;
722 	uint32_t rc = 1;
723         ocs_t *ocs  = (ocs_t *)arg1;
724         const struct firmware *fw;
725 	const struct ocs_hw_grp_hdr *fw_image;
726 
727         rc = sysctl_handle_string(oidp, file_name, sizeof(file_name), req);
728         if (rc || !req->newptr)
729                 return rc;
730 
731         fw = firmware_get(file_name);
732         if (fw == NULL) {
733                 device_printf(ocs->dev, "Unable to get Firmware. "
734                         "Make sure %s is copied to /boot/modules\n", file_name);
735                 return ENOENT;
736         }
737 
738 	fw_image = (const struct ocs_hw_grp_hdr *)fw->data;
739 
740         /* Check if firmware provided is compatible with this particular
741          * Adapter of not*/
742         if ((ocs_be32toh(fw_image->magic_number) != OCS_HW_OBJECT_G5) &&
743                 (ocs_be32toh(fw_image->magic_number) != OCS_HW_OBJECT_G6)) {
744                 device_printf(ocs->dev,
745                         "Invalid FW image found Magic: 0x%x Size: %zu \n",
746                         ocs_be32toh(fw_image->magic_number), fw->datasize);
747                 rc = -1;
748                 goto exit;
749         }
750 
751         if (!strncmp(ocs->fw_version, fw_image->revision,
752 					strnlen(fw_image->revision, 16))) {
753                 device_printf(ocs->dev, "No update req. "
754 				"Firmware is already up to date. \n");
755                 rc = 0;
756                 goto exit;
757         }
758 
759 	device_printf(ocs->dev, "Upgrading Firmware from %s to %s \n",
760 				ocs->fw_version, fw_image->revision);
761 
762 	rc = ocs_firmware_write(ocs, fw->data, fw->datasize, &fw_change_status);
763         if (rc) {
764                 ocs_log_err(ocs, "Firmware update failed with status = %d\n", rc);
765         } else {
766                 ocs_log_info(ocs, "Firmware updated successfully\n");
767                 switch (fw_change_status) {
768                         case 0x00:
769                                 device_printf(ocs->dev,
770 				"No reset needed, new firmware is active.\n");
771                                 break;
772                         case 0x01:
773                                 device_printf(ocs->dev,
774 				"A physical device reset (host reboot) is "
775 				"needed to activate the new firmware\n");
776                                 break;
777                         case 0x02:
778                         case 0x03:
779                                 device_printf(ocs->dev,
780 				"firmware is resetting to activate the new "
781 				"firmware, Host reboot is needed \n");
782                                 break;
783                         default:
784                                 ocs_log_warn(ocs,
785                                         "Unexected value change_status: %d\n",
786                                         fw_change_status);
787                                 break;
788                 }
789         }
790 
791 exit:
792         /* Release Firmware*/
793         firmware_put(fw, FIRMWARE_UNLOAD);
794 
795         return rc;
796 
797 }
798 
799 static int
800 ocs_sysctl_wwnn(SYSCTL_HANDLER_ARGS)
801 {
802 	uint32_t rc = 1;
803 	ocs_t *ocs = oidp->oid_arg1;
804 	char old[64];
805 	char new[64];
806 	uint64_t *wwnn = NULL;
807 	ocs_xport_t *xport = ocs->xport;
808 
809 	if (xport->req_wwnn) {
810 		wwnn = &xport->req_wwnn;
811 		memset(old, 0, sizeof(old));
812 		snprintf(old, sizeof(old), "0x%llx" , (unsigned long long) *wwnn);
813 
814 	} else {
815 		wwnn = ocs_hw_get_ptr(&ocs->hw, OCS_HW_WWN_NODE);
816 
817 		memset(old, 0, sizeof(old));
818 		snprintf(old, sizeof(old), "0x%llx" , (unsigned long long) ocs_htobe64(*wwnn));
819 	}
820 
821 	/*Read wwnn*/
822 	if (!req->newptr) {
823 		return (sysctl_handle_string(oidp, old, sizeof(old), req));
824 	}
825 
826 	/*Configure port wwn*/
827 	rc = sysctl_handle_string(oidp, new, sizeof(new), req);
828 	if (rc)
829 		return (rc);
830 
831 	if (strncmp(old, new, strlen(old)) == 0) {
832 		return 0;
833 	}
834 
835 	return (set_req_wwnn(ocs, NULL, new));
836 }
837 
838 static int
839 ocs_sysctl_wwpn(SYSCTL_HANDLER_ARGS)
840 {
841 	uint32_t rc = 1;
842 	ocs_t *ocs = oidp->oid_arg1;
843 	char old[64];
844 	char new[64];
845 	uint64_t *wwpn = NULL;
846 	ocs_xport_t *xport = ocs->xport;
847 
848 	if (xport->req_wwpn) {
849 		wwpn = &xport->req_wwpn;
850 		memset(old, 0, sizeof(old));
851 		snprintf(old, sizeof(old), "0x%llx",(unsigned long long) *wwpn);
852 	} else {
853 		wwpn = ocs_hw_get_ptr(&ocs->hw, OCS_HW_WWN_PORT);
854 		memset(old, 0, sizeof(old));
855 		snprintf(old, sizeof(old), "0x%llx",(unsigned long long) ocs_htobe64(*wwpn));
856 	}
857 
858 	/*Read wwpn*/
859 	if (!req->newptr) {
860 		return (sysctl_handle_string(oidp, old, sizeof(old), req));
861 	}
862 
863 	/*Configure port wwn*/
864 	rc = sysctl_handle_string(oidp, new, sizeof(new), req);
865 	if (rc)
866 		return (rc);
867 
868 	if (strncmp(old, new, strlen(old)) == 0) {
869 		return 0;
870 	}
871 
872 	return (set_req_wwpn(ocs, NULL, new));
873 }
874 
875 static int
876 ocs_sysctl_current_topology(SYSCTL_HANDLER_ARGS)
877 {
878 	ocs_t *ocs = oidp->oid_arg1;
879 	uint32_t value;
880 
881 	ocs_hw_get(&ocs->hw, OCS_HW_TOPOLOGY, &value);
882 
883 	return (sysctl_handle_int(oidp, &value, 0, req));
884 }
885 
886 static int
887 ocs_sysctl_current_speed(SYSCTL_HANDLER_ARGS)
888 {
889 	ocs_t *ocs = oidp->oid_arg1;
890 	uint32_t value;
891 
892 	ocs_hw_get(&ocs->hw, OCS_HW_LINK_SPEED, &value);
893 
894 	return (sysctl_handle_int(oidp, &value, 0, req));
895 }
896 
897 static int
898 ocs_sysctl_config_topology(SYSCTL_HANDLER_ARGS)
899 {
900 	uint32_t rc = 1;
901 	ocs_t *ocs = oidp->oid_arg1;
902 	uint32_t old_value;
903 	uint32_t new_value;
904 	char buf[64];
905 
906 	ocs_hw_get(&ocs->hw, OCS_HW_CONFIG_TOPOLOGY, &old_value);
907 
908 	/*Read topo*/
909 	if (!req->newptr) {
910 		return (sysctl_handle_int(oidp, &old_value, 0, req));
911 	}
912 
913 	/*Configure port wwn*/
914 	rc = sysctl_handle_int(oidp, &new_value, 0, req);
915 	if (rc)
916 		return (rc);
917 
918 	if (new_value == old_value) {
919 		return 0;
920 	}
921 
922 	snprintf(buf, sizeof(buf), "%d",new_value);
923 	rc = set_configured_topology(ocs, NULL, buf);
924 	return rc;
925 }
926 
927 static int
928 ocs_sysctl_config_speed(SYSCTL_HANDLER_ARGS)
929 {
930 	uint32_t rc = 1;
931 	ocs_t *ocs = oidp->oid_arg1;
932 	uint32_t old_value;
933 	uint32_t new_value;
934 	char buf[64];
935 
936 	ocs_hw_get(&ocs->hw, OCS_HW_LINK_CONFIG_SPEED, &old_value);
937 
938 	/*Read topo*/
939 	if (!req->newptr) {
940 		return (sysctl_handle_int(oidp, &old_value, 0, req));
941 	}
942 
943 	/*Configure port wwn*/
944 	rc = sysctl_handle_int(oidp, &new_value, 0, req);
945 	if (rc)
946 		return (rc);
947 
948 	if (new_value == old_value) {
949 		return 0;
950 	}
951 
952 	snprintf(buf, sizeof(buf), "%d",new_value);
953 	rc = set_configured_speed(ocs, NULL,buf);
954 	return rc;
955 }
956 
957 static int
958 ocs_sysctl_fcid(SYSCTL_HANDLER_ARGS)
959 {
960 	ocs_t *ocs = oidp->oid_arg1;
961 	char buf[64];
962 
963 	memset(buf, 0, sizeof(buf));
964 	if (ocs->domain && ocs->domain->attached) {
965 		snprintf(buf, sizeof(buf), "0x%06x",
966 			ocs->domain->sport->fc_id);
967 	}
968 
969 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
970 }
971 
972 static int
973 ocs_sysctl_port_state(SYSCTL_HANDLER_ARGS)
974 {
975 
976 	char new[256] = {0};
977 	uint32_t rc = 1;
978 	ocs_xport_stats_t old;
979 	ocs_t *ocs  = (ocs_t *)arg1;
980 
981 	ocs_xport_status(ocs->xport, OCS_XPORT_CONFIG_PORT_STATUS, &old);
982 
983 	/*Read port state */
984 	if (!req->newptr) {
985 		snprintf(new, sizeof(new), "%s",
986 			(old.value == OCS_XPORT_PORT_OFFLINE) ?
987 					 "offline" : "online");
988 		return (sysctl_handle_string(oidp, new, sizeof(new), req));
989         }
990 
991 	/*Configure port state*/
992 	rc = sysctl_handle_string(oidp, new, sizeof(new), req);
993 	if (rc)
994 		return (rc);
995 
996 	if (ocs_strcasecmp(new, "offline") == 0) {
997 		if (old.value == OCS_XPORT_PORT_OFFLINE) {
998 			return (0);
999 		}
1000 		ocs_log_debug(ocs, "Setting port to %s\n", new);
1001 		rc = ocs_xport_control(ocs->xport, OCS_XPORT_PORT_OFFLINE);
1002 		if (rc != 0) {
1003 			ocs_log_err(ocs, "Setting port to offline failed\n");
1004 		}
1005 	} else if (ocs_strcasecmp(new, "online") == 0) {
1006 		if (old.value == OCS_XPORT_PORT_ONLINE) {
1007 			return (0);
1008 		}
1009 		ocs_log_debug(ocs, "Setting port to %s\n", new);
1010 		rc = ocs_xport_control(ocs->xport, OCS_XPORT_PORT_ONLINE);
1011 		if (rc != 0) {
1012 			ocs_log_err(ocs, "Setting port to online failed\n");
1013 		}
1014 	} else {
1015 		ocs_log_err(ocs, "Unsupported link state %s\n", new);
1016 		rc = 1;
1017 	}
1018 
1019 	return (rc);
1020 
1021 }
1022 
1023 static int
1024 ocs_sysctl_vport_wwpn(SYSCTL_HANDLER_ARGS)
1025 {
1026 	ocs_fcport *fcp = oidp->oid_arg1;
1027 	char str_wwpn[64];
1028 
1029 	memset(str_wwpn, 0, sizeof(str_wwpn));
1030 	snprintf(str_wwpn, sizeof(str_wwpn), "0x%llx", (unsigned long long)fcp->vport->wwpn);
1031 
1032 	return (sysctl_handle_string(oidp, str_wwpn, sizeof(str_wwpn), req));
1033 }
1034 
1035 static int
1036 ocs_sysctl_vport_wwnn(SYSCTL_HANDLER_ARGS)
1037 {
1038 	ocs_fcport *fcp = oidp->oid_arg1;
1039 	char str_wwnn[64];
1040 
1041 	memset(str_wwnn, 0, sizeof(str_wwnn));
1042 	snprintf(str_wwnn, sizeof(str_wwnn), "0x%llx", (unsigned long long)fcp->vport->wwnn);
1043 
1044 	return (sysctl_handle_string(oidp, str_wwnn, sizeof(str_wwnn), req));
1045 }
1046 
1047 /**
1048  * @brief Initialize sysctl
1049  *
1050  * Initialize sysctl so elxsdkutil can query device information.
1051  *
1052  * @param ocs pointer to ocs
1053  * @return void
1054  */
1055 static void
1056 ocs_sysctl_init(ocs_t *ocs)
1057 {
1058 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(ocs->dev);
1059 	struct sysctl_oid *tree = device_get_sysctl_tree(ocs->dev);
1060 	struct sysctl_oid *vtree;
1061 	const char *str = NULL;
1062 	char name[16];
1063 	uint32_t rev, if_type, family, i;
1064 	ocs_fcport *fcp = NULL;
1065 
1066 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1067 			"devid", CTLFLAG_RD, NULL,
1068 			pci_get_devid(ocs->dev), "Device ID");
1069 
1070 	memset(ocs->modeldesc, 0, sizeof(ocs->modeldesc));
1071 	if (0 == pci_get_vpd_ident(ocs->dev, &str)) {
1072 		snprintf(ocs->modeldesc, sizeof(ocs->modeldesc), "%s", str);
1073 	}
1074 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1075 			"modeldesc", CTLFLAG_RD,
1076 			ocs->modeldesc,
1077 			0, "Model Description");
1078 
1079 	memset(ocs->serialnum, 0, sizeof(ocs->serialnum));
1080 	if (0 == pci_get_vpd_readonly(ocs->dev, "SN", &str)) {
1081 		snprintf(ocs->serialnum, sizeof(ocs->serialnum), "%s", str);
1082 	}
1083 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1084 			"sn", CTLFLAG_RD,
1085 			ocs->serialnum,
1086 			0, "Serial Number");
1087 
1088 	ocs_hw_get(&ocs->hw, OCS_HW_SLI_REV, &rev);
1089 	ocs_hw_get(&ocs->hw, OCS_HW_IF_TYPE, &if_type);
1090 	ocs_hw_get(&ocs->hw, OCS_HW_SLI_FAMILY, &family);
1091 
1092 	memset(ocs->fwrev, 0, sizeof(ocs->fwrev));
1093 	snprintf(ocs->fwrev, sizeof(ocs->fwrev), "%s, sli-%d:%d:%x",
1094 			(char *)ocs_hw_get_ptr(&ocs->hw, OCS_HW_FW_REV),
1095 			rev, if_type, family);
1096 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1097 			"fwrev", CTLFLAG_RD,
1098 			ocs->fwrev,
1099 			0, "Firmware Revision");
1100 
1101 	memset(ocs->sli_intf, 0, sizeof(ocs->sli_intf));
1102 	snprintf(ocs->sli_intf, sizeof(ocs->sli_intf), "%08x",
1103 		 ocs_config_read32(ocs, SLI4_INTF_REG));
1104 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1105 			  "sli_intf", CTLFLAG_RD,
1106 			  ocs->sli_intf,
1107 			  0, "SLI Interface");
1108 
1109         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "fw_upgrade",
1110             CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, (void *)ocs, 0,
1111 	    ocs_sys_fwupgrade, "A", "Firmware grp file");
1112 
1113 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1114 	    "wwnn", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1115 	    ocs, 0, ocs_sysctl_wwnn, "A",
1116 	    "World Wide Node Name, wwnn should be in the format 0x<XXXXXXXXXXXXXXXX>");
1117 
1118 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1119 	    "wwpn", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1120 	    ocs, 0, ocs_sysctl_wwpn, "A",
1121 	    "World Wide Port Name, wwpn should be in the format 0x<XXXXXXXXXXXXXXXX>");
1122 
1123 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1124 	    "current_topology", CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE,
1125 	    ocs, 0, ocs_sysctl_current_topology, "IU",
1126 	    "Current Topology, 1-NPort; 2-Loop; 3-None");
1127 
1128 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1129 	    "current_speed", CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE,
1130 	    ocs, 0, ocs_sysctl_current_speed, "IU",
1131 	    "Current Speed");
1132 
1133 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1134 	    "configured_topology", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1135 	    ocs, 0, ocs_sysctl_config_topology, "IU",
1136 	    "Configured Topology, 0-Auto; 1-NPort; 2-Loop");
1137 
1138 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1139 	    "configured_speed", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1140 	    ocs, 0, ocs_sysctl_config_speed, "IU",
1141 	    "Configured Speed, 0-Auto, 2000, 4000, 8000, 16000, 32000");
1142 
1143 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1144 	    "businfo", CTLFLAG_RD, ocs->businfo, 0, "Bus Info");
1145 
1146 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1147 	    "fcid", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1148 	    ocs, 0, ocs_sysctl_fcid, "A", "Port FC ID");
1149 
1150 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1151 	    "port_state", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1152 	    ocs, 0, ocs_sysctl_port_state, "A", "configured port state");
1153 
1154 	for (i	= 0; i < ocs->num_vports; i++) {
1155 		fcp = FCPORT(ocs, i+1);
1156 
1157 		memset(name, 0, sizeof(name));
1158 		snprintf(name, sizeof(name), "vport%d", i);
1159 		vtree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
1160 		    OID_AUTO, name, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1161 		    "Virtual port");
1162 
1163 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(vtree), OID_AUTO,
1164 		    "wwnn", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1165 		    fcp, 0, ocs_sysctl_vport_wwnn, "A",
1166 		    "World Wide Node Name");
1167 
1168 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(vtree), OID_AUTO,
1169 		    "wwpn", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1170 		    fcp, 0, ocs_sysctl_vport_wwpn, "A", "World Wide Port Name");
1171 	}
1172 
1173 }
1174 
1175 /**
1176  * @brief Initialize the debug module
1177  *
1178  * Parse device hints (similar to Linux module parameters) here. To use,
1179  * run the command
1180  *    kenv hint.ocs.U.P=V
1181  * from the command line replacing U with the unit # (0,1,...),
1182  * P with the parameter name (debug_mask), and V with the value
1183  */
1184 void
1185 ocs_debug_attach(void *os)
1186 {
1187 	struct ocs_softc *ocs = os;
1188 	int error = 0;
1189 	char *resname = NULL;
1190 	int32_t	unit = INT32_MAX;
1191 	uint32_t ocs_debug_mask = 0;
1192 
1193 	resname = "debug_mask";
1194 	if (0 == (error = resource_int_value(device_get_name(ocs->dev), device_get_unit(ocs->dev),
1195 				resname, &ocs_debug_mask))) {
1196 		device_printf(ocs->dev, "setting %s to %010x\n", resname, ocs_debug_mask);
1197 		ocs_debug_enable(ocs_debug_mask);
1198 	}
1199 
1200 	unit = device_get_unit(ocs->dev);
1201 	ocs->cdev = make_dev(&ocs_cdevsw, unit, UID_ROOT, GID_OPERATOR, 0640,
1202 			"ocs%d", unit);
1203 	if (ocs->cdev) {
1204 		ocs->cdev->si_drv1 = ocs;
1205 	}
1206 
1207 	/* initialize sysctl interface */
1208 	ocs_sysctl_init(ocs);
1209 	mtx_init(&ocs->dbg_lock, "ocs_dbg_lock", NULL, MTX_DEF);
1210 }
1211 
1212 /**
1213  * @brief Free the debug module
1214  */
1215 void
1216 ocs_debug_detach(void *os)
1217 {
1218 	struct ocs_softc *ocs = os;
1219 
1220 	mtx_destroy(&ocs->dbg_lock);
1221 
1222 	if (ocs->cdev) {
1223 		ocs->cdev->si_drv1 = NULL;
1224 		destroy_dev(ocs->cdev);
1225 	}
1226 }
1227