1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Realtek PCI-Express card reader
5 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
12 #include <linux/blkdev.h>
13 #include <linux/kthread.h>
14 #include <linux/sched.h>
15 #include <linux/workqueue.h>
22 MODULE_DESCRIPTION("Realtek PCI-Express card reader rts5208/rts5288 driver");
23 MODULE_LICENSE("GPL");
25 static unsigned int delay_use = 1;
26 module_param(delay_use, uint, 0644);
27 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
30 module_param(ss_en, int, 0644);
31 MODULE_PARM_DESC(ss_en, "enable selective suspend");
33 static int ss_interval = 50;
34 module_param(ss_interval, int, 0644);
35 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
37 static int auto_delink_en;
38 module_param(auto_delink_en, int, 0644);
39 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
41 static unsigned char aspm_l0s_l1_en;
42 module_param(aspm_l0s_l1_en, byte, 0644);
43 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
46 module_param(msi_en, int, 0644);
47 MODULE_PARM_DESC(msi_en, "enable msi");
49 static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
51 /***********************************************************************
53 ***********************************************************************/
55 static const char *host_info(struct Scsi_Host *host)
57 return "SCSI emulation for PCI-Express Mass Storage devices";
60 static int slave_alloc(struct scsi_device *sdev)
63 * Set the INQUIRY transfer length to 36. We don't use any of
64 * the extra data and many devices choke if asked for more or
67 sdev->inquiry_len = 36;
71 static int slave_configure(struct scsi_device *sdev)
74 * Scatter-gather buffers (all but the last) must have a length
75 * divisible by the bulk maxpacket size. Otherwise a data packet
76 * would end up being short, causing a premature end to the data
77 * transfer. Since high-speed bulk pipes have a maxpacket size
78 * of 512, we'll use that as the scsi device queue's DMA alignment
79 * mask. Guaranteeing proper alignment of the first buffer will
80 * have the desired effect because, except at the beginning and
81 * the end, scatter-gather buffers follow page boundaries.
83 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
85 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
86 * what is originally reported. We need this to avoid confusing
87 * the SCSI layer with devices that report 0 or 1, but need 10-byte
88 * commands (ala ATAPI devices behind certain bridges, or devices
89 * which simply have broken INQUIRY data).
91 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
92 * actual information. This seems to be the preference for
95 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
96 * the actual value or the modified one, depending on where the
99 if (sdev->scsi_level < SCSI_2) {
100 sdev->scsi_level = SCSI_2;
101 sdev->sdev_target->scsi_level = SCSI_2;
107 /***********************************************************************
108 * /proc/scsi/ functions
109 ***********************************************************************/
111 /* we use this macro to help us write into the buffer */
113 #define SPRINTF(args...) \
115 if (pos < buffer + length) \
116 pos += sprintf(pos, ## args); \
119 /* queue a command */
120 /* This is always called with scsi_lock(host) held */
121 static int queuecommand_lck(struct scsi_cmnd *srb)
123 void (*done)(struct scsi_cmnd *) = scsi_done;
124 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
125 struct rtsx_chip *chip = dev->chip;
127 /* check for state-transition errors */
129 dev_err(&dev->pci->dev, "Error: chip->srb = %p\n",
131 return SCSI_MLQUEUE_HOST_BUSY;
134 /* fail the command if we are disconnecting */
135 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
136 dev_info(&dev->pci->dev, "Fail command during disconnect\n");
137 srb->result = DID_NO_CONNECT << 16;
142 /* enqueue the command and wake up the control thread */
144 complete(&dev->cmnd_ready);
149 static DEF_SCSI_QCMD(queuecommand)
151 /***********************************************************************
152 * Error handling functions
153 ***********************************************************************/
155 /* Command timeout and abort */
156 static int command_abort(struct scsi_cmnd *srb)
158 struct Scsi_Host *host = srb->device->host;
159 struct rtsx_dev *dev = host_to_rtsx(host);
160 struct rtsx_chip *chip = dev->chip;
164 /* Is this command still active? */
165 if (chip->srb != srb) {
167 dev_info(&dev->pci->dev, "-- nothing to abort\n");
171 rtsx_set_stat(chip, RTSX_STAT_ABORT);
175 /* Wait for the aborted command to finish */
176 wait_for_completion(&dev->notify);
182 * This invokes the transport reset mechanism to reset the state of the
185 static int device_reset(struct scsi_cmnd *srb)
191 * this defines our host template, with which we'll allocate hosts
194 static const struct scsi_host_template rtsx_host_template = {
195 /* basic userland interface stuff */
196 .name = CR_DRIVER_NAME,
197 .proc_name = CR_DRIVER_NAME,
200 /* command interface -- queued only */
201 .queuecommand = queuecommand,
203 /* error and abort handlers */
204 .eh_abort_handler = command_abort,
205 .eh_device_reset_handler = device_reset,
207 /* queue commands only, only one command per LUN */
210 /* unknown initiator id */
213 .slave_alloc = slave_alloc,
214 .slave_configure = slave_configure,
216 /* lots of sg segments can be handled */
217 .sg_tablesize = SG_ALL,
219 /* limit the total size of a transfer to 120 KB */
225 /* we do our own delay after a device or bus reset */
226 .skip_settle_delay = 1,
228 /* module management */
229 .module = THIS_MODULE
232 static int rtsx_acquire_irq(struct rtsx_dev *dev)
234 struct rtsx_chip *chip = dev->chip;
236 dev_info(&dev->pci->dev, "%s: chip->msi_en = %d, pci->irq = %d\n",
237 __func__, chip->msi_en, dev->pci->irq);
239 if (request_irq(dev->pci->irq, rtsx_interrupt,
240 chip->msi_en ? 0 : IRQF_SHARED,
241 CR_DRIVER_NAME, dev)) {
242 dev_err(&dev->pci->dev,
243 "rtsx: unable to grab IRQ %d, disabling device\n",
248 dev->irq = dev->pci->irq;
249 pci_intx(dev->pci, !chip->msi_en);
257 static int __maybe_unused rtsx_suspend(struct device *dev_d)
259 struct pci_dev *pci = to_pci_dev(dev_d);
260 struct rtsx_dev *dev = pci_get_drvdata(pci);
261 struct rtsx_chip *chip;
266 /* lock the device pointers */
267 mutex_lock(&dev->dev_mutex);
271 rtsx_do_before_power_down(chip, PM_S3);
274 free_irq(dev->irq, (void *)dev);
279 pci_free_irq_vectors(pci);
281 device_wakeup_enable(dev_d);
283 /* unlock the device pointers */
284 mutex_unlock(&dev->dev_mutex);
289 static int __maybe_unused rtsx_resume(struct device *dev_d)
291 struct pci_dev *pci = to_pci_dev(dev_d);
292 struct rtsx_dev *dev = pci_get_drvdata(pci);
293 struct rtsx_chip *chip;
300 /* lock the device pointers */
301 mutex_lock(&dev->dev_mutex);
306 if (pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) < 0)
310 if (rtsx_acquire_irq(dev) < 0) {
311 /* unlock the device pointers */
312 mutex_unlock(&dev->dev_mutex);
316 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
317 rtsx_init_chip(chip);
319 /* unlock the device pointers */
320 mutex_unlock(&dev->dev_mutex);
325 static void rtsx_shutdown(struct pci_dev *pci)
327 struct rtsx_dev *dev = pci_get_drvdata(pci);
328 struct rtsx_chip *chip;
335 rtsx_do_before_power_down(chip, PM_S1);
338 free_irq(dev->irq, (void *)dev);
343 pci_free_irq_vectors(pci);
345 pci_disable_device(pci);
348 static int rtsx_control_thread(void *__dev)
350 struct rtsx_dev *dev = __dev;
351 struct rtsx_chip *chip = dev->chip;
352 struct Scsi_Host *host = rtsx_to_host(dev);
355 if (wait_for_completion_interruptible(&dev->cmnd_ready))
358 /* lock the device pointers */
359 mutex_lock(&dev->dev_mutex);
361 /* if the device has disconnected, we are free to exit */
362 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
363 dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
364 mutex_unlock(&dev->dev_mutex);
368 /* lock access to the state */
371 /* has the command aborted ? */
372 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
373 chip->srb->result = DID_ABORT << 16;
379 /* reject the command if the direction indicator
382 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
383 dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
384 chip->srb->result = DID_ERROR << 16;
387 /* reject if target != 0 or if LUN is higher than
388 * the maximum known LUN
390 else if (chip->srb->device->id) {
391 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
392 chip->srb->device->id,
393 (u8)chip->srb->device->lun);
394 chip->srb->result = DID_BAD_TARGET << 16;
397 else if (chip->srb->device->lun > chip->max_lun) {
398 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
399 chip->srb->device->id,
400 (u8)chip->srb->device->lun);
401 chip->srb->result = DID_BAD_TARGET << 16;
404 /* we've got a command, let's do it! */
406 scsi_show_command(chip);
407 rtsx_invoke_transport(chip->srb, chip);
410 /* lock access to the state */
413 /* did the command already complete because of a disconnect? */
415 ; /* nothing to do */
417 /* indicate that the command is done */
418 else if (chip->srb->result != DID_ABORT << 16) {
419 scsi_done(chip->srb);
422 dev_err(&dev->pci->dev, "scsi command aborted\n");
425 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
426 complete(&dev->notify);
428 rtsx_set_stat(chip, RTSX_STAT_IDLE);
431 /* finished working on this command */
435 /* unlock the device pointers */
436 mutex_unlock(&dev->dev_mutex);
439 /* notify the exit routine that we're actually exiting now
441 * complete()/wait_for_completion() is similar to up()/down(),
442 * except that complete() is safe in the case where the structure
443 * is getting deleted in a parallel mode of execution (i.e. just
444 * after the down() -- that's necessary for the thread-shutdown
447 * kthread_complete_and_exit() goes even further than this --
448 * it is safe in the case that the thread of the caller is going away
449 * (not just the structure) -- this is necessary for the module-remove
450 * case. This is important in preemption kernels, which transfer the
451 * flow of execution immediately upon a complete().
453 kthread_complete_and_exit(&dev->control_exit, 0);
456 static int rtsx_polling_thread(void *__dev)
458 struct rtsx_dev *dev = __dev;
459 struct rtsx_chip *chip = dev->chip;
460 struct sd_info *sd_card = &chip->sd_card;
461 struct xd_info *xd_card = &chip->xd_card;
462 struct ms_info *ms_card = &chip->ms_card;
464 sd_card->cleanup_counter = 0;
465 xd_card->cleanup_counter = 0;
466 ms_card->cleanup_counter = 0;
468 /* Wait until SCSI scan finished */
469 wait_timeout((delay_use + 5) * 1000);
472 set_current_state(TASK_INTERRUPTIBLE);
473 schedule_timeout(msecs_to_jiffies(POLLING_INTERVAL));
475 /* lock the device pointers */
476 mutex_lock(&dev->dev_mutex);
478 /* if the device has disconnected, we are free to exit */
479 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
480 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
481 mutex_unlock(&dev->dev_mutex);
485 mutex_unlock(&dev->dev_mutex);
487 mspro_polling_format_status(chip);
489 /* lock the device pointers */
490 mutex_lock(&dev->dev_mutex);
492 rtsx_polling_func(chip);
494 /* unlock the device pointers */
495 mutex_unlock(&dev->dev_mutex);
498 kthread_complete_and_exit(&dev->polling_exit, 0);
504 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
506 struct rtsx_dev *dev = dev_id;
507 struct rtsx_chip *chip;
519 spin_lock(&dev->reg_lock);
521 retval = rtsx_pre_handle_interrupt(chip);
522 if (retval == STATUS_FAIL) {
523 spin_unlock(&dev->reg_lock);
524 if (chip->int_reg == 0xFFFFFFFF)
529 status = chip->int_reg;
531 if (dev->check_card_cd) {
532 if (!(dev->check_card_cd & status)) {
533 /* card not exist, return TRANS_RESULT_FAIL */
534 dev->trans_result = TRANS_RESULT_FAIL;
541 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
542 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
543 if (status & DELINK_INT)
544 RTSX_SET_DELINK(chip);
545 dev->trans_result = TRANS_RESULT_FAIL;
548 } else if (status & TRANS_OK_INT) {
549 dev->trans_result = TRANS_RESULT_OK;
552 } else if (status & DATA_DONE_INT) {
553 dev->trans_result = TRANS_NOT_READY;
554 if (dev->done && dev->trans_state == STATE_TRANS_SG)
560 spin_unlock(&dev->reg_lock);
564 /* Release all our dynamic resources */
565 static void rtsx_release_resources(struct rtsx_dev *dev)
567 dev_info(&dev->pci->dev, "-- %s\n", __func__);
569 /* Tell the control thread to exit. The SCSI host must
570 * already have been removed so it won't try to queue
573 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
574 complete(&dev->cmnd_ready);
576 wait_for_completion(&dev->control_exit);
577 if (dev->polling_thread)
578 wait_for_completion(&dev->polling_exit);
582 if (dev->rtsx_resv_buf) {
583 dev->chip->host_cmds_ptr = NULL;
584 dev->chip->host_sg_tbl_ptr = NULL;
588 free_irq(dev->irq, (void *)dev);
589 if (dev->chip->msi_en)
590 pci_free_irq_vectors(dev->pci);
592 iounmap(dev->remap_addr);
594 rtsx_release_chip(dev->chip);
599 * First stage of disconnect processing: stop all commands and remove
602 static void quiesce_and_remove_host(struct rtsx_dev *dev)
604 struct Scsi_Host *host = rtsx_to_host(dev);
605 struct rtsx_chip *chip = dev->chip;
608 * Prevent new transfers, stop the current command, and
609 * interrupt a SCSI-scan or device-reset delay
611 mutex_lock(&dev->dev_mutex);
613 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
615 mutex_unlock(&dev->dev_mutex);
616 wake_up(&dev->delay_wait);
617 wait_for_completion(&dev->scanning_done);
619 /* Wait some time to let other threads exist */
623 * queuecommand won't accept any new commands and the control
624 * thread won't execute a previously-queued command. If there
625 * is such a command pending, complete it with an error.
627 mutex_lock(&dev->dev_mutex);
629 chip->srb->result = DID_NO_CONNECT << 16;
631 scsi_done(dev->chip->srb);
635 mutex_unlock(&dev->dev_mutex);
637 /* Now we own no commands so it's safe to remove the SCSI host */
638 scsi_remove_host(host);
641 /* Second stage of disconnect processing: deallocate all resources */
642 static void release_everything(struct rtsx_dev *dev)
644 rtsx_release_resources(dev);
647 * Drop our reference to the host; the SCSI core will free it
648 * when the refcount becomes 0.
650 scsi_host_put(rtsx_to_host(dev));
653 /* Thread to carry out delayed SCSI-device scanning */
654 static int rtsx_scan_thread(void *__dev)
656 struct rtsx_dev *dev = __dev;
657 struct rtsx_chip *chip = dev->chip;
659 /* Wait for the timeout to expire or for a disconnect */
661 dev_info(&dev->pci->dev,
662 "%s: waiting for device to settle before scanning\n",
664 wait_event_interruptible_timeout
666 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
670 /* If the device is still connected, perform the scanning */
671 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
672 scsi_scan_host(rtsx_to_host(dev));
673 dev_info(&dev->pci->dev, "%s: device scan complete\n",
676 /* Should we unbind if no devices were detected? */
679 kthread_complete_and_exit(&dev->scanning_done, 0);
682 static void rtsx_init_options(struct rtsx_chip *chip)
684 chip->vendor_id = chip->rtsx->pci->vendor;
685 chip->product_id = chip->rtsx->pci->device;
688 chip->driver_first_load = 1;
689 #ifdef HW_AUTO_SWITCH_SD_BUS
690 chip->sdio_in_charge = 0;
693 chip->mspro_formatter_enable = 1;
695 chip->use_hw_setting = 0;
696 chip->lun_mode = DEFAULT_SINGLE;
697 chip->auto_delink_en = auto_delink_en;
699 chip->ss_idle_period = ss_interval * 1000;
700 chip->remote_wakeup_en = 0;
701 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
702 chip->dynamic_aspm = 1;
703 chip->fpga_sd_sdr104_clk = CLK_200;
704 chip->fpga_sd_ddr50_clk = CLK_100;
705 chip->fpga_sd_sdr50_clk = CLK_100;
706 chip->fpga_sd_hs_clk = CLK_100;
707 chip->fpga_mmc_52m_clk = CLK_80;
708 chip->fpga_ms_hg_clk = CLK_80;
709 chip->fpga_ms_4bit_clk = CLK_80;
710 chip->fpga_ms_1bit_clk = CLK_40;
711 chip->asic_sd_sdr104_clk = 203;
712 chip->asic_sd_sdr50_clk = 98;
713 chip->asic_sd_ddr50_clk = 98;
714 chip->asic_sd_hs_clk = 98;
715 chip->asic_mmc_52m_clk = 98;
716 chip->asic_ms_hg_clk = 117;
717 chip->asic_ms_4bit_clk = 78;
718 chip->asic_ms_1bit_clk = 39;
719 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
720 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
721 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
722 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
723 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
724 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
725 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
726 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
728 chip->sd_speed_prior = 0x01040203;
729 chip->sd_current_prior = 0x00010203;
730 chip->sd_ctl = SD_PUSH_POINT_AUTO |
731 SD_SAMPLE_POINT_AUTO |
732 SUPPORT_MMC_DDR_MODE;
733 chip->sd_ddr_tx_phase = 0;
734 chip->mmc_ddr_tx_phase = 1;
735 chip->sd_default_tx_phase = 15;
736 chip->sd_default_rx_phase = 15;
737 chip->pmos_pwr_on_interval = 200;
738 chip->sd_voltage_switch_delay = 1000;
739 chip->ms_power_class_en = 3;
741 chip->sd_400mA_ocp_thd = 1;
742 chip->sd_800mA_ocp_thd = 5;
743 chip->ms_ocp_thd = 2;
745 chip->card_drive_sel = 0x55;
746 chip->sd30_drive_sel_1v8 = 0x03;
747 chip->sd30_drive_sel_3v3 = 0x01;
749 chip->do_delink_before_power_down = 1;
750 chip->auto_power_down = 1;
751 chip->polling_config = 0;
753 chip->force_clkreq_0 = 1;
754 chip->ft2_fast_mode = 0;
756 chip->sdio_retry_cnt = 1;
758 chip->xd_timeout = 2000;
759 chip->sd_timeout = 10000;
760 chip->ms_timeout = 2000;
761 chip->mspro_timeout = 15000;
763 chip->power_down_in_ss = 1;
769 chip->delink_stage1_step = 100;
770 chip->delink_stage2_step = 40;
771 chip->delink_stage3_step = 20;
773 chip->auto_delink_in_L1 = 1;
775 chip->msi_en = msi_en;
776 chip->hp_watch_bios_hotplug = 0;
777 chip->max_payload = 0;
778 chip->phy_voltage = 0;
780 chip->support_ms_8bit = 1;
781 chip->s3_pwr_off_delay = 1000;
784 static int rtsx_probe(struct pci_dev *pci,
785 const struct pci_device_id *pci_id)
787 struct Scsi_Host *host;
788 struct rtsx_dev *dev;
790 struct task_struct *th;
792 dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
794 err = pcim_enable_device(pci);
796 dev_err(&pci->dev, "PCI enable device failed!\n");
800 err = pci_request_regions(pci, CR_DRIVER_NAME);
802 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
808 * Ask the SCSI layer to allocate a host structure, with extra
809 * space at the end for our private rtsx_dev structure.
811 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
813 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
815 goto scsi_host_alloc_fail;
818 dev = host_to_rtsx(host);
819 memset(dev, 0, sizeof(struct rtsx_dev));
821 dev->chip = kzalloc(sizeof(*dev->chip), GFP_KERNEL);
824 goto chip_alloc_fail;
827 spin_lock_init(&dev->reg_lock);
828 mutex_init(&dev->dev_mutex);
829 init_completion(&dev->cmnd_ready);
830 init_completion(&dev->control_exit);
831 init_completion(&dev->polling_exit);
832 init_completion(&dev->notify);
833 init_completion(&dev->scanning_done);
834 init_waitqueue_head(&dev->delay_wait);
839 dev_info(&pci->dev, "Resource length: 0x%x\n",
840 (unsigned int)pci_resource_len(pci, 0));
841 dev->addr = pci_resource_start(pci, 0);
842 dev->remap_addr = ioremap(dev->addr, pci_resource_len(pci, 0));
843 if (!dev->remap_addr) {
844 dev_err(&pci->dev, "ioremap error\n");
850 * Using "unsigned long" cast here to eliminate gcc warning in
853 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
854 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
856 dev->rtsx_resv_buf = dmam_alloc_coherent(&pci->dev, RTSX_RESV_BUF_LEN,
857 &dev->rtsx_resv_buf_addr,
859 if (!dev->rtsx_resv_buf) {
860 dev_err(&pci->dev, "alloc dma buffer fail\n");
864 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
865 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
866 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
867 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
870 dev->chip->rtsx = dev;
872 rtsx_init_options(dev->chip);
874 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
876 if (dev->chip->msi_en) {
877 if (pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) < 0)
878 dev->chip->msi_en = 0;
881 if (rtsx_acquire_irq(dev) < 0) {
883 goto irq_acquire_fail;
887 synchronize_irq(dev->irq);
889 rtsx_init_chip(dev->chip);
892 * set the supported max_lun and max_id for the scsi host
893 * NOTE: the minimal value of max_id is 1
896 host->max_lun = dev->chip->max_lun;
898 /* Start up our control thread */
899 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
901 dev_err(&pci->dev, "Unable to start control thread\n");
903 goto control_thread_fail;
905 dev->ctl_thread = th;
907 err = scsi_add_host(host, &pci->dev);
909 dev_err(&pci->dev, "Unable to add the scsi host\n");
910 goto scsi_add_host_fail;
913 /* Start up the thread for delayed SCSI-device scanning */
914 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
916 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
917 complete(&dev->scanning_done);
919 goto scan_thread_fail;
922 /* Start up the thread for polling thread */
923 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
925 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
927 goto scan_thread_fail;
929 dev->polling_thread = th;
931 pci_set_drvdata(pci, dev);
935 /* We come here if there are any problems */
937 quiesce_and_remove_host(dev);
939 complete(&dev->cmnd_ready);
940 wait_for_completion(&dev->control_exit);
942 free_irq(dev->irq, (void *)dev);
943 rtsx_release_chip(dev->chip);
945 dev->chip->host_cmds_ptr = NULL;
946 dev->chip->host_sg_tbl_ptr = NULL;
947 if (dev->chip->msi_en)
948 pci_free_irq_vectors(dev->pci);
950 iounmap(dev->remap_addr);
954 dev_err(&pci->dev, "%s failed\n", __func__);
956 scsi_host_alloc_fail:
957 pci_release_regions(pci);
961 static void rtsx_remove(struct pci_dev *pci)
963 struct rtsx_dev *dev = pci_get_drvdata(pci);
965 quiesce_and_remove_host(dev);
966 release_everything(dev);
967 pci_release_regions(pci);
971 static const struct pci_device_id rtsx_ids[] = {
972 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208),
973 PCI_CLASS_OTHERS << 16, 0xFF0000 },
974 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288),
975 PCI_CLASS_OTHERS << 16, 0xFF0000 },
979 MODULE_DEVICE_TABLE(pci, rtsx_ids);
981 static SIMPLE_DEV_PM_OPS(rtsx_pm_ops, rtsx_suspend, rtsx_resume);
983 /* pci_driver definition */
984 static struct pci_driver rtsx_driver = {
985 .name = CR_DRIVER_NAME,
986 .id_table = rtsx_ids,
988 .remove = rtsx_remove,
989 .driver.pm = &rtsx_pm_ops,
990 .shutdown = rtsx_shutdown,
993 module_pci_driver(rtsx_driver);