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;
385 } else if (chip->srb->device->id) {
386 /* reject if target != 0 or if LUN is higher than
387 * the maximum known LUN
389 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
390 chip->srb->device->id,
391 (u8)chip->srb->device->lun);
392 chip->srb->result = DID_BAD_TARGET << 16;
393 } else if (chip->srb->device->lun > chip->max_lun) {
394 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
395 chip->srb->device->id,
396 (u8)chip->srb->device->lun);
397 chip->srb->result = DID_BAD_TARGET << 16;
399 /* we've got a command, let's do it! */
400 scsi_show_command(chip);
401 rtsx_invoke_transport(chip->srb, chip);
404 /* lock access to the state */
407 /* did the command already complete because of a disconnect? */
409 ; /* nothing to do */
411 /* indicate that the command is done */
412 else if (chip->srb->result != DID_ABORT << 16) {
413 scsi_done(chip->srb);
416 dev_err(&dev->pci->dev, "scsi command aborted\n");
419 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
420 complete(&dev->notify);
422 rtsx_set_stat(chip, RTSX_STAT_IDLE);
425 /* finished working on this command */
429 /* unlock the device pointers */
430 mutex_unlock(&dev->dev_mutex);
433 /* notify the exit routine that we're actually exiting now
435 * complete()/wait_for_completion() is similar to up()/down(),
436 * except that complete() is safe in the case where the structure
437 * is getting deleted in a parallel mode of execution (i.e. just
438 * after the down() -- that's necessary for the thread-shutdown
441 * kthread_complete_and_exit() goes even further than this --
442 * it is safe in the case that the thread of the caller is going away
443 * (not just the structure) -- this is necessary for the module-remove
444 * case. This is important in preemption kernels, which transfer the
445 * flow of execution immediately upon a complete().
447 kthread_complete_and_exit(&dev->control_exit, 0);
450 static int rtsx_polling_thread(void *__dev)
452 struct rtsx_dev *dev = __dev;
453 struct rtsx_chip *chip = dev->chip;
454 struct sd_info *sd_card = &chip->sd_card;
455 struct xd_info *xd_card = &chip->xd_card;
456 struct ms_info *ms_card = &chip->ms_card;
458 sd_card->cleanup_counter = 0;
459 xd_card->cleanup_counter = 0;
460 ms_card->cleanup_counter = 0;
462 /* Wait until SCSI scan finished */
463 wait_timeout((delay_use + 5) * 1000);
466 set_current_state(TASK_INTERRUPTIBLE);
467 schedule_timeout(msecs_to_jiffies(POLLING_INTERVAL));
469 /* lock the device pointers */
470 mutex_lock(&dev->dev_mutex);
472 /* if the device has disconnected, we are free to exit */
473 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
474 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
475 mutex_unlock(&dev->dev_mutex);
479 mutex_unlock(&dev->dev_mutex);
481 mspro_polling_format_status(chip);
483 /* lock the device pointers */
484 mutex_lock(&dev->dev_mutex);
486 rtsx_polling_func(chip);
488 /* unlock the device pointers */
489 mutex_unlock(&dev->dev_mutex);
492 kthread_complete_and_exit(&dev->polling_exit, 0);
498 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
500 struct rtsx_dev *dev = dev_id;
501 struct rtsx_chip *chip;
513 spin_lock(&dev->reg_lock);
515 retval = rtsx_pre_handle_interrupt(chip);
516 if (retval == STATUS_FAIL) {
517 spin_unlock(&dev->reg_lock);
518 if (chip->int_reg == 0xFFFFFFFF)
523 status = chip->int_reg;
525 if (dev->check_card_cd) {
526 if (!(dev->check_card_cd & status)) {
527 /* card not exist, return TRANS_RESULT_FAIL */
528 dev->trans_result = TRANS_RESULT_FAIL;
535 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
536 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
537 if (status & DELINK_INT)
538 RTSX_SET_DELINK(chip);
539 dev->trans_result = TRANS_RESULT_FAIL;
542 } else if (status & TRANS_OK_INT) {
543 dev->trans_result = TRANS_RESULT_OK;
546 } else if (status & DATA_DONE_INT) {
547 dev->trans_result = TRANS_NOT_READY;
548 if (dev->done && dev->trans_state == STATE_TRANS_SG)
554 spin_unlock(&dev->reg_lock);
558 /* Release all our dynamic resources */
559 static void rtsx_release_resources(struct rtsx_dev *dev)
561 dev_info(&dev->pci->dev, "-- %s\n", __func__);
563 /* Tell the control thread to exit. The SCSI host must
564 * already have been removed so it won't try to queue
567 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
568 complete(&dev->cmnd_ready);
570 wait_for_completion(&dev->control_exit);
571 if (dev->polling_thread)
572 wait_for_completion(&dev->polling_exit);
576 if (dev->rtsx_resv_buf) {
577 dev->chip->host_cmds_ptr = NULL;
578 dev->chip->host_sg_tbl_ptr = NULL;
582 free_irq(dev->irq, (void *)dev);
583 if (dev->chip->msi_en)
584 pci_free_irq_vectors(dev->pci);
586 iounmap(dev->remap_addr);
588 rtsx_release_chip(dev->chip);
593 * First stage of disconnect processing: stop all commands and remove
596 static void quiesce_and_remove_host(struct rtsx_dev *dev)
598 struct Scsi_Host *host = rtsx_to_host(dev);
599 struct rtsx_chip *chip = dev->chip;
602 * Prevent new transfers, stop the current command, and
603 * interrupt a SCSI-scan or device-reset delay
605 mutex_lock(&dev->dev_mutex);
607 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
609 mutex_unlock(&dev->dev_mutex);
610 wake_up(&dev->delay_wait);
611 wait_for_completion(&dev->scanning_done);
613 /* Wait some time to let other threads exist */
617 * queuecommand won't accept any new commands and the control
618 * thread won't execute a previously-queued command. If there
619 * is such a command pending, complete it with an error.
621 mutex_lock(&dev->dev_mutex);
623 chip->srb->result = DID_NO_CONNECT << 16;
625 scsi_done(dev->chip->srb);
629 mutex_unlock(&dev->dev_mutex);
631 /* Now we own no commands so it's safe to remove the SCSI host */
632 scsi_remove_host(host);
635 /* Second stage of disconnect processing: deallocate all resources */
636 static void release_everything(struct rtsx_dev *dev)
638 rtsx_release_resources(dev);
641 * Drop our reference to the host; the SCSI core will free it
642 * when the refcount becomes 0.
644 scsi_host_put(rtsx_to_host(dev));
647 /* Thread to carry out delayed SCSI-device scanning */
648 static int rtsx_scan_thread(void *__dev)
650 struct rtsx_dev *dev = __dev;
651 struct rtsx_chip *chip = dev->chip;
653 /* Wait for the timeout to expire or for a disconnect */
655 dev_info(&dev->pci->dev,
656 "%s: waiting for device to settle before scanning\n",
658 wait_event_interruptible_timeout
660 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
664 /* If the device is still connected, perform the scanning */
665 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
666 scsi_scan_host(rtsx_to_host(dev));
667 dev_info(&dev->pci->dev, "%s: device scan complete\n",
670 /* Should we unbind if no devices were detected? */
673 kthread_complete_and_exit(&dev->scanning_done, 0);
676 static void rtsx_init_options(struct rtsx_chip *chip)
678 chip->vendor_id = chip->rtsx->pci->vendor;
679 chip->product_id = chip->rtsx->pci->device;
682 chip->driver_first_load = 1;
683 #ifdef HW_AUTO_SWITCH_SD_BUS
684 chip->sdio_in_charge = 0;
687 chip->mspro_formatter_enable = 1;
689 chip->use_hw_setting = 0;
690 chip->lun_mode = DEFAULT_SINGLE;
691 chip->auto_delink_en = auto_delink_en;
693 chip->ss_idle_period = ss_interval * 1000;
694 chip->remote_wakeup_en = 0;
695 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
696 chip->dynamic_aspm = 1;
697 chip->fpga_sd_sdr104_clk = CLK_200;
698 chip->fpga_sd_ddr50_clk = CLK_100;
699 chip->fpga_sd_sdr50_clk = CLK_100;
700 chip->fpga_sd_hs_clk = CLK_100;
701 chip->fpga_mmc_52m_clk = CLK_80;
702 chip->fpga_ms_hg_clk = CLK_80;
703 chip->fpga_ms_4bit_clk = CLK_80;
704 chip->fpga_ms_1bit_clk = CLK_40;
705 chip->asic_sd_sdr104_clk = 203;
706 chip->asic_sd_sdr50_clk = 98;
707 chip->asic_sd_ddr50_clk = 98;
708 chip->asic_sd_hs_clk = 98;
709 chip->asic_mmc_52m_clk = 98;
710 chip->asic_ms_hg_clk = 117;
711 chip->asic_ms_4bit_clk = 78;
712 chip->asic_ms_1bit_clk = 39;
713 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
714 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
715 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
716 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
717 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
718 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
719 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
720 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
722 chip->sd_speed_prior = 0x01040203;
723 chip->sd_current_prior = 0x00010203;
724 chip->sd_ctl = SD_PUSH_POINT_AUTO |
725 SD_SAMPLE_POINT_AUTO |
726 SUPPORT_MMC_DDR_MODE;
727 chip->sd_ddr_tx_phase = 0;
728 chip->mmc_ddr_tx_phase = 1;
729 chip->sd_default_tx_phase = 15;
730 chip->sd_default_rx_phase = 15;
731 chip->pmos_pwr_on_interval = 200;
732 chip->sd_voltage_switch_delay = 1000;
733 chip->ms_power_class_en = 3;
735 chip->sd_400mA_ocp_thd = 1;
736 chip->sd_800mA_ocp_thd = 5;
737 chip->ms_ocp_thd = 2;
739 chip->card_drive_sel = 0x55;
740 chip->sd30_drive_sel_1v8 = 0x03;
741 chip->sd30_drive_sel_3v3 = 0x01;
743 chip->do_delink_before_power_down = 1;
744 chip->auto_power_down = 1;
745 chip->polling_config = 0;
747 chip->force_clkreq_0 = 1;
748 chip->ft2_fast_mode = 0;
750 chip->sdio_retry_cnt = 1;
752 chip->xd_timeout = 2000;
753 chip->sd_timeout = 10000;
754 chip->ms_timeout = 2000;
755 chip->mspro_timeout = 15000;
757 chip->power_down_in_ss = 1;
763 chip->delink_stage1_step = 100;
764 chip->delink_stage2_step = 40;
765 chip->delink_stage3_step = 20;
767 chip->auto_delink_in_L1 = 1;
769 chip->msi_en = msi_en;
770 chip->hp_watch_bios_hotplug = 0;
771 chip->max_payload = 0;
772 chip->phy_voltage = 0;
774 chip->support_ms_8bit = 1;
775 chip->s3_pwr_off_delay = 1000;
778 static int rtsx_probe(struct pci_dev *pci,
779 const struct pci_device_id *pci_id)
781 struct Scsi_Host *host;
782 struct rtsx_dev *dev;
784 struct task_struct *th;
786 dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
788 err = pcim_enable_device(pci);
790 dev_err(&pci->dev, "PCI enable device failed!\n");
794 err = pci_request_regions(pci, CR_DRIVER_NAME);
796 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
802 * Ask the SCSI layer to allocate a host structure, with extra
803 * space at the end for our private rtsx_dev structure.
805 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
807 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
809 goto scsi_host_alloc_fail;
812 dev = host_to_rtsx(host);
813 memset(dev, 0, sizeof(struct rtsx_dev));
815 dev->chip = kzalloc(sizeof(*dev->chip), GFP_KERNEL);
818 goto chip_alloc_fail;
821 spin_lock_init(&dev->reg_lock);
822 mutex_init(&dev->dev_mutex);
823 init_completion(&dev->cmnd_ready);
824 init_completion(&dev->control_exit);
825 init_completion(&dev->polling_exit);
826 init_completion(&dev->notify);
827 init_completion(&dev->scanning_done);
828 init_waitqueue_head(&dev->delay_wait);
833 dev_info(&pci->dev, "Resource length: 0x%x\n",
834 (unsigned int)pci_resource_len(pci, 0));
835 dev->addr = pci_resource_start(pci, 0);
836 dev->remap_addr = ioremap(dev->addr, pci_resource_len(pci, 0));
837 if (!dev->remap_addr) {
838 dev_err(&pci->dev, "ioremap error\n");
844 * Using "unsigned long" cast here to eliminate gcc warning in
847 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
848 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
850 dev->rtsx_resv_buf = dmam_alloc_coherent(&pci->dev, RTSX_RESV_BUF_LEN,
851 &dev->rtsx_resv_buf_addr,
853 if (!dev->rtsx_resv_buf) {
854 dev_err(&pci->dev, "alloc dma buffer fail\n");
858 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
859 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
860 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
861 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
864 dev->chip->rtsx = dev;
866 rtsx_init_options(dev->chip);
868 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
870 if (dev->chip->msi_en) {
871 if (pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) < 0)
872 dev->chip->msi_en = 0;
875 if (rtsx_acquire_irq(dev) < 0) {
877 goto irq_acquire_fail;
881 synchronize_irq(dev->irq);
883 rtsx_init_chip(dev->chip);
886 * set the supported max_lun and max_id for the scsi host
887 * NOTE: the minimal value of max_id is 1
890 host->max_lun = dev->chip->max_lun;
892 /* Start up our control thread */
893 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
895 dev_err(&pci->dev, "Unable to start control thread\n");
897 goto control_thread_fail;
899 dev->ctl_thread = th;
901 err = scsi_add_host(host, &pci->dev);
903 dev_err(&pci->dev, "Unable to add the scsi host\n");
904 goto scsi_add_host_fail;
907 /* Start up the thread for delayed SCSI-device scanning */
908 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
910 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
911 complete(&dev->scanning_done);
913 goto scan_thread_fail;
916 /* Start up the thread for polling thread */
917 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
919 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
921 goto scan_thread_fail;
923 dev->polling_thread = th;
925 pci_set_drvdata(pci, dev);
929 /* We come here if there are any problems */
931 quiesce_and_remove_host(dev);
933 complete(&dev->cmnd_ready);
934 wait_for_completion(&dev->control_exit);
936 free_irq(dev->irq, (void *)dev);
937 rtsx_release_chip(dev->chip);
939 dev->chip->host_cmds_ptr = NULL;
940 dev->chip->host_sg_tbl_ptr = NULL;
941 if (dev->chip->msi_en)
942 pci_free_irq_vectors(dev->pci);
944 iounmap(dev->remap_addr);
948 dev_err(&pci->dev, "%s failed\n", __func__);
950 scsi_host_alloc_fail:
951 pci_release_regions(pci);
955 static void rtsx_remove(struct pci_dev *pci)
957 struct rtsx_dev *dev = pci_get_drvdata(pci);
959 quiesce_and_remove_host(dev);
960 release_everything(dev);
961 pci_release_regions(pci);
965 static const struct pci_device_id rtsx_ids[] = {
966 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208),
967 PCI_CLASS_OTHERS << 16, 0xFF0000 },
968 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288),
969 PCI_CLASS_OTHERS << 16, 0xFF0000 },
973 MODULE_DEVICE_TABLE(pci, rtsx_ids);
975 static SIMPLE_DEV_PM_OPS(rtsx_pm_ops, rtsx_suspend, rtsx_resume);
977 /* pci_driver definition */
978 static struct pci_driver rtsx_driver = {
979 .name = CR_DRIVER_NAME,
980 .id_table = rtsx_ids,
982 .remove = rtsx_remove,
983 .driver.pm = &rtsx_pm_ops,
984 .shutdown = rtsx_shutdown,
987 module_pci_driver(rtsx_driver);