aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/smartpqi/smartpqi_ioctl.c
blob: 9f43f2e1faa2777f1aff22b64af04c4a8256c5b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*-
 * Copyright 2016-2021 Microchip Technology, Inc. and/or its subsidiaries.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* $FreeBSD$ */

/*
 * Management interface for smartpqi driver
 */

#include "smartpqi_includes.h"

/*
 * Wrapper function to copy to user from kernel
 */
int
os_copy_to_user(struct pqisrc_softstate *softs, void *dest_buf,
		void *src_buf, int size, int mode)
{
	return(copyout(src_buf, dest_buf, size));
}

/*
 * Wrapper function to copy from user to kernel
 */
int
os_copy_from_user(struct pqisrc_softstate *softs, void *dest_buf,
		void *src_buf, int size, int mode)
{
	return(copyin(src_buf, dest_buf, size));
}

/*
 * Device open function for ioctl entry 
 */
static int
smartpqi_open(struct cdev *cdev, int flags, int devtype,
		struct thread *td)
{
	return BSD_SUCCESS;
}

/*
 * Device close function for ioctl entry
 */
static int
smartpqi_close(struct cdev *cdev, int flags, int devtype,
		struct thread *td)
{
	return BSD_SUCCESS;
}

/*
 * ioctl for getting driver info
 */
static void
smartpqi_get_driver_info_ioctl(caddr_t udata, struct cdev *cdev)
{
	struct pqisrc_softstate *softs = cdev->si_drv1;
	pdriver_info driver_info = (pdriver_info)udata;

	DBG_FUNC("IN udata = %p cdev = %p\n", udata, cdev);

	driver_info->major_version = PQISRC_OS_VERSION;
	driver_info->minor_version = PQISRC_FEATURE_VERSION;
	driver_info->release_version = PQISRC_PATCH_VERSION;
	driver_info->build_revision = PQISRC_BUILD_VERSION;
	driver_info->max_targets = PQI_MAX_DEVICES - 1;
	driver_info->max_io = softs->max_io_for_scsi_ml;
	driver_info->max_transfer_length = softs->pqi_cap.max_transfer_size;

	DBG_FUNC("OUT\n");
}

/*
 * ioctl for getting controller info
 */
static void
smartpqi_get_pci_info_ioctl(caddr_t udata, struct cdev *cdev)
{
	struct pqisrc_softstate *softs = cdev->si_drv1;
	device_t dev = softs->os_specific.pqi_dev;
	pqi_pci_info_t *pci_info = (pqi_pci_info_t *)udata;
	uint32_t sub_vendor = 0;
	uint32_t sub_device = 0;
	uint32_t vendor = 0;
	uint32_t device = 0;

	DBG_FUNC("IN udata = %p cdev = %p\n", udata, cdev);

	pci_info->bus = pci_get_bus(dev);
	pci_info->dev_fn = pci_get_function(dev);
	pci_info->domain = pci_get_domain(dev);
	sub_vendor = pci_read_config(dev, PCIR_SUBVEND_0, 2);
	sub_device = pci_read_config(dev, PCIR_SUBDEV_0, 2);
	pci_info->board_id = ((sub_device << 16) & 0xffff0000) | sub_vendor;
	vendor = pci_get_vendor(dev);
	device =  pci_get_device(dev);
	pci_info->chip_id = ((device << 16) & 0xffff0000) | vendor;
	DBG_FUNC("OUT\n");
}

static inline int
pqi_status_to_bsd_ioctl_status(int pqi_status)
{
	if (PQI_STATUS_SUCCESS == pqi_status)
		return BSD_SUCCESS;
	else
		return EIO;
}

/*
 * ioctl entry point for user
 */
static int
smartpqi_ioctl(struct cdev *cdev, u_long cmd, caddr_t udata,
		int flags, struct thread *td)
{
	int bsd_status, pqi_status;
	struct pqisrc_softstate *softs = cdev->si_drv1;

	DBG_FUNC("IN cmd = 0x%lx udata = %p cdev = %p\n", cmd, udata, cdev);

	if (!udata) {
		DBG_ERR("udata is null !!\n");
		return EINVAL;
	}

	if (pqisrc_ctrl_offline(softs)){
		return ENOTTY;
	}

	switch (cmd) {
		case CCISS_GETDRIVVER:
			smartpqi_get_driver_info_ioctl(udata, cdev);
			bsd_status = BSD_SUCCESS;
			break;
		case CCISS_GETPCIINFO:
			smartpqi_get_pci_info_ioctl(udata, cdev);
			bsd_status = BSD_SUCCESS;
			break;
		case SMARTPQI_PASS_THRU:
		case CCISS_PASSTHRU:
			pqi_status = pqisrc_passthru_ioctl(softs, udata, 0);
			bsd_status = pqi_status_to_bsd_ioctl_status(pqi_status);
			break;
		case CCISS_REGNEWD:
			pqi_status = pqisrc_scan_devices(softs);
			bsd_status = pqi_status_to_bsd_ioctl_status(pqi_status);
			break;
		default:
			DBG_WARN( "!IOCTL cmd 0x%lx not supported\n", cmd);
			bsd_status = ENOTTY;
			break;
	}

	DBG_FUNC("OUT error = %d\n", bsd_status);
	return bsd_status;
}

static struct cdevsw smartpqi_cdevsw =
{
	.d_version = D_VERSION,
	.d_open    = smartpqi_open,
	.d_close   = smartpqi_close,
	.d_ioctl   = smartpqi_ioctl,
	.d_name    = "smartpqi",
};

/*
 * Function to create device node for ioctl
 */
int
create_char_dev(struct pqisrc_softstate *softs, int card_index)
{
	int error = BSD_SUCCESS;

	DBG_FUNC("IN idx = %d\n", card_index);

	softs->os_specific.cdev = make_dev(&smartpqi_cdevsw, card_index,
				UID_ROOT, GID_OPERATOR, 0640,
				"smartpqi%u", card_index);
	if(softs->os_specific.cdev) {
		softs->os_specific.cdev->si_drv1 = softs;
	} else {
		error = ENXIO;
	}

	DBG_FUNC("OUT error = %d\n", error);

	return error;
}

/*
 * Function to destroy device node for ioctl
 */
void
destroy_char_dev(struct pqisrc_softstate *softs)
{
	DBG_FUNC("IN\n");
	if (softs->os_specific.cdev) {
		destroy_dev(softs->os_specific.cdev);
		softs->os_specific.cdev = NULL;
	}
	DBG_FUNC("OUT\n");
}

/*
 * Function used to send passthru commands to adapter
 * to support management tools. For eg. ssacli, sscon.
 */
int
pqisrc_passthru_ioctl(struct pqisrc_softstate *softs, void *arg, int mode)
{
	int ret = PQI_STATUS_SUCCESS;
	char *drv_buf = NULL;
	uint32_t tag = 0;
	IOCTL_Command_struct *iocommand = (IOCTL_Command_struct *)arg;
	dma_mem_t ioctl_dma_buf;
	pqisrc_raid_req_t request;
	raid_path_error_info_elem_t error_info;
	ib_queue_t *ib_q = &softs->op_raid_ib_q[PQI_DEFAULT_IB_QUEUE];
	ob_queue_t *ob_q = &softs->op_ob_q[PQI_DEFAULT_IB_QUEUE];
	rcb_t *rcb = NULL;

	memset(&request, 0, sizeof(request));
	memset(&error_info, 0, sizeof(error_info));

	DBG_FUNC("IN");

	if (pqisrc_ctrl_offline(softs))
		return PQI_STATUS_FAILURE;

	if (!arg)
		return (PQI_STATUS_FAILURE);

	if (iocommand->buf_size < 1 &&
		iocommand->Request.Type.Direction != PQIIOCTL_NONE)
		return PQI_STATUS_FAILURE;
	if (iocommand->Request.CDBLen > sizeof(request.cdb))
		return PQI_STATUS_FAILURE;

	switch (iocommand->Request.Type.Direction) {
		case PQIIOCTL_NONE:
		case PQIIOCTL_WRITE:
		case PQIIOCTL_READ:
		case PQIIOCTL_BIDIRECTIONAL:
			break;
		default:
			return PQI_STATUS_FAILURE;
	}

	if (iocommand->buf_size > 0) {
		memset(&ioctl_dma_buf, 0, sizeof(struct dma_mem));
		ioctl_dma_buf.tag = "Ioctl_PassthruCmd_Buffer";
		ioctl_dma_buf.size = iocommand->buf_size;
		ioctl_dma_buf.align = PQISRC_DEFAULT_DMA_ALIGN;
		/* allocate memory */
		ret = os_dma_mem_alloc(softs, &ioctl_dma_buf);
		if (ret) {
			DBG_ERR("Failed to Allocate dma mem for Ioctl PassthruCmd Buffer : %d\n", ret);
			ret = PQI_STATUS_FAILURE;
			goto out;
		}

		DBG_INFO("ioctl_dma_buf.dma_addr  = %p\n",(void*)ioctl_dma_buf.dma_addr);
		DBG_INFO("ioctl_dma_buf.virt_addr = %p\n",(void*)ioctl_dma_buf.virt_addr);

		drv_buf = (char *)ioctl_dma_buf.virt_addr;
		if (iocommand->Request.Type.Direction & PQIIOCTL_WRITE) {
			if ((ret = os_copy_from_user(softs, (void *)drv_buf,
					(void *)iocommand->buf,
					iocommand->buf_size, mode)) != 0) {
				ret = PQI_STATUS_FAILURE;
				goto free_mem;
			}
		}
	}

	request.header.iu_type = PQI_IU_TYPE_RAID_PATH_IO_REQUEST;
	request.header.iu_length = offsetof(pqisrc_raid_req_t, sg_descriptors[1]) -
									PQI_REQUEST_HEADER_LENGTH;
	memcpy(request.lun_number, iocommand->LUN_info.LunAddrBytes,
		sizeof(request.lun_number));
	memcpy(request.cdb, iocommand->Request.CDB, iocommand->Request.CDBLen);
	request.additional_cdb_bytes_usage = PQI_ADDITIONAL_CDB_BYTES_0;

	switch (iocommand->Request.Type.Direction) {
	case PQIIOCTL_NONE:
		request.data_direction = SOP_DATA_DIR_NONE;
		break;
	case PQIIOCTL_WRITE:
		request.data_direction = SOP_DATA_DIR_FROM_DEVICE;
		break;
	case PQIIOCTL_READ:
		request.data_direction = SOP_DATA_DIR_TO_DEVICE;
		break;
	case PQIIOCTL_BIDIRECTIONAL:
		request.data_direction = SOP_DATA_DIR_BIDIRECTIONAL;
		break;
	}

	request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
	if (iocommand->buf_size > 0) {
		request.buffer_length = iocommand->buf_size;
		request.sg_descriptors[0].addr = ioctl_dma_buf.dma_addr;
		request.sg_descriptors[0].len = iocommand->buf_size;
		request.sg_descriptors[0].flags =  SG_FLAG_LAST;
	}
	tag = pqisrc_get_tag(&softs->taglist);
	if (INVALID_ELEM == tag) {
		DBG_ERR("Tag not available\n");
		ret = PQI_STATUS_FAILURE;
		goto free_mem;
	}
	request.request_id = tag;
	request.response_queue_id = ob_q->q_id;
	request.error_index = request.request_id;
	if (softs->timeout_in_passthrough) {
		request.timeout_in_sec = iocommand->Request.Timeout;
	}

	rcb = &softs->rcb[tag];
	rcb->success_cmp_callback = pqisrc_process_internal_raid_response_success;
	rcb->error_cmp_callback = pqisrc_process_internal_raid_response_error;
	rcb->tag = tag;
	rcb->req_pending = true;
	/* Submit Command */
	ret = pqisrc_submit_cmnd(softs, ib_q, &request);
	if (ret != PQI_STATUS_SUCCESS) {
		DBG_ERR("Unable to submit command\n");
		goto err_out;
	}

	ret = pqisrc_wait_on_condition(softs, rcb,
			PQISRC_PASSTHROUGH_CMD_TIMEOUT);
	if (ret != PQI_STATUS_SUCCESS) {
		DBG_ERR("Passthru IOCTL cmd timed out !!\n");
		goto err_out;
	}

	memset(&iocommand->error_info, 0, sizeof(iocommand->error_info));


	if (rcb->status) {
		size_t sense_data_length;

		memcpy(&error_info, rcb->error_info, sizeof(error_info));
		iocommand->error_info.ScsiStatus = error_info.status;
		sense_data_length = error_info.sense_data_len;

		if (!sense_data_length)
			sense_data_length = error_info.resp_data_len;

		if (sense_data_length &&
			(sense_data_length > sizeof(error_info.data)))
				sense_data_length = sizeof(error_info.data);

		if (sense_data_length) {
			if (sense_data_length >
				sizeof(iocommand->error_info.SenseInfo))
				sense_data_length =
					sizeof(iocommand->error_info.SenseInfo);
			memcpy (iocommand->error_info.SenseInfo,
					error_info.data, sense_data_length);
			iocommand->error_info.SenseLen = sense_data_length;
		}

		if (error_info.data_out_result ==
				PQI_RAID_DATA_IN_OUT_UNDERFLOW){
			rcb->status = REQUEST_SUCCESS;
		}
	}

	if (rcb->status == REQUEST_SUCCESS && iocommand->buf_size > 0 &&
		(iocommand->Request.Type.Direction & PQIIOCTL_READ)) {

		if ((ret = os_copy_to_user(softs, (void*)iocommand->buf,
			(void*)drv_buf, iocommand->buf_size, mode)) != 0) {
				DBG_ERR("Failed to copy the response\n");
				goto err_out;
		}
	}

	os_reset_rcb(rcb);
	pqisrc_put_tag(&softs->taglist, request.request_id);
	if (iocommand->buf_size > 0)
			os_dma_mem_free(softs,&ioctl_dma_buf);

	DBG_FUNC("OUT\n");
	return ret;
err_out:
	os_reset_rcb(rcb);
	pqisrc_put_tag(&softs->taglist, request.request_id);

free_mem:
	if (iocommand->buf_size > 0)
		os_dma_mem_free(softs, &ioctl_dma_buf);

out:
	DBG_FUNC("Failed OUT\n");
	return PQI_STATUS_FAILURE;
}