1 /* Driver for Realtek PCI-Express card reader
2  * Header file
3  *
4  * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2, or (at your option) any
9  * later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, see <http://www.gnu.org/licenses/>.
18  *
19  * Author:
20  *   wwang (wei_wang@realsil.com.cn)
21  *   No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
22  */
23 
24 #ifndef __REALTEK_RTSX_H
25 #define __REALTEK_RTSX_H
26 
27 #include <linux/io.h>
28 #include <linux/bitops.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/pci.h>
37 #include <linux/mutex.h>
38 #include <linux/cdrom.h>
39 #include <linux/workqueue.h>
40 #include <linux/timer.h>
41 #include <linux/time.h>
42 
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_device.h>
46 #include <scsi/scsi_devinfo.h>
47 #include <scsi/scsi_eh.h>
48 #include <scsi/scsi_host.h>
49 
50 #include "debug.h"
51 #include "trace.h"
52 #include "general.h"
53 
54 #define CR_DRIVER_NAME		"rts_pstor"
55 
56 #define pci_get_bus_and_slot(bus, devfn)	\
57 	pci_get_domain_bus_and_slot(0, (bus), (devfn))
58 
59 /*
60  * macros for easy use
61  */
62 #define rtsx_writel(chip, reg, value) \
63 	iowrite32(value, (chip)->rtsx->remap_addr + reg)
64 #define rtsx_readl(chip, reg) \
65 	ioread32((chip)->rtsx->remap_addr + reg)
66 #define rtsx_writew(chip, reg, value) \
67 	iowrite16(value, (chip)->rtsx->remap_addr + reg)
68 #define rtsx_readw(chip, reg) \
69 	ioread16((chip)->rtsx->remap_addr + reg)
70 #define rtsx_writeb(chip, reg, value) \
71 	iowrite8(value, (chip)->rtsx->remap_addr + reg)
72 #define rtsx_readb(chip, reg) \
73 	ioread8((chip)->rtsx->remap_addr + reg)
74 
75 #define rtsx_read_config_byte(chip, where, val) \
76 	pci_read_config_byte((chip)->rtsx->pci, where, val)
77 
78 #define rtsx_write_config_byte(chip, where, val) \
79 	pci_write_config_byte((chip)->rtsx->pci, where, val)
80 
81 #define wait_timeout_x(task_state, msecs)		\
82 do {							\
83 		set_current_state((task_state));	\
84 		schedule_timeout((msecs) * HZ / 1000);	\
85 } while (0)
86 #define wait_timeout(msecs)	wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
87 
88 
89 #define STATE_TRANS_NONE	0
90 #define STATE_TRANS_CMD		1
91 #define STATE_TRANS_BUF		2
92 #define STATE_TRANS_SG		3
93 
94 #define TRANS_NOT_READY		0
95 #define TRANS_RESULT_OK		1
96 #define TRANS_RESULT_FAIL	2
97 
98 #define SCSI_LUN(srb)		((srb)->device->lun)
99 
100 typedef unsigned long DELAY_PARA_T;
101 
102 struct rtsx_chip;
103 
104 struct rtsx_dev {
105 	struct pci_dev *pci;
106 
107 	/* pci resources */
108 	unsigned long 		addr;
109 	void __iomem 		*remap_addr;
110 	int irq;
111 
112 	/* locks */
113 	spinlock_t 		reg_lock;
114 
115 	struct task_struct	*ctl_thread;	 /* the control thread   */
116 	struct task_struct	*polling_thread; /* the polling thread   */
117 
118 	/* mutual exclusion and synchronization structures */
119 	struct completion	cmnd_ready;	 /* to sleep thread on	    */
120 	struct completion	control_exit;	 /* control thread exit	    */
121 	struct completion	polling_exit;	 /* polling thread exit	    */
122 	struct completion	notify;		 /* thread begin/end	    */
123 	struct completion	scanning_done;	 /* wait for scan thread    */
124 
125 	wait_queue_head_t	delay_wait;	 /* wait during scan, reset */
126 	struct mutex		dev_mutex;
127 
128 	/* host reserved buffer */
129 	void 			*rtsx_resv_buf;
130 	dma_addr_t 		rtsx_resv_buf_addr;
131 
132 	char			trans_result;
133 	char			trans_state;
134 
135 	struct completion 	*done;
136 	/* Whether interrupt handler should care card cd info */
137 	u32 			check_card_cd;
138 
139 	struct rtsx_chip 	*chip;
140 };
141 
142 typedef struct rtsx_dev rtsx_dev_t;
143 
144 /* Convert between rtsx_dev and the corresponding Scsi_Host */
rtsx_to_host(struct rtsx_dev * dev)145 static inline struct Scsi_Host *rtsx_to_host(struct rtsx_dev *dev)
146 {
147 	return container_of((void *) dev, struct Scsi_Host, hostdata);
148 }
host_to_rtsx(struct Scsi_Host * host)149 static inline struct rtsx_dev *host_to_rtsx(struct Scsi_Host *host)
150 {
151 	return (struct rtsx_dev *) host->hostdata;
152 }
153 
get_current_time(u8 * timeval_buf,int buf_len)154 static inline void get_current_time(u8 *timeval_buf, int buf_len)
155 {
156 	struct timeval tv;
157 
158 	if (!timeval_buf || (buf_len < 8))
159 		return;
160 
161 	do_gettimeofday(&tv);
162 
163 	timeval_buf[0] = (u8)(tv.tv_sec >> 24);
164 	timeval_buf[1] = (u8)(tv.tv_sec >> 16);
165 	timeval_buf[2] = (u8)(tv.tv_sec >> 8);
166 	timeval_buf[3] = (u8)(tv.tv_sec);
167 	timeval_buf[4] = (u8)(tv.tv_usec >> 24);
168 	timeval_buf[5] = (u8)(tv.tv_usec >> 16);
169 	timeval_buf[6] = (u8)(tv.tv_usec >> 8);
170 	timeval_buf[7] = (u8)(tv.tv_usec);
171 }
172 
173 /* The scsi_lock() and scsi_unlock() macros protect the sm_state and the
174  * single queue element srb for write access */
175 #define scsi_unlock(host)	spin_unlock_irq(host->host_lock)
176 #define scsi_lock(host)		spin_lock_irq(host->host_lock)
177 
178 #define lock_state(chip)	spin_lock_irq(&((chip)->rtsx->reg_lock))
179 #define unlock_state(chip)	spin_unlock_irq(&((chip)->rtsx->reg_lock))
180 
181 /* struct scsi_cmnd transfer buffer access utilities */
182 enum xfer_buf_dir	{TO_XFER_BUF, FROM_XFER_BUF};
183 
184 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val);
185 
186 #endif  /* __REALTEK_RTSX_H */
187