1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Driver for Realtek PCI-Express card reader
4  *
5  * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
6  *
7  * Author:
8  *   Wei WANG (wei_wang@realsil.com.cn)
9  *   Micky Ching (micky_ching@realsil.com.cn)
10  */
11 
12 #ifndef __REALTEK_RTSX_H
13 #define __REALTEK_RTSX_H
14 
15 #include <linux/io.h>
16 #include <linux/bitops.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/slab.h>
23 #include <linux/pci.h>
24 #include <linux/mutex.h>
25 #include <linux/cdrom.h>
26 #include <linux/workqueue.h>
27 #include <linux/timer.h>
28 #include <linux/time64.h>
29 
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_devinfo.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_host.h>
36 
37 #define CR_DRIVER_NAME		"rts5208"
38 
39 /*
40  * macros for easy use
41  */
42 #define rtsx_writel(chip, reg, value) \
43 	iowrite32(value, (chip)->rtsx->remap_addr + reg)
44 #define rtsx_readl(chip, reg) \
45 	ioread32((chip)->rtsx->remap_addr + reg)
46 #define rtsx_writew(chip, reg, value) \
47 	iowrite16(value, (chip)->rtsx->remap_addr + reg)
48 #define rtsx_readw(chip, reg) \
49 	ioread16((chip)->rtsx->remap_addr + reg)
50 #define rtsx_writeb(chip, reg, value) \
51 	iowrite8(value, (chip)->rtsx->remap_addr + reg)
52 #define rtsx_readb(chip, reg) \
53 	ioread8((chip)->rtsx->remap_addr + reg)
54 
55 #define rtsx_read_config_byte(chip, where, val) \
56 	pci_read_config_byte((chip)->rtsx->pci, where, val)
57 
58 #define rtsx_write_config_byte(chip, where, val) \
59 	pci_write_config_byte((chip)->rtsx->pci, where, val)
60 
61 #define wait_timeout_x(task_state, msecs)	\
62 do {						\
63 	set_current_state((task_state));	\
64 	schedule_timeout((msecs) * HZ / 1000);	\
65 } while (0)
66 #define wait_timeout(msecs)	wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
67 
68 #define STATE_TRANS_NONE	0
69 #define STATE_TRANS_CMD		1
70 #define STATE_TRANS_BUF		2
71 #define STATE_TRANS_SG		3
72 
73 #define TRANS_NOT_READY		0
74 #define TRANS_RESULT_OK		1
75 #define TRANS_RESULT_FAIL	2
76 
77 #define SCSI_LUN(srb)		((srb)->device->lun)
78 
79 struct rtsx_chip;
80 
81 struct rtsx_dev {
82 	struct pci_dev *pci;
83 
84 	/* pci resources */
85 	unsigned long		addr;
86 	void __iomem		*remap_addr;
87 	int irq;
88 
89 	/* locks */
90 	spinlock_t		reg_lock;
91 
92 	struct task_struct	*ctl_thread;	 /* the control thread   */
93 	struct task_struct	*polling_thread; /* the polling thread   */
94 
95 	/* mutual exclusion and synchronization structures */
96 	struct completion	cmnd_ready;	 /* to sleep thread on	    */
97 	struct completion	control_exit;	 /* control thread exit	    */
98 	struct completion	polling_exit;	 /* polling thread exit	    */
99 	struct completion	notify;		 /* thread begin/end	    */
100 	struct completion	scanning_done;	 /* wait for scan thread    */
101 
102 	wait_queue_head_t	delay_wait;	 /* wait during scan, reset */
103 	struct mutex		dev_mutex;
104 
105 	/* host reserved buffer */
106 	void			*rtsx_resv_buf;
107 	dma_addr_t		rtsx_resv_buf_addr;
108 
109 	char			trans_result;
110 	char			trans_state;
111 
112 	struct completion	*done;
113 	/* Whether interrupt handler should care card cd info */
114 	u32			check_card_cd;
115 
116 	struct rtsx_chip	*chip;
117 };
118 
119 /* Convert between rtsx_dev and the corresponding Scsi_Host */
rtsx_to_host(struct rtsx_dev * dev)120 static inline struct Scsi_Host *rtsx_to_host(struct rtsx_dev *dev)
121 {
122 	return container_of((void *)dev, struct Scsi_Host, hostdata);
123 }
124 
host_to_rtsx(struct Scsi_Host * host)125 static inline struct rtsx_dev *host_to_rtsx(struct Scsi_Host *host)
126 {
127 	return (struct rtsx_dev *)host->hostdata;
128 }
129 
130 /*
131  * The scsi_lock() and scsi_unlock() macros protect the sm_state and the
132  * single queue element srb for write access
133  */
134 #define scsi_unlock(host)	spin_unlock_irq(host->host_lock)
135 #define scsi_lock(host)		spin_lock_irq(host->host_lock)
136 
137 #define lock_state(chip)	spin_lock_irq(&((chip)->rtsx->reg_lock))
138 #define unlock_state(chip)	spin_unlock_irq(&((chip)->rtsx->reg_lock))
139 
140 /* struct scsi_cmnd transfer buffer access utilities */
141 enum xfer_buf_dir	{TO_XFER_BUF, FROM_XFER_BUF};
142 
143 #include "rtsx_chip.h"
144 #include "rtsx_transport.h"
145 #include "rtsx_scsi.h"
146 #include "rtsx_card.h"
147 #include "rtsx_sys.h"
148 #include "general.h"
149 
150 #endif  /* __REALTEK_RTSX_H */
151