1 /* Driver for Realtek PCI-Express card reader
3 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 * Wei WANG (wei_wang@realsil.com.cn)
20 * Micky Ching (micky_ching@realsil.com.cn)
23 #include <linux/blkdev.h>
24 #include <linux/kthread.h>
25 #include <linux/sched.h>
26 #include <linux/workqueue.h>
29 #include "rtsx_chip.h"
30 #include "rtsx_transport.h"
31 #include "rtsx_scsi.h"
32 #include "rtsx_card.h"
39 MODULE_DESCRIPTION("Realtek PCI-Express card reader rts5208/rts5288 driver");
40 MODULE_LICENSE("GPL");
42 static unsigned int delay_use = 1;
43 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
44 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
47 module_param(ss_en, int, S_IRUGO | S_IWUSR);
48 MODULE_PARM_DESC(ss_en, "enable selective suspend");
50 static int ss_interval = 50;
51 module_param(ss_interval, int, S_IRUGO | S_IWUSR);
52 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
54 static int auto_delink_en;
55 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
58 static unsigned char aspm_l0s_l1_en;
59 module_param(aspm_l0s_l1_en, byte, S_IRUGO | S_IWUSR);
60 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
63 module_param(msi_en, int, S_IRUGO | S_IWUSR);
64 MODULE_PARM_DESC(msi_en, "enable msi");
66 static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
68 /***********************************************************************
70 ***********************************************************************/
72 static const char *host_info(struct Scsi_Host *host)
74 return "SCSI emulation for PCI-Express Mass Storage devices";
77 static int slave_alloc(struct scsi_device *sdev)
80 * Set the INQUIRY transfer length to 36. We don't use any of
81 * the extra data and many devices choke if asked for more or
84 sdev->inquiry_len = 36;
88 static int slave_configure(struct scsi_device *sdev)
90 /* Scatter-gather buffers (all but the last) must have a length
91 * divisible by the bulk maxpacket size. Otherwise a data packet
92 * would end up being short, causing a premature end to the data
93 * transfer. Since high-speed bulk pipes have a maxpacket size
94 * of 512, we'll use that as the scsi device queue's DMA alignment
95 * mask. Guaranteeing proper alignment of the first buffer will
96 * have the desired effect because, except at the beginning and
97 * the end, scatter-gather buffers follow page boundaries. */
98 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
100 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
101 * what is originally reported. We need this to avoid confusing
102 * the SCSI layer with devices that report 0 or 1, but need 10-byte
103 * commands (ala ATAPI devices behind certain bridges, or devices
104 * which simply have broken INQUIRY data).
106 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
107 * actual information. This seems to be the preference for
108 * programs like that.
110 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
111 * the actual value or the modified one, depending on where the
114 if (sdev->scsi_level < SCSI_2)
115 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
121 /***********************************************************************
122 * /proc/scsi/ functions
123 ***********************************************************************/
125 /* we use this macro to help us write into the buffer */
127 #define SPRINTF(args...) \
128 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
130 /* queue a command */
131 /* This is always called with scsi_lock(host) held */
132 static int queuecommand_lck(struct scsi_cmnd *srb,
133 void (*done)(struct scsi_cmnd *))
135 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
136 struct rtsx_chip *chip = dev->chip;
138 /* check for state-transition errors */
139 if (chip->srb != NULL) {
140 dev_err(&dev->pci->dev, "Error in %s: chip->srb = %p\n",
141 __func__, chip->srb);
142 return SCSI_MLQUEUE_HOST_BUSY;
145 /* fail the command if we are disconnecting */
146 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
147 dev_info(&dev->pci->dev, "Fail command during disconnect\n");
148 srb->result = DID_NO_CONNECT << 16;
153 /* enqueue the command and wake up the control thread */
154 srb->scsi_done = done;
156 complete(&dev->cmnd_ready);
161 static DEF_SCSI_QCMD(queuecommand)
163 /***********************************************************************
164 * Error handling functions
165 ***********************************************************************/
167 /* Command timeout and abort */
168 static int command_abort(struct scsi_cmnd *srb)
170 struct Scsi_Host *host = srb->device->host;
171 struct rtsx_dev *dev = host_to_rtsx(host);
172 struct rtsx_chip *chip = dev->chip;
174 dev_info(&dev->pci->dev, "%s called\n", __func__);
178 /* Is this command still active? */
179 if (chip->srb != srb) {
181 dev_info(&dev->pci->dev, "-- nothing to abort\n");
185 rtsx_set_stat(chip, RTSX_STAT_ABORT);
189 /* Wait for the aborted command to finish */
190 wait_for_completion(&dev->notify);
195 /* This invokes the transport reset mechanism to reset the state of the
197 static int device_reset(struct scsi_cmnd *srb)
200 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
202 dev_info(&dev->pci->dev, "%s called\n", __func__);
204 return result < 0 ? FAILED : SUCCESS;
207 /* Simulate a SCSI bus reset by resetting the device's USB port. */
208 static int bus_reset(struct scsi_cmnd *srb)
211 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
213 dev_info(&dev->pci->dev, "%s called\n", __func__);
215 return result < 0 ? FAILED : SUCCESS;
220 * this defines our host template, with which we'll allocate hosts
223 static struct scsi_host_template rtsx_host_template = {
224 /* basic userland interface stuff */
225 .name = CR_DRIVER_NAME,
226 .proc_name = CR_DRIVER_NAME,
229 /* command interface -- queued only */
230 .queuecommand = queuecommand,
232 /* error and abort handlers */
233 .eh_abort_handler = command_abort,
234 .eh_device_reset_handler = device_reset,
235 .eh_bus_reset_handler = bus_reset,
237 /* queue commands only, only one command per LUN */
241 /* unknown initiator id */
244 .slave_alloc = slave_alloc,
245 .slave_configure = slave_configure,
247 /* lots of sg segments can be handled */
248 .sg_tablesize = SG_ALL,
250 /* limit the total size of a transfer to 120 KB */
253 /* merge commands... this seems to help performance, but
254 * periodically someone should test to see which setting is more
262 /* we do our own delay after a device or bus reset */
263 .skip_settle_delay = 1,
265 /* module management */
266 .module = THIS_MODULE
270 static int rtsx_acquire_irq(struct rtsx_dev *dev)
272 struct rtsx_chip *chip = dev->chip;
274 dev_info(&dev->pci->dev, "%s: chip->msi_en = %d, pci->irq = %d\n",
275 __func__, chip->msi_en, dev->pci->irq);
277 if (request_irq(dev->pci->irq, rtsx_interrupt,
278 chip->msi_en ? 0 : IRQF_SHARED,
279 CR_DRIVER_NAME, dev)) {
280 dev_err(&dev->pci->dev,
281 "rtsx: unable to grab IRQ %d, disabling device\n",
286 dev->irq = dev->pci->irq;
287 pci_intx(dev->pci, !chip->msi_en);
293 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val)
295 struct pci_dev *pdev;
297 u8 devfn = (dev << 3) | func;
299 pdev = pci_get_bus_and_slot(bus, devfn);
303 pci_read_config_byte(pdev, offset, &data);
314 static int rtsx_suspend(struct pci_dev *pci, pm_message_t state)
316 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
317 struct rtsx_chip *chip;
322 /* lock the device pointers */
323 mutex_lock(&(dev->dev_mutex));
327 rtsx_do_before_power_down(chip, PM_S3);
330 synchronize_irq(dev->irq);
331 free_irq(dev->irq, (void *)dev);
336 pci_disable_msi(pci);
339 pci_enable_wake(pci, pci_choose_state(pci, state), 1);
340 pci_disable_device(pci);
341 pci_set_power_state(pci, pci_choose_state(pci, state));
343 /* unlock the device pointers */
344 mutex_unlock(&dev->dev_mutex);
349 static int rtsx_resume(struct pci_dev *pci)
351 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
352 struct rtsx_chip *chip;
359 /* lock the device pointers */
360 mutex_lock(&(dev->dev_mutex));
362 pci_set_power_state(pci, PCI_D0);
363 pci_restore_state(pci);
364 if (pci_enable_device(pci) < 0) {
365 dev_err(&dev->pci->dev,
366 "%s: pci_enable_device failed, disabling device\n",
368 /* unlock the device pointers */
369 mutex_unlock(&dev->dev_mutex);
375 if (pci_enable_msi(pci) < 0)
379 if (rtsx_acquire_irq(dev) < 0) {
380 /* unlock the device pointers */
381 mutex_unlock(&dev->dev_mutex);
385 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
386 rtsx_init_chip(chip);
388 /* unlock the device pointers */
389 mutex_unlock(&dev->dev_mutex);
393 #endif /* CONFIG_PM */
395 static void rtsx_shutdown(struct pci_dev *pci)
397 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
398 struct rtsx_chip *chip;
405 rtsx_do_before_power_down(chip, PM_S1);
408 synchronize_irq(dev->irq);
409 free_irq(dev->irq, (void *)dev);
414 pci_disable_msi(pci);
416 pci_disable_device(pci);
421 static int rtsx_control_thread(void *__dev)
423 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
424 struct rtsx_chip *chip = dev->chip;
425 struct Scsi_Host *host = rtsx_to_host(dev);
428 if (wait_for_completion_interruptible(&dev->cmnd_ready))
431 /* lock the device pointers */
432 mutex_lock(&(dev->dev_mutex));
434 /* if the device has disconnected, we are free to exit */
435 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
436 dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
437 mutex_unlock(&dev->dev_mutex);
441 /* lock access to the state */
444 /* has the command aborted ? */
445 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
446 chip->srb->result = DID_ABORT << 16;
452 /* reject the command if the direction indicator
455 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
456 dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
457 chip->srb->result = DID_ERROR << 16;
460 /* reject if target != 0 or if LUN is higher than
461 * the maximum known LUN
463 else if (chip->srb->device->id) {
464 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
465 chip->srb->device->id,
466 (u8)chip->srb->device->lun);
467 chip->srb->result = DID_BAD_TARGET << 16;
470 else if (chip->srb->device->lun > chip->max_lun) {
471 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
472 chip->srb->device->id,
473 (u8)chip->srb->device->lun);
474 chip->srb->result = DID_BAD_TARGET << 16;
477 /* we've got a command, let's do it! */
479 scsi_show_command(chip);
480 rtsx_invoke_transport(chip->srb, chip);
483 /* lock access to the state */
486 /* did the command already complete because of a disconnect? */
488 ; /* nothing to do */
490 /* indicate that the command is done */
491 else if (chip->srb->result != DID_ABORT << 16) {
492 chip->srb->scsi_done(chip->srb);
495 dev_err(&dev->pci->dev, "scsi command aborted\n");
498 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
499 complete(&(dev->notify));
501 rtsx_set_stat(chip, RTSX_STAT_IDLE);
504 /* finished working on this command */
508 /* unlock the device pointers */
509 mutex_unlock(&dev->dev_mutex);
512 /* notify the exit routine that we're actually exiting now
514 * complete()/wait_for_completion() is similar to up()/down(),
515 * except that complete() is safe in the case where the structure
516 * is getting deleted in a parallel mode of execution (i.e. just
517 * after the down() -- that's necessary for the thread-shutdown
520 * complete_and_exit() goes even further than this -- it is safe in
521 * the case that the thread of the caller is going away (not just
522 * the structure) -- this is necessary for the module-remove case.
523 * This is important in preemption kernels, which transfer the flow
524 * of execution immediately upon a complete().
526 complete_and_exit(&dev->control_exit, 0);
530 static int rtsx_polling_thread(void *__dev)
532 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
533 struct rtsx_chip *chip = dev->chip;
534 struct sd_info *sd_card = &(chip->sd_card);
535 struct xd_info *xd_card = &(chip->xd_card);
536 struct ms_info *ms_card = &(chip->ms_card);
538 sd_card->cleanup_counter = 0;
539 xd_card->cleanup_counter = 0;
540 ms_card->cleanup_counter = 0;
542 /* Wait until SCSI scan finished */
543 wait_timeout((delay_use + 5) * 1000);
547 set_current_state(TASK_INTERRUPTIBLE);
548 schedule_timeout(POLLING_INTERVAL);
550 /* lock the device pointers */
551 mutex_lock(&(dev->dev_mutex));
553 /* if the device has disconnected, we are free to exit */
554 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
555 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
556 mutex_unlock(&dev->dev_mutex);
560 mutex_unlock(&dev->dev_mutex);
562 mspro_polling_format_status(chip);
564 /* lock the device pointers */
565 mutex_lock(&(dev->dev_mutex));
567 rtsx_polling_func(chip);
569 /* unlock the device pointers */
570 mutex_unlock(&dev->dev_mutex);
573 complete_and_exit(&dev->polling_exit, 0);
579 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
581 struct rtsx_dev *dev = dev_id;
582 struct rtsx_chip *chip;
594 spin_lock(&dev->reg_lock);
596 retval = rtsx_pre_handle_interrupt(chip);
597 if (retval == STATUS_FAIL) {
598 spin_unlock(&dev->reg_lock);
599 if (chip->int_reg == 0xFFFFFFFF)
605 status = chip->int_reg;
607 if (dev->check_card_cd) {
608 if (!(dev->check_card_cd & status)) {
609 /* card not exist, return TRANS_RESULT_FAIL */
610 dev->trans_result = TRANS_RESULT_FAIL;
617 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
618 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
619 if (status & DELINK_INT)
620 RTSX_SET_DELINK(chip);
621 dev->trans_result = TRANS_RESULT_FAIL;
624 } else if (status & TRANS_OK_INT) {
625 dev->trans_result = TRANS_RESULT_OK;
628 } else if (status & DATA_DONE_INT) {
629 dev->trans_result = TRANS_NOT_READY;
630 if (dev->done && (dev->trans_state == STATE_TRANS_SG))
636 spin_unlock(&dev->reg_lock);
641 /* Release all our dynamic resources */
642 static void rtsx_release_resources(struct rtsx_dev *dev)
644 dev_info(&dev->pci->dev, "-- %s\n", __func__);
646 /* Tell the control thread to exit. The SCSI host must
647 * already have been removed so it won't try to queue
650 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
651 complete(&dev->cmnd_ready);
653 wait_for_completion(&dev->control_exit);
654 if (dev->polling_thread)
655 wait_for_completion(&dev->polling_exit);
659 if (dev->rtsx_resv_buf) {
660 dma_free_coherent(&(dev->pci->dev), RTSX_RESV_BUF_LEN,
661 dev->rtsx_resv_buf, dev->rtsx_resv_buf_addr);
662 dev->chip->host_cmds_ptr = NULL;
663 dev->chip->host_sg_tbl_ptr = NULL;
667 free_irq(dev->irq, (void *)dev);
668 if (dev->chip->msi_en)
669 pci_disable_msi(dev->pci);
671 iounmap(dev->remap_addr);
673 pci_disable_device(dev->pci);
674 pci_release_regions(dev->pci);
676 rtsx_release_chip(dev->chip);
680 /* First stage of disconnect processing: stop all commands and remove
682 static void quiesce_and_remove_host(struct rtsx_dev *dev)
684 struct Scsi_Host *host = rtsx_to_host(dev);
685 struct rtsx_chip *chip = dev->chip;
687 /* Prevent new transfers, stop the current command, and
688 * interrupt a SCSI-scan or device-reset delay */
689 mutex_lock(&dev->dev_mutex);
691 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
693 mutex_unlock(&dev->dev_mutex);
694 wake_up(&dev->delay_wait);
695 wait_for_completion(&dev->scanning_done);
697 /* Wait some time to let other threads exist */
700 /* queuecommand won't accept any new commands and the control
701 * thread won't execute a previously-queued command. If there
702 * is such a command pending, complete it with an error. */
703 mutex_lock(&dev->dev_mutex);
705 chip->srb->result = DID_NO_CONNECT << 16;
707 chip->srb->scsi_done(dev->chip->srb);
711 mutex_unlock(&dev->dev_mutex);
713 /* Now we own no commands so it's safe to remove the SCSI host */
714 scsi_remove_host(host);
717 /* Second stage of disconnect processing: deallocate all resources */
718 static void release_everything(struct rtsx_dev *dev)
720 rtsx_release_resources(dev);
722 /* Drop our reference to the host; the SCSI core will free it
723 * when the refcount becomes 0. */
724 scsi_host_put(rtsx_to_host(dev));
727 /* Thread to carry out delayed SCSI-device scanning */
728 static int rtsx_scan_thread(void *__dev)
730 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
731 struct rtsx_chip *chip = dev->chip;
733 /* Wait for the timeout to expire or for a disconnect */
735 dev_info(&dev->pci->dev,
736 "%s: waiting for device to settle before scanning\n",
738 wait_event_interruptible_timeout(dev->delay_wait,
739 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
743 /* If the device is still connected, perform the scanning */
744 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
745 scsi_scan_host(rtsx_to_host(dev));
746 dev_info(&dev->pci->dev, "%s: device scan complete\n",
749 /* Should we unbind if no devices were detected? */
752 complete_and_exit(&dev->scanning_done, 0);
755 static void rtsx_init_options(struct rtsx_chip *chip)
757 chip->vendor_id = chip->rtsx->pci->vendor;
758 chip->product_id = chip->rtsx->pci->device;
761 chip->driver_first_load = 1;
762 #ifdef HW_AUTO_SWITCH_SD_BUS
763 chip->sdio_in_charge = 0;
766 chip->mspro_formatter_enable = 1;
768 chip->use_hw_setting = 0;
769 chip->lun_mode = DEFAULT_SINGLE;
770 chip->auto_delink_en = auto_delink_en;
772 chip->ss_idle_period = ss_interval * 1000;
773 chip->remote_wakeup_en = 0;
774 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
775 chip->dynamic_aspm = 1;
776 chip->fpga_sd_sdr104_clk = CLK_200;
777 chip->fpga_sd_ddr50_clk = CLK_100;
778 chip->fpga_sd_sdr50_clk = CLK_100;
779 chip->fpga_sd_hs_clk = CLK_100;
780 chip->fpga_mmc_52m_clk = CLK_80;
781 chip->fpga_ms_hg_clk = CLK_80;
782 chip->fpga_ms_4bit_clk = CLK_80;
783 chip->fpga_ms_1bit_clk = CLK_40;
784 chip->asic_sd_sdr104_clk = 203;
785 chip->asic_sd_sdr50_clk = 98;
786 chip->asic_sd_ddr50_clk = 98;
787 chip->asic_sd_hs_clk = 98;
788 chip->asic_mmc_52m_clk = 98;
789 chip->asic_ms_hg_clk = 117;
790 chip->asic_ms_4bit_clk = 78;
791 chip->asic_ms_1bit_clk = 39;
792 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
793 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
794 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
795 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
796 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
797 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
798 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
799 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
801 chip->sd_speed_prior = 0x01040203;
802 chip->sd_current_prior = 0x00010203;
803 chip->sd_ctl = SD_PUSH_POINT_AUTO |
804 SD_SAMPLE_POINT_AUTO |
805 SUPPORT_MMC_DDR_MODE;
806 chip->sd_ddr_tx_phase = 0;
807 chip->mmc_ddr_tx_phase = 1;
808 chip->sd_default_tx_phase = 15;
809 chip->sd_default_rx_phase = 15;
810 chip->pmos_pwr_on_interval = 200;
811 chip->sd_voltage_switch_delay = 1000;
812 chip->ms_power_class_en = 3;
814 chip->sd_400mA_ocp_thd = 1;
815 chip->sd_800mA_ocp_thd = 5;
816 chip->ms_ocp_thd = 2;
818 chip->card_drive_sel = 0x55;
819 chip->sd30_drive_sel_1v8 = 0x03;
820 chip->sd30_drive_sel_3v3 = 0x01;
822 chip->do_delink_before_power_down = 1;
823 chip->auto_power_down = 1;
824 chip->polling_config = 0;
826 chip->force_clkreq_0 = 1;
827 chip->ft2_fast_mode = 0;
829 chip->sdio_retry_cnt = 1;
831 chip->xd_timeout = 2000;
832 chip->sd_timeout = 10000;
833 chip->ms_timeout = 2000;
834 chip->mspro_timeout = 15000;
836 chip->power_down_in_ss = 1;
842 chip->delink_stage1_step = 100;
843 chip->delink_stage2_step = 40;
844 chip->delink_stage3_step = 20;
846 chip->auto_delink_in_L1 = 1;
848 chip->msi_en = msi_en;
849 chip->hp_watch_bios_hotplug = 0;
850 chip->max_payload = 0;
851 chip->phy_voltage = 0;
853 chip->support_ms_8bit = 1;
854 chip->s3_pwr_off_delay = 1000;
857 static int rtsx_probe(struct pci_dev *pci,
858 const struct pci_device_id *pci_id)
860 struct Scsi_Host *host;
861 struct rtsx_dev *dev;
863 struct task_struct *th;
865 dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
867 err = pci_enable_device(pci);
869 dev_err(&pci->dev, "PCI enable device failed!\n");
873 err = pci_request_regions(pci, CR_DRIVER_NAME);
875 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
877 pci_disable_device(pci);
882 * Ask the SCSI layer to allocate a host structure, with extra
883 * space at the end for our private rtsx_dev structure.
885 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
887 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
888 pci_release_regions(pci);
889 pci_disable_device(pci);
893 dev = host_to_rtsx(host);
894 memset(dev, 0, sizeof(struct rtsx_dev));
896 dev->chip = kzalloc(sizeof(struct rtsx_chip), GFP_KERNEL);
897 if (dev->chip == NULL) {
902 spin_lock_init(&dev->reg_lock);
903 mutex_init(&(dev->dev_mutex));
904 init_completion(&dev->cmnd_ready);
905 init_completion(&dev->control_exit);
906 init_completion(&dev->polling_exit);
907 init_completion(&(dev->notify));
908 init_completion(&dev->scanning_done);
909 init_waitqueue_head(&dev->delay_wait);
914 dev_info(&pci->dev, "Resource length: 0x%x\n",
915 (unsigned int)pci_resource_len(pci, 0));
916 dev->addr = pci_resource_start(pci, 0);
917 dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
918 if (dev->remap_addr == NULL) {
919 dev_err(&pci->dev, "ioremap error\n");
925 * Using "unsigned long" cast here to eliminate gcc warning in
928 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
929 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
931 dev->rtsx_resv_buf = dma_alloc_coherent(&(pci->dev), RTSX_RESV_BUF_LEN,
932 &(dev->rtsx_resv_buf_addr), GFP_KERNEL);
933 if (dev->rtsx_resv_buf == NULL) {
934 dev_err(&pci->dev, "alloc dma buffer fail\n");
938 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
939 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
940 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
941 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
944 dev->chip->rtsx = dev;
946 rtsx_init_options(dev->chip);
948 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
950 if (dev->chip->msi_en) {
951 if (pci_enable_msi(pci) < 0)
952 dev->chip->msi_en = 0;
955 if (rtsx_acquire_irq(dev) < 0) {
961 synchronize_irq(dev->irq);
963 rtsx_init_chip(dev->chip);
965 /* set the supported max_lun and max_id for the scsi host
966 * NOTE: the minimal value of max_id is 1 */
968 host->max_lun = dev->chip->max_lun;
970 /* Start up our control thread */
971 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
973 dev_err(&pci->dev, "Unable to start control thread\n");
977 dev->ctl_thread = th;
979 err = scsi_add_host(host, &pci->dev);
981 dev_err(&pci->dev, "Unable to add the scsi host\n");
985 /* Start up the thread for delayed SCSI-device scanning */
986 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
988 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
989 complete(&dev->scanning_done);
990 quiesce_and_remove_host(dev);
995 /* Start up the thread for polling thread */
996 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
998 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
999 quiesce_and_remove_host(dev);
1003 dev->polling_thread = th;
1005 pci_set_drvdata(pci, dev);
1009 /* We come here if there are any problems */
1011 dev_err(&pci->dev, "rtsx_probe() failed\n");
1012 release_everything(dev);
1018 static void rtsx_remove(struct pci_dev *pci)
1020 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
1022 dev_info(&pci->dev, "rtsx_remove() called\n");
1024 quiesce_and_remove_host(dev);
1025 release_everything(dev);
1027 pci_set_drvdata(pci, NULL);
1031 static const struct pci_device_id rtsx_ids[] = {
1032 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208),
1033 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1034 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288),
1035 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1039 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1041 /* pci_driver definition */
1042 static struct pci_driver driver = {
1043 .name = CR_DRIVER_NAME,
1044 .id_table = rtsx_ids,
1045 .probe = rtsx_probe,
1046 .remove = rtsx_remove,
1048 .suspend = rtsx_suspend,
1049 .resume = rtsx_resume,
1051 .shutdown = rtsx_shutdown,
1054 static int __init rtsx_init(void)
1056 pr_info("Initializing Realtek PCIE storage driver...\n");
1058 return pci_register_driver(&driver);
1061 static void __exit rtsx_exit(void)
1063 pr_info("rtsx_exit() called\n");
1065 pci_unregister_driver(&driver);
1067 pr_info("%s module exit\n", CR_DRIVER_NAME);
1070 module_init(rtsx_init)
1071 module_exit(rtsx_exit)