xref: /freebsd/sys/dev/qlxgb/qla_ioctl.c (revision ab40f58ccfe6c07ebefddc72f4661a52fe746353)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011-2013 Qlogic Corporation
5  * All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
11  *  1. Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *  2. Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  *  POSSIBILITY OF SUCH DAMAGE.
28  */
29 /*
30  * File: qla_ioctl.c
31  * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "qla_os.h"
38 #include "qla_reg.h"
39 #include "qla_hw.h"
40 #include "qla_def.h"
41 #include "qla_reg.h"
42 #include "qla_inline.h"
43 #include "qla_glbl.h"
44 #include "qla_ioctl.h"
45 
46 static struct cdevsw qla_cdevsw = {
47 	.d_version = D_VERSION,
48 	.d_ioctl = qla_eioctl,
49 	.d_name = "qlcnic",
50 };
51 
52 int
53 qla_make_cdev(qla_host_t *ha)
54 {
55         ha->ioctl_dev = make_dev(&qla_cdevsw,
56 				ha->ifp->if_dunit,
57                                 UID_ROOT,
58                                 GID_WHEEL,
59                                 0600,
60                                 "%s",
61                                 if_name(ha->ifp));
62 
63 	if (ha->ioctl_dev == NULL)
64 		return (-1);
65 
66         ha->ioctl_dev->si_drv1 = ha;
67 
68 	return (0);
69 }
70 
71 void
72 qla_del_cdev(qla_host_t *ha)
73 {
74 	if (ha->ioctl_dev != NULL)
75 		destroy_dev(ha->ioctl_dev);
76 	return;
77 }
78 
79 int
80 qla_eioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
81         struct thread *td)
82 {
83         qla_host_t *ha;
84         int rval = 0;
85         qla_reg_val_t *rv;
86         qla_rd_flash_t *rdf;
87 	qla_wr_flash_t *wrf;
88 	qla_rd_pci_ids_t *pci_ids;
89 	device_t pci_dev;
90 
91         if ((ha = (qla_host_t *)dev->si_drv1) == NULL)
92                 return ENXIO;
93 
94 	pci_dev= ha->pci_dev;
95 
96         switch(cmd) {
97 
98         case QLA_RDWR_REG:
99 
100                 rv = (qla_reg_val_t *)data;
101 
102                 if (rv->direct) {
103                         if (rv->rd) {
104                                 rv->val = READ_OFFSET32(ha, rv->reg);
105                         } else {
106                                 WRITE_OFFSET32(ha, rv->reg, rv->val);
107                         }
108                 } else {
109                         if ((rval = qla_rdwr_indreg32(ha, rv->reg, &rv->val,
110                                 rv->rd)))
111                                 rval = ENXIO;
112                 }
113                 break;
114 
115         case QLA_RD_FLASH:
116                 rdf = (qla_rd_flash_t *)data;
117                 if ((rval = qla_rd_flash32(ha, rdf->off, &rdf->data)))
118                         rval = ENXIO;
119                 break;
120 
121         case QLA_WR_FLASH:
122                 wrf = (qla_wr_flash_t *)data;
123                 if ((rval = qla_wr_flash_buffer(ha, wrf->off, wrf->size,
124 					wrf->buffer, wrf->pattern)))
125                         rval = ENXIO;
126                 break;
127 
128 
129 	case QLA_ERASE_FLASH:
130 		if (qla_erase_flash(ha, ((qla_erase_flash_t *)data)->off,
131 			((qla_erase_flash_t *)data)->size))
132 			rval = ENXIO;
133 		break;
134 
135 	case QLA_RD_PCI_IDS:
136 		pci_ids = (qla_rd_pci_ids_t *)data;
137 		pci_ids->ven_id = pci_get_vendor(pci_dev);
138 		pci_ids->dev_id = pci_get_device(pci_dev);
139 		pci_ids->subsys_ven_id = pci_get_subvendor(pci_dev);
140 		pci_ids->subsys_dev_id = pci_get_subdevice(pci_dev);
141 		pci_ids->rev_id = pci_read_config(pci_dev, PCIR_REVID, 1);
142 		break;
143 
144         default:
145                 break;
146         }
147 
148         return rval;
149 }
150 
151