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