summaryrefslogtreecommitdiff
path: root/drivers/scsi/leapraid/leapraid_app.c
blob: 742f19c07fcbb4799193b38aff3bb9036e85ddf6 (plain)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2026 LeapIO Tech Inc.
 *
 * LeapRAID storage and RAID controller driver.
 */
#include <linux/compat.h>
#include <linux/module.h>
#include <linux/miscdevice.h>

#include "leapraid_func.h"

/* IOCTL device file name. */
#define LEAPRAID_DEV_NAME       "leapraid_ctl"

/* IOCTL version. */
#define LEAPRAID_IOCTL_VERSION  0x07

/* IOCTL commands. */
#define LEAPRAID_ADAPTER_INFO   17
#define LEAPRAID_COMMAND        20
#define LEAPRAID_EVENTQUERY     21
#define LEAPRAID_EVENTREPORT    23

/**
 * struct leapraid_ioctl_header - IOCTL command header
 *
 * @adapter_id : Adapter identifier.
 * @port_number: Port identifier.
 * @max_data_size: Maximum data size for transfer.
 */
struct leapraid_ioctl_header {
	u32 adapter_id;
	u32 port_number;
	u32 max_data_size;
};

/**
 * struct leapraid_ioctl_diag_reset - Diagnostic reset request
 *
 * @hdr: Common IOCTL header.
 */
struct leapraid_ioctl_diag_reset {
	struct leapraid_ioctl_header hdr;
};

/**
 * struct leapraid_ioctl_pci_info - PCI device information
 *
 * @u: Union holding PCI bus/device/function information.
 * @u.bits.dev: PCI device number.
 * @u.bits.func: PCI function number.
 * @u.bits.bus: PCI bus number.
 * @u.word: Combined representation of PCI BDF.
 * @seg_id: PCI segment identifier.
 */
struct leapraid_ioctl_pci_info {
	union {
		struct {
			u32 dev:5;
			u32 func:3;
			u32 bus:24;
		} bits;
		u32 word;
	} u;
	u32 seg_id;
};

/**
 * struct leapraid_ioctl_adapter_info - Adapter information for IOCTL
 *
 * @hdr: IOCTL header.
 * @adapter_type: Adapter type identifier.
 * @port_number: Port number.
 * @pci_id: PCI device ID.
 * @revision: Revision number.
 * @sub_dev: Subsystem device ID.
 * @sub_vendor: Subsystem vendor ID.
 * @r0: Reserved.
 * @fw_ver: Firmware version.
 * @bios_ver: BIOS version.
 * @driver_ver: Driver version.
 * @r1: Reserved.
 * @scsi_id: SCSI ID.
 * @r2: Reserved.
 * @pci_info: PCI information structure.
 */
struct leapraid_ioctl_adapter_info {
	struct leapraid_ioctl_header hdr;
	u32 adapter_type;
	u32 port_number;
	u32 pci_id;
	u32 revision;
	u32 sub_dev;
	u32 sub_vendor;
	u32 r0;
	u32 fw_ver;
	u32 bios_ver;
	u8 driver_ver[32];
	u8 r1;
	u8 scsi_id;
	u16 r2;
	struct leapraid_ioctl_pci_info pci_info;
};

/**
 * struct leapraid_ioctl_command - IOCTL command structure
 *
 * @hdr: IOCTL header.
 * @timeout: Command timeout.
 * @rep_msg_buf_ptr: User pointer to reply message buffer.
 * @c2h_buf_ptr: User pointer to card-to-host data buffer.
 * @h2c_buf_ptr: User pointer to host-to-card data buffer.
 * @sense_data_ptr: User pointer to sense data buffer.
 * @max_rep_bytes: Maximum reply bytes.
 * @c2h_size: Card-to-host data size.
 * @h2c_size: Host-to-card data size.
 * @max_sense_bytes: Maximum sense data bytes.
 * @data_sge_offset: Data SGE offset.
 * @mf: Message frame data (flexible array).
 */
struct leapraid_ioctl_command {
	struct leapraid_ioctl_header hdr;
	u32 timeout;
	void __user *rep_msg_buf_ptr;
	void __user *c2h_buf_ptr;
	void __user *h2c_buf_ptr;
	void __user *sense_data_ptr;
	u32 max_rep_bytes;
	u32 c2h_size;
	u32 h2c_size;
	u32 max_sense_bytes;
	u32 data_sge_offset;
	u8 mf[];
};

static int leapraid_ctl_validate_sge_offset(struct leapraid_adapter *adapter,
					    const struct leapraid_req *req,
					    u32 offset_bytes,
					    size_t h2c_size,
					    size_t c2h_size)
{
	size_t sge_bytes;

	switch (req->func) {
	case LEAPRAID_FUNC_SCSIIO:
	case LEAPRAID_FUNC_SCSIIO_RAID_PASSTHROUGH:
	case LEAPRAID_FUNC_SMP_PASSTHROUGH:
	case LEAPRAID_FUNC_SCSIIO_SATA_PASSTHROUGH:
	case LEAPRAID_FUNC_FW_DOWNLOAD:
	case LEAPRAID_FUNC_FW_UPLOAD:
		sge_bytes = LEAPRAID_IEEE_SGE64_ENTRY_SIZE;
		break;
	default:
		sge_bytes = adapter->adapter_attr.use_32_dma_mask ?
			sizeof(struct leapraid_sge_simple32) :
			sizeof(struct leapraid_sge_simple64);
		break;
	}

	if (h2c_size && c2h_size)
		sge_bytes *= 2;

	if (offset_bytes > LEAPRAID_REQUEST_SIZE - sge_bytes) {
		dev_err(&adapter->pdev->dev,
			"%s: Invalid offset_bytes=%u for func=0x%x\n",
			__func__, offset_bytes, req->func);
		return -EINVAL;
	}

	return 0;
}

static struct leapraid_adapter *leapraid_ctl_lookup_adapter(int adapter_id)
{
	struct leapraid_adapter *adapter;
	struct Scsi_Host *shost;

	spin_lock(&leapraid_adapter_lock);
	list_for_each_entry(adapter, &leapraid_adapter_list, list) {
		if (adapter->adapter_attr.id == adapter_id) {
			if (READ_ONCE(adapter->access_ctrl.host_removing))
				break;
			shost = adapter->shost;
			if (!shost || !scsi_host_get(shost))
				break;
			spin_unlock(&leapraid_adapter_lock);
			return adapter;
		}
	}
	spin_unlock(&leapraid_adapter_lock);

	return NULL;
}

static void leapraid_ctl_put_adapter(struct leapraid_adapter *adapter)
{
	if (adapter && adapter->shost)
		scsi_host_put(adapter->shost);
}

static void leapraid_ctl_scsiio_cmd(struct leapraid_adapter *adapter,
				    void *ctl_sp_mpi_req,
				    u16 taskid,
				    dma_addr_t h2c_dma_addr, size_t h2c_size,
				    dma_addr_t c2h_dma_addr, size_t c2h_size,
				    u16 dev_hdl, void *psge)
{
	struct leapraid_mpi_scsiio_req *scsiio_request = ctl_sp_mpi_req;

	scsiio_request->sense_buffer_len = SCSI_SENSE_BUFFERSIZE;
	scsiio_request->sense_buffer_low_add =
		leapraid_get_sense_buffer_dma(adapter, taskid);
	memset(&adapter->driver_cmds.ctl_cmd.sense,
	       0, SCSI_SENSE_BUFFERSIZE);
	leapraid_build_ieee_sg(adapter, psge, h2c_dma_addr,
			       h2c_size, c2h_dma_addr, c2h_size);
	if (scsiio_request->func == LEAPRAID_FUNC_SCSIIO)
		leapraid_fire_scsi_io(adapter, taskid, dev_hdl);
	else
		leapraid_fire_task(adapter, taskid);
}

static int leapraid_ctl_smp_passthrough_cmd(struct leapraid_adapter *adapter,
					    void *ctl_sp_req,
					    u16 taskid,
					    dma_addr_t h2c_dma_addr,
					    size_t h2c_size,
					    dma_addr_t c2h_dma_addr,
					    size_t c2h_size,
					    void *psge, void *h2c)
{
	struct leapraid_smp_passthrough_req *smp_pt_req = ctl_sp_req;
	u8 *data;

	if (!adapter->adapter_attr.enable_mp)
		smp_pt_req->physical_port = LEAPRAID_DISABLE_MP_PORT_ID;
	if (smp_pt_req->passthrough_flg & LEAPRAID_SMP_PT_FLAG_SGL_PTR)
		data = (u8 *)&smp_pt_req->sgl;
	else
		data = h2c;

	if (!(smp_pt_req->passthrough_flg & LEAPRAID_SMP_PT_FLAG_SGL_PTR) &&
	    h2c_size <= 10) {
		dev_err(&adapter->pdev->dev,
			"%s: Invalid request size=%zu\n",
			__func__, h2c_size);
		return -EINVAL;
	}

	if (data[1] == SMP_PHY_CONTROL &&
	    (data[10] == SMP_PHY_CONTROL_LINK_RESET ||
	     data[10] == SMP_PHY_CONTROL_HARD_RESET))
		adapter->reset_desc.adapter_link_resetting = 1;

	leapraid_build_ieee_sg(adapter, psge, h2c_dma_addr,
			       h2c_size, c2h_dma_addr, c2h_size);
	leapraid_fire_task(adapter, taskid);
	return 0;
}

static void leapraid_ctl_sas_io_unit_ctrl_cmd(struct leapraid_adapter *adapter,
					      void *ctl_sp_req,
					      dma_addr_t h2c_dma_addr,
					      size_t h2c_size,
					      dma_addr_t c2h_dma_addr,
					      size_t c2h_size,
					      void *psge, u16 taskid)
{
	struct leapraid_sas_io_unit_ctrl_req *sas_io_unit_ctl_req = ctl_sp_req;

	if (sas_io_unit_ctl_req->op == LEAPRAID_SAS_OP_PHY_HARD_RESET ||
	    sas_io_unit_ctl_req->op == LEAPRAID_SAS_OP_PHY_LINK_RESET)
		adapter->reset_desc.adapter_link_resetting = 1;
	leapraid_build_mpi_sg(adapter, psge, h2c_dma_addr,
			      h2c_size, c2h_dma_addr, c2h_size);
	leapraid_fire_task(adapter, taskid);
}

static int leapraid_ctl_do_command(struct leapraid_adapter *adapter,
				   struct leapraid_ioctl_command *karg,
				   void __user *mf)
{
	struct leapraid_req *leap_mpi_req;
	struct leapraid_req *ctl_sp_mpi_req;
	u16 taskid;
	void *h2c = NULL;
	size_t h2c_size = 0;
	dma_addr_t h2c_dma_addr = 0;
	void *c2h = NULL;
	size_t c2h_size = 0;
	dma_addr_t c2h_dma_addr = 0;
	void *psge;
	unsigned long timeout;
	u16 dev_hdl = LEAPRAID_INVALID_DEV_HANDLE;
	bool issue_reset = false;
	u32 data_sge_offset_bytes;
	u32 sz;
	int rc;

	rc = leapraid_check_adapter_is_op(adapter, LEAPRAID_DB_WAIT_OP_SHORT,
					  __func__);
	if (rc)
		return rc;

	leap_mpi_req = kzalloc(LEAPRAID_REQUEST_SIZE, GFP_KERNEL);
	if (!leap_mpi_req)
		return -ENOMEM;

	if (karg->data_sge_offset > (UINT_MAX / LEAPRAID_SGE_OFFSET_SIZE)) {
		dev_err(&adapter->pdev->dev,
			"%s: Invalid data_sge_offset=%u\n",
			__func__, karg->data_sge_offset);
		rc = -EINVAL;
		goto out_cleanup;
	}

	data_sge_offset_bytes = karg->data_sge_offset *
				LEAPRAID_SGE_OFFSET_SIZE;
	if (data_sge_offset_bytes > LEAPRAID_REQUEST_SIZE) {
		dev_err(&adapter->pdev->dev,
			"%s: Invalid data_sge_offset=%u\n",
			__func__, karg->data_sge_offset);
		rc = -EINVAL;
		goto out_cleanup;
	}

	if (copy_from_user(leap_mpi_req, mf, data_sge_offset_bytes)) {
		dev_err(&adapter->pdev->dev,
			"%s: Failed to copy request message from user\n",
			__func__);
		rc = -EFAULT;
		goto out_cleanup;
	}

	h2c_size = karg->h2c_size;
	c2h_size = karg->c2h_size;
	rc = leapraid_ctl_validate_sge_offset(adapter, leap_mpi_req,
					      data_sge_offset_bytes,
					      h2c_size, c2h_size);
	if (rc)
		goto out_cleanup;

	taskid = adapter->driver_cmds.ctl_cmd.taskid;

	adapter->driver_cmds.ctl_cmd.status = LEAPRAID_CMD_PENDING;
	memset(&adapter->driver_cmds.ctl_cmd.reply, 0, LEAPRAID_REPLY_SIZE);
	ctl_sp_mpi_req = leapraid_get_task_desc(adapter, taskid);
	memset(ctl_sp_mpi_req, 0, LEAPRAID_REQUEST_SIZE);
	memcpy(ctl_sp_mpi_req, leap_mpi_req, data_sge_offset_bytes);

	if (ctl_sp_mpi_req->func == LEAPRAID_FUNC_SCSIIO ||
	    ctl_sp_mpi_req->func == LEAPRAID_FUNC_SCSIIO_RAID_PASSTHROUGH ||
	    ctl_sp_mpi_req->func == LEAPRAID_FUNC_SCSIIO_SATA_PASSTHROUGH) {
		dev_hdl = le16_to_cpu(ctl_sp_mpi_req->func_dep1);
		if (!dev_hdl ||
		    dev_hdl > adapter->adapter_attr.features.max_dev_handle) {
			dev_err(&adapter->pdev->dev,
				"%s: Invalid device handle\n", __func__);
			rc = -EINVAL;
			goto out_cleanup;
		}
	}

	if (WARN_ON(ctl_sp_mpi_req->func == LEAPRAID_FUNC_SCSI_TMF)) {
		rc = -EINVAL;
		goto out_cleanup;
	}

	if (h2c_size) {
		h2c = dma_alloc_coherent(&adapter->pdev->dev, h2c_size,
					 &h2c_dma_addr, GFP_KERNEL);
		if (!h2c) {
			rc = -ENOMEM;
			goto out_cleanup;
		}
		if (copy_from_user(h2c, karg->h2c_buf_ptr, h2c_size)) {
			dev_err(&adapter->pdev->dev,
				"%s: Failed to copy h2c from user\n",
				__func__);
			rc = -EFAULT;
			goto out_cleanup;
		}
	}
	if (c2h_size) {
		c2h = dma_alloc_coherent(&adapter->pdev->dev, c2h_size,
					 &c2h_dma_addr, GFP_KERNEL);
		if (!c2h) {
			rc = -ENOMEM;
			goto out_cleanup;
		}
	}

	psge = (void *)ctl_sp_mpi_req + data_sge_offset_bytes;
	init_completion(&adapter->driver_cmds.ctl_cmd.done);

	switch (ctl_sp_mpi_req->func) {
	case LEAPRAID_FUNC_SCSIIO:
	case LEAPRAID_FUNC_SCSIIO_RAID_PASSTHROUGH:
		leapraid_ctl_scsiio_cmd(adapter, ctl_sp_mpi_req, taskid,
					h2c_dma_addr, h2c_size,
					c2h_dma_addr, c2h_size,
					dev_hdl, psge);
		break;
	case LEAPRAID_FUNC_SMP_PASSTHROUGH:
		if (!h2c) {
			rc = -EINVAL;
			goto out_cleanup;
		}
		rc = leapraid_ctl_smp_passthrough_cmd(adapter,
						      ctl_sp_mpi_req, taskid,
						      h2c_dma_addr, h2c_size,
						      c2h_dma_addr, c2h_size,
						      psge, h2c);
		if (rc)
			goto out_cleanup;
		break;
	case LEAPRAID_FUNC_SCSIIO_SATA_PASSTHROUGH:
		leapraid_build_ieee_sg(adapter, psge, h2c_dma_addr, h2c_size,
				       c2h_dma_addr, c2h_size);
		leapraid_fire_task(adapter, taskid);
		break;
	case LEAPRAID_FUNC_FW_DOWNLOAD:
	case LEAPRAID_FUNC_FW_UPLOAD:
		leapraid_build_ieee_sg(adapter, psge, h2c_dma_addr, h2c_size,
				       c2h_dma_addr, c2h_size);
		leapraid_fire_task(adapter, taskid);
		break;
	case LEAPRAID_FUNC_SAS_IO_UNIT_CTRL:
		leapraid_ctl_sas_io_unit_ctrl_cmd(adapter, ctl_sp_mpi_req,
						  h2c_dma_addr, h2c_size,
						  c2h_dma_addr, c2h_size,
						  psge, taskid);
		break;
	default:
		leapraid_build_mpi_sg(adapter, psge, h2c_dma_addr,
				      h2c_size, c2h_dma_addr, c2h_size);
		leapraid_fire_task(adapter, taskid);
		break;
	}

	timeout = karg->timeout;
	if (timeout < LEAPRAID_CTL_CMD_TIMEOUT)
		timeout = LEAPRAID_CTL_CMD_TIMEOUT;
	if (!wait_for_completion_timeout(&adapter->driver_cmds.ctl_cmd.done,
					 timeout * HZ)) {
		dev_err(&adapter->pdev->dev,
			"%s: ctl_cmd timeout, status=0x%x\n",
			__func__, adapter->driver_cmds.ctl_cmd.status);
		leapraid_log_req_context(adapter, taskid, ctl_sp_mpi_req);
	}

	if ((leap_mpi_req->func == LEAPRAID_FUNC_SMP_PASSTHROUGH ||
	     leap_mpi_req->func == LEAPRAID_FUNC_SAS_IO_UNIT_CTRL) &&
	    adapter->reset_desc.adapter_link_resetting)
		adapter->reset_desc.adapter_link_resetting = 0;

	if (!(adapter->driver_cmds.ctl_cmd.status & LEAPRAID_CMD_DONE)) {
		issue_reset =
			leapraid_check_reset(
				adapter->driver_cmds.ctl_cmd.status);
		goto reset;
	}

	if (c2h_size && copy_to_user(karg->c2h_buf_ptr, c2h, c2h_size)) {
		dev_err(&adapter->pdev->dev,
			"%s: Failed to copy c2h to user\n", __func__);
		rc = -ENODATA;
		goto out_cleanup;
	}
	if (karg->max_rep_bytes) {
		sz = min_t(u32, karg->max_rep_bytes, LEAPRAID_REPLY_SIZE);
		if (copy_to_user(karg->rep_msg_buf_ptr,
				 &adapter->driver_cmds.ctl_cmd.reply,
				 sz)) {
			dev_err(&adapter->pdev->dev,
				"%s: Failed to copy reply to user\n",
				__func__);
			rc = -ENODATA;
			goto out_cleanup;
		}
	}

	if (karg->max_sense_bytes &&
	    (leap_mpi_req->func == LEAPRAID_FUNC_SCSIIO ||
	     leap_mpi_req->func == LEAPRAID_FUNC_SCSIIO_RAID_PASSTHROUGH)) {
		if (!karg->sense_data_ptr)
			goto out_cleanup;

		sz = min_t(u32, karg->max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
		if (copy_to_user(karg->sense_data_ptr,
				 &adapter->driver_cmds.ctl_cmd.sense,
				 sz)) {
			dev_err(&adapter->pdev->dev,
				"%s: Failed to copy sense data to user\n",
				__func__);
			rc = -ENODATA;
			goto out_cleanup;
		}
	}
reset:
	if (issue_reset) {
		rc = -ENODATA;
		if (leap_mpi_req->func == LEAPRAID_FUNC_SCSIIO ||
		    leap_mpi_req->func ==
		    LEAPRAID_FUNC_SCSIIO_RAID_PASSTHROUGH ||
		    leap_mpi_req->func ==
		    LEAPRAID_FUNC_SCSIIO_SATA_PASSTHROUGH) {
			dev_err(&adapter->pdev->dev,
				"Fire tgt reset, hdl=0x%04x\n",
				le16_to_cpu(leap_mpi_req->func_dep1));
			leapraid_issue_locked_tm(
				adapter,
				le16_to_cpu(leap_mpi_req->func_dep1), 0, 0, 0,
				LEAPRAID_TM_TASKTYPE_TARGET_RESET, taskid,
				LEAPRAID_TM_MSGFLAGS_LINK_RESET);
		} else {
			dev_info(&adapter->pdev->dev,
				 "%s:%d: call hard_reset\n",
				 __func__, __LINE__);
			leapraid_hard_reset_handler(adapter, FULL_RESET);
		}
	}
out_cleanup:
	if (c2h)
		dma_free_coherent(&adapter->pdev->dev, c2h_size,
				  c2h, c2h_dma_addr);
	if (h2c)
		dma_free_coherent(&adapter->pdev->dev, h2c_size,
				  h2c, h2c_dma_addr);
	kfree(leap_mpi_req);
	adapter->driver_cmds.ctl_cmd.status = LEAPRAID_CMD_NOT_USED;
	return rc;
}

static int leapraid_ctl_get_adapter_info(struct leapraid_adapter *adapter,
					 void __user *arg)
{
	struct leapraid_ioctl_adapter_info *karg;
	u8 revision;
	int rc = 0;

	karg = kzalloc_obj(*karg);
	if (!karg)
		return -ENOMEM;

	pci_read_config_byte(adapter->pdev, PCI_CLASS_REVISION, &revision);
	karg->revision = revision;
	karg->pci_id = adapter->pdev->device;
	karg->sub_dev = adapter->pdev->subsystem_device;
	karg->sub_vendor = adapter->pdev->subsystem_vendor;
	karg->pci_info.u.bits.bus = adapter->pdev->bus->number;
	karg->pci_info.u.bits.dev = PCI_SLOT(adapter->pdev->devfn);
	karg->pci_info.u.bits.func = PCI_FUNC(adapter->pdev->devfn);
	karg->pci_info.seg_id = pci_domain_nr(adapter->pdev->bus);
	karg->fw_ver = adapter->adapter_attr.features.fw_version;

	snprintf(karg->driver_ver, sizeof(karg->driver_ver), "%s-%s",
		 LEAPRAID_DRIVER_NAME, LEAPRAID_DRIVER_VERSION);

	karg->adapter_type = LEAPRAID_IOCTL_VERSION;
	karg->bios_ver = adapter->adapter_attr.bios_version;
	if (copy_to_user(arg, karg,
			 sizeof(struct leapraid_ioctl_adapter_info))) {
		dev_err(&adapter->pdev->dev,
			"%s: Failed to copy info to user\n", __func__);
		rc = -EFAULT;
		goto free_buf;
	}

free_buf:
	kfree(karg);
	return rc;
}

static int leapraid_ctl_ioctl_main(struct file *file, unsigned int cmd,
				   void __user *arg)
{
	struct leapraid_ioctl_header ioctl_header;
	struct leapraid_adapter *adapter = NULL;
	struct leapraid_ioctl_command __user *uarg;
	struct leapraid_ioctl_command karg;
	int rc = -ENOIOCTLCMD;

	if (copy_from_user(&ioctl_header, arg,
			   sizeof(struct leapraid_ioctl_header))) {
		pr_err("%s:%s: Failed to copy header from user\n",
		       LEAPRAID_DRIVER_NAME, __func__);
		return -EFAULT;
	}

	adapter = leapraid_ctl_lookup_adapter(ioctl_header.adapter_id);
	if (!adapter)
		return -EFAULT;

	if (atomic_read(&adapter->overheat_desc.thermal_alert)) {
		dev_warn(&adapter->pdev->dev,
			 "%s: Failed, thermal_alert=%d\n",
			 __func__,
			 atomic_read(&adapter->overheat_desc.thermal_alert));
		rc = -EFAULT;
		goto out_put;
	}

	mutex_lock(&adapter->access_ctrl.pci_access_lock);

	rc = leapraid_check_adapter_is_op(adapter, LEAPRAID_DB_WAIT_OP_LONG,
					  __func__);
	if (rc)
		goto unlock;

	if (!wait_event_timeout(adapter->access_ctrl.shost_recover_wq,
				!adapter->access_ctrl.shost_recover_async,
				LEAPRAID_WAIT_SHOST_RECOVERY * HZ)) {
		dev_warn(&adapter->pdev->dev,
			 "Timeout waiting for shost recovery async\n");
		rc = -EAGAIN;
		goto unlock;
	}

	if (adapter->access_ctrl.pcie_recovering ||
	    adapter->scan_dev_desc.driver_loading ||
	    adapter->access_ctrl.host_removing) {
		rc = -EAGAIN;
		goto unlock;
	}

	if (file->f_flags & O_NONBLOCK) {
		if (!mutex_trylock(&adapter->driver_cmds.ctl_cmd.mutex)) {
			rc = -EAGAIN;
			goto unlock;
		}
	} else if (mutex_lock_interruptible(&adapter->driver_cmds
					    .ctl_cmd.mutex)) {
		rc = -ERESTARTSYS;
		goto unlock;
	}

	switch (_IOC_NR(cmd)) {
	case LEAPRAID_ADAPTER_INFO:
		if (_IOC_SIZE(cmd) ==
		    sizeof(struct leapraid_ioctl_adapter_info))
			rc = leapraid_ctl_get_adapter_info(adapter, arg);
		break;
	case LEAPRAID_COMMAND:
		if (copy_from_user(&karg, arg, sizeof(karg))) {
			dev_err(&adapter->pdev->dev,
				"%s: Failed to copy data from user\n",
				__func__);
			rc = -EFAULT;
			break;
		}

		if (karg.hdr.adapter_id != ioctl_header.adapter_id) {
			rc = -EINVAL;
			break;
		}

		if (_IOC_SIZE(cmd) == sizeof(struct leapraid_ioctl_command)) {
			uarg = arg;
			rc = leapraid_ctl_do_command(adapter, &karg,
						     &uarg->mf);
			if (rc)
				dev_warn(&adapter->pdev->dev,
					 "%s: IOCTL cmd failed rc=%d\n",
					 __func__, rc);
		}
		break;
	case LEAPRAID_EVENTQUERY:
	case LEAPRAID_EVENTREPORT:
		rc = 0;
		break;
	default:
		dev_err(&adapter->pdev->dev,
			"Unknown IOCTL opcode=0x%08x\n", cmd);
		break;
	}
	mutex_unlock(&adapter->driver_cmds.ctl_cmd.mutex);

unlock:
	mutex_unlock(&adapter->access_ctrl.pci_access_lock);
out_put:
	leapraid_ctl_put_adapter(adapter);
	return rc;
}

static long leapraid_ctl_ioctl(struct file *file, unsigned int cmd,
			       unsigned long arg)
{
	return leapraid_ctl_ioctl_main(file, cmd, (void __user *)arg);
}

static void leapraid_fw_mmap_open(struct vm_area_struct *vma)
{
	struct leapraid_adapter *adapter = vma->vm_private_data;

	if (!adapter)
		return;

	atomic_inc(&adapter->fw_log_desc.mmap_refcnt);
}

static void leapraid_fw_mmap_close(struct vm_area_struct *vma)
{
	struct leapraid_adapter *adapter = vma->vm_private_data;

	if (!adapter)
		return;

	if (atomic_dec_and_test(&adapter->fw_log_desc.mmap_refcnt))
		wake_up(&adapter->fw_log_desc.mmap_waitq);
	leapraid_ctl_put_adapter(adapter);
}

static const struct vm_operations_struct leapraid_fw_mmap_vm_ops = {
	.open = leapraid_fw_mmap_open,
	.close = leapraid_fw_mmap_close,
};

static int leapraid_fw_mmap(struct file *filp, struct vm_area_struct *vma)
{
	struct leapraid_adapter *adapter = NULL;
	/* Userspace passes the adapter ID via vma->vm_pgoff. */
	u32 adapter_id = vma->vm_pgoff;
	unsigned long length;
	int rc = -EINVAL;

	length = vma->vm_end - vma->vm_start;

	adapter = leapraid_ctl_lookup_adapter(adapter_id);
	if (!adapter) {
		pr_err("%s: No adapter found!\n", __func__);
		return -EINVAL;
	}

	if (READ_ONCE(adapter->access_ctrl.host_removing)) {
		rc = -EAGAIN;
		goto out_put;
	}

	if (length > (LEAPRAID_SYS_LOG_BUF_SIZE +
		      LEAPRAID_SYS_LOG_BUF_RESERVE)) {
		dev_err(&adapter->pdev->dev,
			"Requested mapping size is too large!\n");
		rc = -EINVAL;
		goto out_put;
	}

	if (!adapter->fw_log_desc.fw_log_buffer) {
		dev_err(&adapter->pdev->dev, "No log buffer!\n");
		rc = -EINVAL;
		goto out_put;
	}

	vma->vm_pgoff = 0;

	rc = dma_mmap_coherent(&adapter->pdev->dev, vma,
			       adapter->fw_log_desc.fw_log_buffer,
			       adapter->fw_log_desc.fw_log_buffer_dma,
			       length);
	if (rc) {
		dev_err(&adapter->pdev->dev,
			"Failed to map memory to user space!\n");
		goto out_put;
	}

	vma->vm_private_data = adapter;
	vma->vm_ops = &leapraid_fw_mmap_vm_ops;
	leapraid_fw_mmap_open(vma);
	adapter = NULL;

	rc = 0;
out_put:
	leapraid_ctl_put_adapter(adapter);
	return rc;
}

static const struct file_operations leapraid_ctl_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = leapraid_ctl_ioctl,
	.mmap = leapraid_fw_mmap,
};

static struct miscdevice leapraid_ctl_dev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = LEAPRAID_DEV_NAME,
	.fops = &leapraid_ctl_fops,
};

int leapraid_ctl_init(void)
{
	if (misc_register(&leapraid_ctl_dev) < 0) {
		pr_err("%s Can't register misc device\n",
		       LEAPRAID_DRIVER_NAME);
		return -ENODEV;
	}
	return 0;
}

void leapraid_ctl_exit(void)
{
	misc_deregister(&leapraid_ctl_dev);
}