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 <asm/io.h>
28 #include <asm/bitops.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/kernel.h>
32 #include <linux/version.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/pci.h>
38 #include <linux/mutex.h>
39 #include <linux/cdrom.h>
40 #include <linux/workqueue.h>
41 #include <linux/timer.h>
42 #include <linux/time.h>
43 
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <scsi/scsi_device.h>
47 #include <scsi/scsi_devinfo.h>
48 #include <scsi/scsi_eh.h>
49 #include <scsi/scsi_host.h>
50 
51 #include "debug.h"
52 #include "trace.h"
53 #include "general.h"
54 
55 #define CR_DRIVER_NAME		"rts_pstor"
56 
57 #define pci_get_bus_and_slot(bus, devfn)	\
58 	pci_get_domain_bus_and_slot(0, (bus), (devfn))
59 
60 /*
61  * macros for easy use
62  */
63 #define rtsx_writel(chip, reg, value) \
64 	iowrite32(value, (chip)->rtsx->remap_addr + reg)
65 #define rtsx_readl(chip, reg) \
66 	ioread32((chip)->rtsx->remap_addr + reg)
67 #define rtsx_writew(chip, reg, value) \
68 	iowrite16(value, (chip)->rtsx->remap_addr + reg)
69 #define rtsx_readw(chip, reg) \
70 	ioread16((chip)->rtsx->remap_addr + reg)
71 #define rtsx_writeb(chip, reg, value) \
72 	iowrite8(value, (chip)->rtsx->remap_addr + reg)
73 #define rtsx_readb(chip, reg) \
74 	ioread8((chip)->rtsx->remap_addr + reg)
75 
76 #define rtsx_read_config_byte(chip, where, val) \
77 	pci_read_config_byte((chip)->rtsx->pci, where, val)
78 
79 #define rtsx_write_config_byte(chip, where, val) \
80 	pci_write_config_byte((chip)->rtsx->pci, where, val)
81 
82 #define wait_timeout_x(task_state, msecs)		\
83 do {							\
84 		set_current_state((task_state)); 	\
85 		schedule_timeout((msecs) * HZ / 1000);	\
86 } while (0)
87 #define wait_timeout(msecs)	wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
88 
89 
90 #define STATE_TRANS_NONE	0
91 #define STATE_TRANS_CMD		1
92 #define STATE_TRANS_BUF		2
93 #define STATE_TRANS_SG		3
94 
95 #define TRANS_NOT_READY		0
96 #define TRANS_RESULT_OK		1
97 #define TRANS_RESULT_FAIL	2
98 
99 #define SCSI_LUN(srb)		((srb)->device->lun)
100 
101 #define rtsx_alloc_dma_buf(chip, size, flag)	kmalloc((size), (flag))
102 #define rtsx_free_dma_buf(chip, ptr)		kfree((ptr))
103 
104 typedef unsigned long DELAY_PARA_T;
105 
106 struct rtsx_chip;
107 
108 struct rtsx_dev {
109 	struct pci_dev 		*pci;
110 
111 	/* pci resources */
112 	unsigned long 		addr;
113 	void __iomem 		*remap_addr;
114 	int 			irq;
115 
116 	/* locks */
117 	spinlock_t 		reg_lock;
118 
119 	/* mutual exclusion and synchronization structures */
120 	struct semaphore	sema;		 /* to sleep thread on	    */
121 	struct completion	notify;		 /* thread begin/end	    */
122 	wait_queue_head_t	delay_wait;	 /* wait during scan, reset */
123 	struct mutex		dev_mutex;
124 
125 	/* host reserved buffer */
126 	void 			*rtsx_resv_buf;
127 	dma_addr_t 		rtsx_resv_buf_addr;
128 
129 	char			trans_result;
130 	char			trans_state;
131 
132 	struct completion 	*done;
133 	/* Whether interrupt handler should care card cd info */
134 	u32 			check_card_cd;
135 
136 	struct rtsx_chip 	*chip;
137 };
138 
139 typedef struct rtsx_dev rtsx_dev_t;
140 
141 /* Convert between rtsx_dev and the corresponding Scsi_Host */
rtsx_to_host(struct rtsx_dev * dev)142 static inline struct Scsi_Host *rtsx_to_host(struct rtsx_dev *dev)
143 {
144 	return container_of((void *) dev, struct Scsi_Host, hostdata);
145 }
host_to_rtsx(struct Scsi_Host * host)146 static inline struct rtsx_dev *host_to_rtsx(struct Scsi_Host *host)
147 {
148 	return (struct rtsx_dev *) host->hostdata;
149 }
150 
get_current_time(u8 * timeval_buf,int buf_len)151 static inline void get_current_time(u8 *timeval_buf, int buf_len)
152 {
153 	struct timeval tv;
154 
155 	if (!timeval_buf || (buf_len < 8))
156 		return;
157 
158 	do_gettimeofday(&tv);
159 
160 	timeval_buf[0] = (u8)(tv.tv_sec >> 24);
161 	timeval_buf[1] = (u8)(tv.tv_sec >> 16);
162 	timeval_buf[2] = (u8)(tv.tv_sec >> 8);
163 	timeval_buf[3] = (u8)(tv.tv_sec);
164 	timeval_buf[4] = (u8)(tv.tv_usec >> 24);
165 	timeval_buf[5] = (u8)(tv.tv_usec >> 16);
166 	timeval_buf[6] = (u8)(tv.tv_usec >> 8);
167 	timeval_buf[7] = (u8)(tv.tv_usec);
168 }
169 
170 /* The scsi_lock() and scsi_unlock() macros protect the sm_state and the
171  * single queue element srb for write access */
172 #define scsi_unlock(host)	spin_unlock_irq(host->host_lock)
173 #define scsi_lock(host)		spin_lock_irq(host->host_lock)
174 
175 #define lock_state(chip)	spin_lock_irq(&((chip)->rtsx->reg_lock))
176 #define unlock_state(chip)	spin_unlock_irq(&((chip)->rtsx->reg_lock))
177 
178 /* struct scsi_cmnd transfer buffer access utilities */
179 enum xfer_buf_dir	{TO_XFER_BUF, FROM_XFER_BUF};
180 
181 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val);
182 
183 #endif  /* __REALTEK_RTSX_H */
184