blob: 2e101da8322081371ac98be551df933ef3b400f7 [file] [log] [blame]
Kim Bradleybb1e6772018-10-22 21:10:26 +01001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Driver for Realtek PCI-Express card reader
Micky Chingfa590c22013-11-12 17:16:08 +08004 *
5 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
6 *
Micky Chingfa590c22013-11-12 17:16:08 +08007 * 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>
Micky Chingfa590c22013-11-12 17:16:08 +080022#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>
Ksenija Stanojevicd5a7d452015-04-30 19:00:23 +020028#include <linux/time64.h>
Micky Chingfa590c22013-11-12 17:16:08 +080029
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
Micky Chingfa590c22013-11-12 17:16:08 +080037#define CR_DRIVER_NAME "rts5208"
38
Micky Chingfa590c22013-11-12 17:16:08 +080039/*
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
Rehas Sachdevabd778ef2016-09-20 16:45:33 +053061#define wait_timeout_x(task_state, msecs) \
62do { \
63 set_current_state((task_state)); \
64 schedule_timeout((msecs) * HZ / 1000); \
Micky Chingfa590c22013-11-12 17:16:08 +080065} while (0)
66#define wait_timeout(msecs) wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
67
Micky Chingfa590c22013-11-12 17:16:08 +080068#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
Micky Chingfa590c22013-11-12 17:16:08 +080079struct rtsx_chip;
80
81struct 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
Micky Chingfa590c22013-11-12 17:16:08 +0800119/* Convert between rtsx_dev and the corresponding Scsi_Host */
120static inline struct Scsi_Host *rtsx_to_host(struct rtsx_dev *dev)
121{
Rehas Sachdeva7c7f2362016-09-20 16:46:24 +0530122 return container_of((void *)dev, struct Scsi_Host, hostdata);
Micky Chingfa590c22013-11-12 17:16:08 +0800123}
Jonas Rickertd5b7d2b2016-07-30 13:27:58 +0200124
Micky Chingfa590c22013-11-12 17:16:08 +0800125static inline struct rtsx_dev *host_to_rtsx(struct Scsi_Host *host)
126{
Rehas Sachdeva7c7f2362016-09-20 16:46:24 +0530127 return (struct rtsx_dev *)host->hostdata;
Micky Chingfa590c22013-11-12 17:16:08 +0800128}
129
Sergio Paracuellos55e94cc2016-09-23 16:12:10 +0200130/*
131 * The scsi_lock() and scsi_unlock() macros protect the sm_state and the
132 * single queue element srb for write access
133 */
Micky Chingfa590c22013-11-12 17:16:08 +0800134#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 */
141enum xfer_buf_dir {TO_XFER_BUF, FROM_XFER_BUF};
142
Joe Perches031366e2015-03-25 12:54:25 -0700143#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
Micky Chingfa590c22013-11-12 17:16:08 +0800150#endif /* __REALTEK_RTSX_H */