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