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 = 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 = 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 = 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);
419 static int rtsx_control_thread(void *__dev)
421 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
422 struct rtsx_chip *chip = dev->chip;
423 struct Scsi_Host *host = rtsx_to_host(dev);
426 if (wait_for_completion_interruptible(&dev->cmnd_ready))
429 /* lock the device pointers */
430 mutex_lock(&(dev->dev_mutex));
432 /* if the device has disconnected, we are free to exit */
433 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
434 dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
435 mutex_unlock(&dev->dev_mutex);
439 /* lock access to the state */
442 /* has the command aborted ? */
443 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
444 chip->srb->result = DID_ABORT << 16;
450 /* reject the command if the direction indicator
453 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
454 dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
455 chip->srb->result = DID_ERROR << 16;
458 /* reject if target != 0 or if LUN is higher than
459 * the maximum known LUN
461 else if (chip->srb->device->id) {
462 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
463 chip->srb->device->id,
464 (u8)chip->srb->device->lun);
465 chip->srb->result = DID_BAD_TARGET << 16;
468 else if (chip->srb->device->lun > chip->max_lun) {
469 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
470 chip->srb->device->id,
471 (u8)chip->srb->device->lun);
472 chip->srb->result = DID_BAD_TARGET << 16;
475 /* we've got a command, let's do it! */
477 scsi_show_command(chip);
478 rtsx_invoke_transport(chip->srb, chip);
481 /* lock access to the state */
484 /* did the command already complete because of a disconnect? */
486 ; /* nothing to do */
488 /* indicate that the command is done */
489 else if (chip->srb->result != DID_ABORT << 16) {
490 chip->srb->scsi_done(chip->srb);
493 dev_err(&dev->pci->dev, "scsi command aborted\n");
496 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
497 complete(&(dev->notify));
499 rtsx_set_stat(chip, RTSX_STAT_IDLE);
502 /* finished working on this command */
506 /* unlock the device pointers */
507 mutex_unlock(&dev->dev_mutex);
510 /* notify the exit routine that we're actually exiting now
512 * complete()/wait_for_completion() is similar to up()/down(),
513 * except that complete() is safe in the case where the structure
514 * is getting deleted in a parallel mode of execution (i.e. just
515 * after the down() -- that's necessary for the thread-shutdown
518 * complete_and_exit() goes even further than this -- it is safe in
519 * the case that the thread of the caller is going away (not just
520 * the structure) -- this is necessary for the module-remove case.
521 * This is important in preemption kernels, which transfer the flow
522 * of execution immediately upon a complete().
524 complete_and_exit(&dev->control_exit, 0);
528 static int rtsx_polling_thread(void *__dev)
530 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
531 struct rtsx_chip *chip = dev->chip;
532 struct sd_info *sd_card = &(chip->sd_card);
533 struct xd_info *xd_card = &(chip->xd_card);
534 struct ms_info *ms_card = &(chip->ms_card);
536 sd_card->cleanup_counter = 0;
537 xd_card->cleanup_counter = 0;
538 ms_card->cleanup_counter = 0;
540 /* Wait until SCSI scan finished */
541 wait_timeout((delay_use + 5) * 1000);
545 set_current_state(TASK_INTERRUPTIBLE);
546 schedule_timeout(POLLING_INTERVAL);
548 /* lock the device pointers */
549 mutex_lock(&(dev->dev_mutex));
551 /* if the device has disconnected, we are free to exit */
552 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
553 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
554 mutex_unlock(&dev->dev_mutex);
558 mutex_unlock(&dev->dev_mutex);
560 mspro_polling_format_status(chip);
562 /* lock the device pointers */
563 mutex_lock(&(dev->dev_mutex));
565 rtsx_polling_func(chip);
567 /* unlock the device pointers */
568 mutex_unlock(&dev->dev_mutex);
571 complete_and_exit(&dev->polling_exit, 0);
577 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
579 struct rtsx_dev *dev = dev_id;
580 struct rtsx_chip *chip;
592 spin_lock(&dev->reg_lock);
594 retval = rtsx_pre_handle_interrupt(chip);
595 if (retval == STATUS_FAIL) {
596 spin_unlock(&dev->reg_lock);
597 if (chip->int_reg == 0xFFFFFFFF)
602 status = chip->int_reg;
604 if (dev->check_card_cd) {
605 if (!(dev->check_card_cd & status)) {
606 /* card not exist, return TRANS_RESULT_FAIL */
607 dev->trans_result = TRANS_RESULT_FAIL;
614 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
615 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
616 if (status & DELINK_INT)
617 RTSX_SET_DELINK(chip);
618 dev->trans_result = TRANS_RESULT_FAIL;
621 } else if (status & TRANS_OK_INT) {
622 dev->trans_result = TRANS_RESULT_OK;
625 } else if (status & DATA_DONE_INT) {
626 dev->trans_result = TRANS_NOT_READY;
627 if (dev->done && (dev->trans_state == STATE_TRANS_SG))
633 spin_unlock(&dev->reg_lock);
638 /* Release all our dynamic resources */
639 static void rtsx_release_resources(struct rtsx_dev *dev)
641 dev_info(&dev->pci->dev, "-- %s\n", __func__);
643 /* Tell the control thread to exit. The SCSI host must
644 * already have been removed so it won't try to queue
647 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
648 complete(&dev->cmnd_ready);
650 wait_for_completion(&dev->control_exit);
651 if (dev->polling_thread)
652 wait_for_completion(&dev->polling_exit);
656 if (dev->rtsx_resv_buf) {
657 dma_free_coherent(&(dev->pci->dev), RTSX_RESV_BUF_LEN,
658 dev->rtsx_resv_buf, dev->rtsx_resv_buf_addr);
659 dev->chip->host_cmds_ptr = NULL;
660 dev->chip->host_sg_tbl_ptr = NULL;
664 free_irq(dev->irq, (void *)dev);
665 if (dev->chip->msi_en)
666 pci_disable_msi(dev->pci);
668 iounmap(dev->remap_addr);
670 pci_disable_device(dev->pci);
671 pci_release_regions(dev->pci);
673 rtsx_release_chip(dev->chip);
677 /* First stage of disconnect processing: stop all commands and remove
679 static void quiesce_and_remove_host(struct rtsx_dev *dev)
681 struct Scsi_Host *host = rtsx_to_host(dev);
682 struct rtsx_chip *chip = dev->chip;
684 /* Prevent new transfers, stop the current command, and
685 * interrupt a SCSI-scan or device-reset delay */
686 mutex_lock(&dev->dev_mutex);
688 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
690 mutex_unlock(&dev->dev_mutex);
691 wake_up(&dev->delay_wait);
692 wait_for_completion(&dev->scanning_done);
694 /* Wait some time to let other threads exist */
697 /* queuecommand won't accept any new commands and the control
698 * thread won't execute a previously-queued command. If there
699 * is such a command pending, complete it with an error. */
700 mutex_lock(&dev->dev_mutex);
702 chip->srb->result = DID_NO_CONNECT << 16;
704 chip->srb->scsi_done(dev->chip->srb);
708 mutex_unlock(&dev->dev_mutex);
710 /* Now we own no commands so it's safe to remove the SCSI host */
711 scsi_remove_host(host);
714 /* Second stage of disconnect processing: deallocate all resources */
715 static void release_everything(struct rtsx_dev *dev)
717 rtsx_release_resources(dev);
719 /* Drop our reference to the host; the SCSI core will free it
720 * when the refcount becomes 0. */
721 scsi_host_put(rtsx_to_host(dev));
724 /* Thread to carry out delayed SCSI-device scanning */
725 static int rtsx_scan_thread(void *__dev)
727 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
728 struct rtsx_chip *chip = dev->chip;
730 /* Wait for the timeout to expire or for a disconnect */
732 dev_info(&dev->pci->dev,
733 "%s: waiting for device to settle before scanning\n",
735 wait_event_interruptible_timeout(dev->delay_wait,
736 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
740 /* If the device is still connected, perform the scanning */
741 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
742 scsi_scan_host(rtsx_to_host(dev));
743 dev_info(&dev->pci->dev, "%s: device scan complete\n",
746 /* Should we unbind if no devices were detected? */
749 complete_and_exit(&dev->scanning_done, 0);
752 static void rtsx_init_options(struct rtsx_chip *chip)
754 chip->vendor_id = chip->rtsx->pci->vendor;
755 chip->product_id = chip->rtsx->pci->device;
758 chip->driver_first_load = 1;
759 #ifdef HW_AUTO_SWITCH_SD_BUS
760 chip->sdio_in_charge = 0;
763 chip->mspro_formatter_enable = 1;
765 chip->use_hw_setting = 0;
766 chip->lun_mode = DEFAULT_SINGLE;
767 chip->auto_delink_en = auto_delink_en;
769 chip->ss_idle_period = ss_interval * 1000;
770 chip->remote_wakeup_en = 0;
771 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
772 chip->dynamic_aspm = 1;
773 chip->fpga_sd_sdr104_clk = CLK_200;
774 chip->fpga_sd_ddr50_clk = CLK_100;
775 chip->fpga_sd_sdr50_clk = CLK_100;
776 chip->fpga_sd_hs_clk = CLK_100;
777 chip->fpga_mmc_52m_clk = CLK_80;
778 chip->fpga_ms_hg_clk = CLK_80;
779 chip->fpga_ms_4bit_clk = CLK_80;
780 chip->fpga_ms_1bit_clk = CLK_40;
781 chip->asic_sd_sdr104_clk = 203;
782 chip->asic_sd_sdr50_clk = 98;
783 chip->asic_sd_ddr50_clk = 98;
784 chip->asic_sd_hs_clk = 98;
785 chip->asic_mmc_52m_clk = 98;
786 chip->asic_ms_hg_clk = 117;
787 chip->asic_ms_4bit_clk = 78;
788 chip->asic_ms_1bit_clk = 39;
789 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
790 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
791 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
792 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
793 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
794 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
795 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
796 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
798 chip->sd_speed_prior = 0x01040203;
799 chip->sd_current_prior = 0x00010203;
800 chip->sd_ctl = SD_PUSH_POINT_AUTO |
801 SD_SAMPLE_POINT_AUTO |
802 SUPPORT_MMC_DDR_MODE;
803 chip->sd_ddr_tx_phase = 0;
804 chip->mmc_ddr_tx_phase = 1;
805 chip->sd_default_tx_phase = 15;
806 chip->sd_default_rx_phase = 15;
807 chip->pmos_pwr_on_interval = 200;
808 chip->sd_voltage_switch_delay = 1000;
809 chip->ms_power_class_en = 3;
811 chip->sd_400mA_ocp_thd = 1;
812 chip->sd_800mA_ocp_thd = 5;
813 chip->ms_ocp_thd = 2;
815 chip->card_drive_sel = 0x55;
816 chip->sd30_drive_sel_1v8 = 0x03;
817 chip->sd30_drive_sel_3v3 = 0x01;
819 chip->do_delink_before_power_down = 1;
820 chip->auto_power_down = 1;
821 chip->polling_config = 0;
823 chip->force_clkreq_0 = 1;
824 chip->ft2_fast_mode = 0;
826 chip->sdio_retry_cnt = 1;
828 chip->xd_timeout = 2000;
829 chip->sd_timeout = 10000;
830 chip->ms_timeout = 2000;
831 chip->mspro_timeout = 15000;
833 chip->power_down_in_ss = 1;
839 chip->delink_stage1_step = 100;
840 chip->delink_stage2_step = 40;
841 chip->delink_stage3_step = 20;
843 chip->auto_delink_in_L1 = 1;
845 chip->msi_en = msi_en;
846 chip->hp_watch_bios_hotplug = 0;
847 chip->max_payload = 0;
848 chip->phy_voltage = 0;
850 chip->support_ms_8bit = 1;
851 chip->s3_pwr_off_delay = 1000;
854 static int rtsx_probe(struct pci_dev *pci,
855 const struct pci_device_id *pci_id)
857 struct Scsi_Host *host;
858 struct rtsx_dev *dev;
860 struct task_struct *th;
862 dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
864 err = pci_enable_device(pci);
866 dev_err(&pci->dev, "PCI enable device failed!\n");
870 err = pci_request_regions(pci, CR_DRIVER_NAME);
872 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
874 pci_disable_device(pci);
879 * Ask the SCSI layer to allocate a host structure, with extra
880 * space at the end for our private rtsx_dev structure.
882 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
884 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
885 pci_release_regions(pci);
886 pci_disable_device(pci);
890 dev = host_to_rtsx(host);
891 memset(dev, 0, sizeof(struct rtsx_dev));
893 dev->chip = kzalloc(sizeof(struct rtsx_chip), GFP_KERNEL);
894 if (dev->chip == NULL) {
899 spin_lock_init(&dev->reg_lock);
900 mutex_init(&(dev->dev_mutex));
901 init_completion(&dev->cmnd_ready);
902 init_completion(&dev->control_exit);
903 init_completion(&dev->polling_exit);
904 init_completion(&(dev->notify));
905 init_completion(&dev->scanning_done);
906 init_waitqueue_head(&dev->delay_wait);
911 dev_info(&pci->dev, "Resource length: 0x%x\n",
912 (unsigned int)pci_resource_len(pci, 0));
913 dev->addr = pci_resource_start(pci, 0);
914 dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
915 if (dev->remap_addr == NULL) {
916 dev_err(&pci->dev, "ioremap error\n");
922 * Using "unsigned long" cast here to eliminate gcc warning in
925 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
926 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
928 dev->rtsx_resv_buf = dma_alloc_coherent(&(pci->dev), RTSX_RESV_BUF_LEN,
929 &(dev->rtsx_resv_buf_addr), GFP_KERNEL);
930 if (dev->rtsx_resv_buf == NULL) {
931 dev_err(&pci->dev, "alloc dma buffer fail\n");
935 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
936 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
937 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
938 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
941 dev->chip->rtsx = dev;
943 rtsx_init_options(dev->chip);
945 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
947 if (dev->chip->msi_en) {
948 if (pci_enable_msi(pci) < 0)
949 dev->chip->msi_en = 0;
952 if (rtsx_acquire_irq(dev) < 0) {
958 synchronize_irq(dev->irq);
960 rtsx_init_chip(dev->chip);
962 /* set the supported max_lun and max_id for the scsi host
963 * NOTE: the minimal value of max_id is 1 */
965 host->max_lun = dev->chip->max_lun;
967 /* Start up our control thread */
968 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
970 dev_err(&pci->dev, "Unable to start control thread\n");
974 dev->ctl_thread = th;
976 err = scsi_add_host(host, &pci->dev);
978 dev_err(&pci->dev, "Unable to add the scsi host\n");
982 /* Start up the thread for delayed SCSI-device scanning */
983 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
985 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
986 complete(&dev->scanning_done);
987 quiesce_and_remove_host(dev);
992 /* Start up the thread for polling thread */
993 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
995 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
996 quiesce_and_remove_host(dev);
1000 dev->polling_thread = th;
1002 pci_set_drvdata(pci, dev);
1006 /* We come here if there are any problems */
1008 dev_err(&pci->dev, "rtsx_probe() failed\n");
1009 release_everything(dev);
1015 static void rtsx_remove(struct pci_dev *pci)
1017 struct rtsx_dev *dev = pci_get_drvdata(pci);
1019 dev_info(&pci->dev, "rtsx_remove() called\n");
1021 quiesce_and_remove_host(dev);
1022 release_everything(dev);
1024 pci_set_drvdata(pci, NULL);
1028 static const struct pci_device_id rtsx_ids[] = {
1029 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208),
1030 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1031 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288),
1032 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1036 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1038 /* pci_driver definition */
1039 static struct pci_driver driver = {
1040 .name = CR_DRIVER_NAME,
1041 .id_table = rtsx_ids,
1042 .probe = rtsx_probe,
1043 .remove = rtsx_remove,
1045 .suspend = rtsx_suspend,
1046 .resume = rtsx_resume,
1048 .shutdown = rtsx_shutdown,
1051 static int __init rtsx_init(void)
1053 pr_info("Initializing Realtek PCIE storage driver...\n");
1055 return pci_register_driver(&driver);
1058 static void __exit rtsx_exit(void)
1060 pr_info("rtsx_exit() called\n");
1062 pci_unregister_driver(&driver);
1064 pr_info("%s module exit\n", CR_DRIVER_NAME);
1067 module_init(rtsx_init)
1068 module_exit(rtsx_exit)