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>
33 MODULE_DESCRIPTION("Realtek PCI-Express card reader rts5208/rts5288 driver");
34 MODULE_LICENSE("GPL");
36 static unsigned int delay_use = 1;
37 module_param(delay_use, uint, 0644);
38 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
41 module_param(ss_en, int, 0644);
42 MODULE_PARM_DESC(ss_en, "enable selective suspend");
44 static int ss_interval = 50;
45 module_param(ss_interval, int, 0644);
46 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
48 static int auto_delink_en;
49 module_param(auto_delink_en, int, 0644);
50 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
52 static unsigned char aspm_l0s_l1_en;
53 module_param(aspm_l0s_l1_en, byte, 0644);
54 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
57 module_param(msi_en, int, 0644);
58 MODULE_PARM_DESC(msi_en, "enable msi");
60 static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
62 /***********************************************************************
64 ***********************************************************************/
66 static const char *host_info(struct Scsi_Host *host)
68 return "SCSI emulation for PCI-Express Mass Storage devices";
71 static int slave_alloc(struct scsi_device *sdev)
74 * Set the INQUIRY transfer length to 36. We don't use any of
75 * the extra data and many devices choke if asked for more or
78 sdev->inquiry_len = 36;
82 static int slave_configure(struct scsi_device *sdev)
85 * Scatter-gather buffers (all but the last) must have a length
86 * divisible by the bulk maxpacket size. Otherwise a data packet
87 * would end up being short, causing a premature end to the data
88 * transfer. Since high-speed bulk pipes have a maxpacket size
89 * of 512, we'll use that as the scsi device queue's DMA alignment
90 * mask. Guaranteeing proper alignment of the first buffer will
91 * have the desired effect because, except at the beginning and
92 * the end, scatter-gather buffers follow page boundaries.
94 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
96 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
97 * what is originally reported. We need this to avoid confusing
98 * the SCSI layer with devices that report 0 or 1, but need 10-byte
99 * commands (ala ATAPI devices behind certain bridges, or devices
100 * which simply have broken INQUIRY data).
102 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
103 * actual information. This seems to be the preference for
104 * programs like that.
106 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
107 * the actual value or the modified one, depending on where the
110 if (sdev->scsi_level < SCSI_2)
111 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
116 /***********************************************************************
117 * /proc/scsi/ functions
118 ***********************************************************************/
120 /* we use this macro to help us write into the buffer */
122 #define SPRINTF(args...) \
123 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
125 /* queue a command */
126 /* This is always called with scsi_lock(host) held */
127 static int queuecommand_lck(struct scsi_cmnd *srb,
128 void (*done)(struct scsi_cmnd *))
130 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
131 struct rtsx_chip *chip = dev->chip;
133 /* check for state-transition errors */
135 dev_err(&dev->pci->dev, "Error: chip->srb = %p\n",
137 return SCSI_MLQUEUE_HOST_BUSY;
140 /* fail the command if we are disconnecting */
141 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
142 dev_info(&dev->pci->dev, "Fail command during disconnect\n");
143 srb->result = DID_NO_CONNECT << 16;
148 /* enqueue the command and wake up the control thread */
149 srb->scsi_done = done;
151 complete(&dev->cmnd_ready);
156 static DEF_SCSI_QCMD(queuecommand)
158 /***********************************************************************
159 * Error handling functions
160 ***********************************************************************/
162 /* Command timeout and abort */
163 static int command_abort(struct scsi_cmnd *srb)
165 struct Scsi_Host *host = srb->device->host;
166 struct rtsx_dev *dev = host_to_rtsx(host);
167 struct rtsx_chip *chip = dev->chip;
169 dev_info(&dev->pci->dev, "%s called\n", __func__);
173 /* Is this command still active? */
174 if (chip->srb != srb) {
176 dev_info(&dev->pci->dev, "-- nothing to abort\n");
180 rtsx_set_stat(chip, RTSX_STAT_ABORT);
184 /* Wait for the aborted command to finish */
185 wait_for_completion(&dev->notify);
191 * This invokes the transport reset mechanism to reset the state of the
194 static int device_reset(struct scsi_cmnd *srb)
197 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
199 dev_info(&dev->pci->dev, "%s called\n", __func__);
201 return result < 0 ? FAILED : SUCCESS;
204 /* Simulate a SCSI bus reset by resetting the device's USB port. */
205 static int bus_reset(struct scsi_cmnd *srb)
208 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
210 dev_info(&dev->pci->dev, "%s called\n", __func__);
212 return result < 0 ? FAILED : SUCCESS;
216 * this defines our host template, with which we'll allocate hosts
219 static struct scsi_host_template rtsx_host_template = {
220 /* basic userland interface stuff */
221 .name = CR_DRIVER_NAME,
222 .proc_name = CR_DRIVER_NAME,
225 /* command interface -- queued only */
226 .queuecommand = queuecommand,
228 /* error and abort handlers */
229 .eh_abort_handler = command_abort,
230 .eh_device_reset_handler = device_reset,
231 .eh_bus_reset_handler = bus_reset,
233 /* queue commands only, only one command per LUN */
236 /* unknown initiator id */
239 .slave_alloc = slave_alloc,
240 .slave_configure = slave_configure,
242 /* lots of sg segments can be handled */
243 .sg_tablesize = SG_ALL,
245 /* limit the total size of a transfer to 120 KB */
248 /* merge commands... this seems to help performance, but
249 * periodically someone should test to see which setting is more
257 /* we do our own delay after a device or bus reset */
258 .skip_settle_delay = 1,
260 /* module management */
261 .module = THIS_MODULE
264 static int rtsx_acquire_irq(struct rtsx_dev *dev)
266 struct rtsx_chip *chip = dev->chip;
268 dev_info(&dev->pci->dev, "%s: chip->msi_en = %d, pci->irq = %d\n",
269 __func__, chip->msi_en, dev->pci->irq);
271 if (request_irq(dev->pci->irq, rtsx_interrupt,
272 chip->msi_en ? 0 : IRQF_SHARED,
273 CR_DRIVER_NAME, dev)) {
274 dev_err(&dev->pci->dev,
275 "rtsx: unable to grab IRQ %d, disabling device\n",
280 dev->irq = dev->pci->irq;
281 pci_intx(dev->pci, !chip->msi_en);
286 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val)
288 struct pci_dev *pdev;
290 u8 devfn = (dev << 3) | func;
292 pdev = pci_get_bus_and_slot(bus, devfn);
296 pci_read_config_byte(pdev, offset, &data);
307 static int rtsx_suspend(struct pci_dev *pci, pm_message_t state)
309 struct rtsx_dev *dev = pci_get_drvdata(pci);
310 struct rtsx_chip *chip;
315 /* lock the device pointers */
316 mutex_lock(&(dev->dev_mutex));
320 rtsx_do_before_power_down(chip, PM_S3);
323 free_irq(dev->irq, (void *)dev);
328 pci_disable_msi(pci);
331 pci_enable_wake(pci, pci_choose_state(pci, state), 1);
332 pci_disable_device(pci);
333 pci_set_power_state(pci, pci_choose_state(pci, state));
335 /* unlock the device pointers */
336 mutex_unlock(&dev->dev_mutex);
341 static int rtsx_resume(struct pci_dev *pci)
343 struct rtsx_dev *dev = pci_get_drvdata(pci);
344 struct rtsx_chip *chip;
351 /* lock the device pointers */
352 mutex_lock(&(dev->dev_mutex));
354 pci_set_power_state(pci, PCI_D0);
355 pci_restore_state(pci);
356 if (pci_enable_device(pci) < 0) {
357 dev_err(&dev->pci->dev,
358 "%s: pci_enable_device failed, disabling device\n",
360 /* unlock the device pointers */
361 mutex_unlock(&dev->dev_mutex);
367 if (pci_enable_msi(pci) < 0)
371 if (rtsx_acquire_irq(dev) < 0) {
372 /* unlock the device pointers */
373 mutex_unlock(&dev->dev_mutex);
377 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
378 rtsx_init_chip(chip);
380 /* unlock the device pointers */
381 mutex_unlock(&dev->dev_mutex);
385 #endif /* CONFIG_PM */
387 static void rtsx_shutdown(struct pci_dev *pci)
389 struct rtsx_dev *dev = pci_get_drvdata(pci);
390 struct rtsx_chip *chip;
397 rtsx_do_before_power_down(chip, PM_S1);
400 free_irq(dev->irq, (void *)dev);
405 pci_disable_msi(pci);
407 pci_disable_device(pci);
410 static int rtsx_control_thread(void *__dev)
412 struct rtsx_dev *dev = __dev;
413 struct rtsx_chip *chip = dev->chip;
414 struct Scsi_Host *host = rtsx_to_host(dev);
417 if (wait_for_completion_interruptible(&dev->cmnd_ready))
420 /* lock the device pointers */
421 mutex_lock(&(dev->dev_mutex));
423 /* if the device has disconnected, we are free to exit */
424 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
425 dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
426 mutex_unlock(&dev->dev_mutex);
430 /* lock access to the state */
433 /* has the command aborted ? */
434 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
435 chip->srb->result = DID_ABORT << 16;
441 /* reject the command if the direction indicator
444 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
445 dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
446 chip->srb->result = DID_ERROR << 16;
449 /* reject if target != 0 or if LUN is higher than
450 * the maximum known LUN
452 else if (chip->srb->device->id) {
453 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
454 chip->srb->device->id,
455 (u8)chip->srb->device->lun);
456 chip->srb->result = DID_BAD_TARGET << 16;
459 else if (chip->srb->device->lun > chip->max_lun) {
460 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
461 chip->srb->device->id,
462 (u8)chip->srb->device->lun);
463 chip->srb->result = DID_BAD_TARGET << 16;
466 /* we've got a command, let's do it! */
468 scsi_show_command(chip);
469 rtsx_invoke_transport(chip->srb, chip);
472 /* lock access to the state */
475 /* did the command already complete because of a disconnect? */
477 ; /* nothing to do */
479 /* indicate that the command is done */
480 else if (chip->srb->result != DID_ABORT << 16) {
481 chip->srb->scsi_done(chip->srb);
484 dev_err(&dev->pci->dev, "scsi command aborted\n");
487 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
488 complete(&(dev->notify));
490 rtsx_set_stat(chip, RTSX_STAT_IDLE);
493 /* finished working on this command */
497 /* unlock the device pointers */
498 mutex_unlock(&dev->dev_mutex);
501 /* notify the exit routine that we're actually exiting now
503 * complete()/wait_for_completion() is similar to up()/down(),
504 * except that complete() is safe in the case where the structure
505 * is getting deleted in a parallel mode of execution (i.e. just
506 * after the down() -- that's necessary for the thread-shutdown
509 * complete_and_exit() goes even further than this -- it is safe in
510 * the case that the thread of the caller is going away (not just
511 * the structure) -- this is necessary for the module-remove case.
512 * This is important in preemption kernels, which transfer the flow
513 * of execution immediately upon a complete().
515 complete_and_exit(&dev->control_exit, 0);
518 static int rtsx_polling_thread(void *__dev)
520 struct rtsx_dev *dev = __dev;
521 struct rtsx_chip *chip = dev->chip;
522 struct sd_info *sd_card = &(chip->sd_card);
523 struct xd_info *xd_card = &(chip->xd_card);
524 struct ms_info *ms_card = &(chip->ms_card);
526 sd_card->cleanup_counter = 0;
527 xd_card->cleanup_counter = 0;
528 ms_card->cleanup_counter = 0;
530 /* Wait until SCSI scan finished */
531 wait_timeout((delay_use + 5) * 1000);
535 set_current_state(TASK_INTERRUPTIBLE);
536 schedule_timeout(msecs_to_jiffies(POLLING_INTERVAL));
538 /* lock the device pointers */
539 mutex_lock(&(dev->dev_mutex));
541 /* if the device has disconnected, we are free to exit */
542 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
543 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
544 mutex_unlock(&dev->dev_mutex);
548 mutex_unlock(&dev->dev_mutex);
550 mspro_polling_format_status(chip);
552 /* lock the device pointers */
553 mutex_lock(&(dev->dev_mutex));
555 rtsx_polling_func(chip);
557 /* unlock the device pointers */
558 mutex_unlock(&dev->dev_mutex);
561 complete_and_exit(&dev->polling_exit, 0);
567 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
569 struct rtsx_dev *dev = dev_id;
570 struct rtsx_chip *chip;
582 spin_lock(&dev->reg_lock);
584 retval = rtsx_pre_handle_interrupt(chip);
585 if (retval == STATUS_FAIL) {
586 spin_unlock(&dev->reg_lock);
587 if (chip->int_reg == 0xFFFFFFFF)
592 status = chip->int_reg;
594 if (dev->check_card_cd) {
595 if (!(dev->check_card_cd & status)) {
596 /* card not exist, return TRANS_RESULT_FAIL */
597 dev->trans_result = TRANS_RESULT_FAIL;
604 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
605 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
606 if (status & DELINK_INT)
607 RTSX_SET_DELINK(chip);
608 dev->trans_result = TRANS_RESULT_FAIL;
611 } else if (status & TRANS_OK_INT) {
612 dev->trans_result = TRANS_RESULT_OK;
615 } else if (status & DATA_DONE_INT) {
616 dev->trans_result = TRANS_NOT_READY;
617 if (dev->done && (dev->trans_state == STATE_TRANS_SG))
623 spin_unlock(&dev->reg_lock);
627 /* Release all our dynamic resources */
628 static void rtsx_release_resources(struct rtsx_dev *dev)
630 dev_info(&dev->pci->dev, "-- %s\n", __func__);
632 /* Tell the control thread to exit. The SCSI host must
633 * already have been removed so it won't try to queue
636 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
637 complete(&dev->cmnd_ready);
639 wait_for_completion(&dev->control_exit);
640 if (dev->polling_thread)
641 wait_for_completion(&dev->polling_exit);
645 if (dev->rtsx_resv_buf) {
646 dev->chip->host_cmds_ptr = NULL;
647 dev->chip->host_sg_tbl_ptr = NULL;
651 free_irq(dev->irq, (void *)dev);
652 if (dev->chip->msi_en)
653 pci_disable_msi(dev->pci);
655 iounmap(dev->remap_addr);
657 rtsx_release_chip(dev->chip);
662 * First stage of disconnect processing: stop all commands and remove
665 static void quiesce_and_remove_host(struct rtsx_dev *dev)
667 struct Scsi_Host *host = rtsx_to_host(dev);
668 struct rtsx_chip *chip = dev->chip;
671 * Prevent new transfers, stop the current command, and
672 * interrupt a SCSI-scan or device-reset delay
674 mutex_lock(&dev->dev_mutex);
676 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
678 mutex_unlock(&dev->dev_mutex);
679 wake_up(&dev->delay_wait);
680 wait_for_completion(&dev->scanning_done);
682 /* Wait some time to let other threads exist */
686 * queuecommand won't accept any new commands and the control
687 * thread won't execute a previously-queued command. If there
688 * is such a command pending, complete it with an error.
690 mutex_lock(&dev->dev_mutex);
692 chip->srb->result = DID_NO_CONNECT << 16;
694 chip->srb->scsi_done(dev->chip->srb);
698 mutex_unlock(&dev->dev_mutex);
700 /* Now we own no commands so it's safe to remove the SCSI host */
701 scsi_remove_host(host);
704 /* Second stage of disconnect processing: deallocate all resources */
705 static void release_everything(struct rtsx_dev *dev)
707 rtsx_release_resources(dev);
710 * Drop our reference to the host; the SCSI core will free it
711 * when the refcount becomes 0.
713 scsi_host_put(rtsx_to_host(dev));
716 /* Thread to carry out delayed SCSI-device scanning */
717 static int rtsx_scan_thread(void *__dev)
719 struct rtsx_dev *dev = __dev;
720 struct rtsx_chip *chip = dev->chip;
722 /* Wait for the timeout to expire or for a disconnect */
724 dev_info(&dev->pci->dev,
725 "%s: waiting for device to settle before scanning\n",
727 wait_event_interruptible_timeout(dev->delay_wait,
728 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
732 /* If the device is still connected, perform the scanning */
733 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
734 scsi_scan_host(rtsx_to_host(dev));
735 dev_info(&dev->pci->dev, "%s: device scan complete\n",
738 /* Should we unbind if no devices were detected? */
741 complete_and_exit(&dev->scanning_done, 0);
744 static void rtsx_init_options(struct rtsx_chip *chip)
746 chip->vendor_id = chip->rtsx->pci->vendor;
747 chip->product_id = chip->rtsx->pci->device;
750 chip->driver_first_load = 1;
751 #ifdef HW_AUTO_SWITCH_SD_BUS
752 chip->sdio_in_charge = 0;
755 chip->mspro_formatter_enable = 1;
757 chip->use_hw_setting = 0;
758 chip->lun_mode = DEFAULT_SINGLE;
759 chip->auto_delink_en = auto_delink_en;
761 chip->ss_idle_period = ss_interval * 1000;
762 chip->remote_wakeup_en = 0;
763 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
764 chip->dynamic_aspm = 1;
765 chip->fpga_sd_sdr104_clk = CLK_200;
766 chip->fpga_sd_ddr50_clk = CLK_100;
767 chip->fpga_sd_sdr50_clk = CLK_100;
768 chip->fpga_sd_hs_clk = CLK_100;
769 chip->fpga_mmc_52m_clk = CLK_80;
770 chip->fpga_ms_hg_clk = CLK_80;
771 chip->fpga_ms_4bit_clk = CLK_80;
772 chip->fpga_ms_1bit_clk = CLK_40;
773 chip->asic_sd_sdr104_clk = 203;
774 chip->asic_sd_sdr50_clk = 98;
775 chip->asic_sd_ddr50_clk = 98;
776 chip->asic_sd_hs_clk = 98;
777 chip->asic_mmc_52m_clk = 98;
778 chip->asic_ms_hg_clk = 117;
779 chip->asic_ms_4bit_clk = 78;
780 chip->asic_ms_1bit_clk = 39;
781 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
782 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
783 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
784 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
785 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
786 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
787 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
788 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
790 chip->sd_speed_prior = 0x01040203;
791 chip->sd_current_prior = 0x00010203;
792 chip->sd_ctl = SD_PUSH_POINT_AUTO |
793 SD_SAMPLE_POINT_AUTO |
794 SUPPORT_MMC_DDR_MODE;
795 chip->sd_ddr_tx_phase = 0;
796 chip->mmc_ddr_tx_phase = 1;
797 chip->sd_default_tx_phase = 15;
798 chip->sd_default_rx_phase = 15;
799 chip->pmos_pwr_on_interval = 200;
800 chip->sd_voltage_switch_delay = 1000;
801 chip->ms_power_class_en = 3;
803 chip->sd_400mA_ocp_thd = 1;
804 chip->sd_800mA_ocp_thd = 5;
805 chip->ms_ocp_thd = 2;
807 chip->card_drive_sel = 0x55;
808 chip->sd30_drive_sel_1v8 = 0x03;
809 chip->sd30_drive_sel_3v3 = 0x01;
811 chip->do_delink_before_power_down = 1;
812 chip->auto_power_down = 1;
813 chip->polling_config = 0;
815 chip->force_clkreq_0 = 1;
816 chip->ft2_fast_mode = 0;
818 chip->sdio_retry_cnt = 1;
820 chip->xd_timeout = 2000;
821 chip->sd_timeout = 10000;
822 chip->ms_timeout = 2000;
823 chip->mspro_timeout = 15000;
825 chip->power_down_in_ss = 1;
831 chip->delink_stage1_step = 100;
832 chip->delink_stage2_step = 40;
833 chip->delink_stage3_step = 20;
835 chip->auto_delink_in_L1 = 1;
837 chip->msi_en = msi_en;
838 chip->hp_watch_bios_hotplug = 0;
839 chip->max_payload = 0;
840 chip->phy_voltage = 0;
842 chip->support_ms_8bit = 1;
843 chip->s3_pwr_off_delay = 1000;
846 static int rtsx_probe(struct pci_dev *pci,
847 const struct pci_device_id *pci_id)
849 struct Scsi_Host *host;
850 struct rtsx_dev *dev;
852 struct task_struct *th;
854 dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
856 err = pcim_enable_device(pci);
858 dev_err(&pci->dev, "PCI enable device failed!\n");
862 err = pci_request_regions(pci, CR_DRIVER_NAME);
864 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
870 * Ask the SCSI layer to allocate a host structure, with extra
871 * space at the end for our private rtsx_dev structure.
873 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
875 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
879 dev = host_to_rtsx(host);
880 memset(dev, 0, sizeof(struct rtsx_dev));
882 dev->chip = kzalloc(sizeof(struct rtsx_chip), GFP_KERNEL);
888 spin_lock_init(&dev->reg_lock);
889 mutex_init(&(dev->dev_mutex));
890 init_completion(&dev->cmnd_ready);
891 init_completion(&dev->control_exit);
892 init_completion(&dev->polling_exit);
893 init_completion(&(dev->notify));
894 init_completion(&dev->scanning_done);
895 init_waitqueue_head(&dev->delay_wait);
900 dev_info(&pci->dev, "Resource length: 0x%x\n",
901 (unsigned int)pci_resource_len(pci, 0));
902 dev->addr = pci_resource_start(pci, 0);
903 dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
904 if (!dev->remap_addr) {
905 dev_err(&pci->dev, "ioremap error\n");
911 * Using "unsigned long" cast here to eliminate gcc warning in
914 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
915 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
917 dev->rtsx_resv_buf = dmam_alloc_coherent(&pci->dev, RTSX_RESV_BUF_LEN,
918 &dev->rtsx_resv_buf_addr, GFP_KERNEL);
919 if (!dev->rtsx_resv_buf) {
920 dev_err(&pci->dev, "alloc dma buffer fail\n");
924 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
925 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
926 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
927 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
930 dev->chip->rtsx = dev;
932 rtsx_init_options(dev->chip);
934 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
936 if (dev->chip->msi_en) {
937 if (pci_enable_msi(pci) < 0)
938 dev->chip->msi_en = 0;
941 if (rtsx_acquire_irq(dev) < 0) {
947 synchronize_irq(dev->irq);
949 rtsx_init_chip(dev->chip);
952 * set the supported max_lun and max_id for the scsi host
953 * NOTE: the minimal value of max_id is 1
956 host->max_lun = dev->chip->max_lun;
958 /* Start up our control thread */
959 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
961 dev_err(&pci->dev, "Unable to start control thread\n");
965 dev->ctl_thread = th;
967 err = scsi_add_host(host, &pci->dev);
969 dev_err(&pci->dev, "Unable to add the scsi host\n");
973 /* Start up the thread for delayed SCSI-device scanning */
974 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
976 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
977 complete(&dev->scanning_done);
978 quiesce_and_remove_host(dev);
983 /* Start up the thread for polling thread */
984 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
986 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
987 quiesce_and_remove_host(dev);
991 dev->polling_thread = th;
993 pci_set_drvdata(pci, dev);
997 /* We come here if there are any problems */
999 dev_err(&pci->dev, "rtsx_probe() failed\n");
1000 release_everything(dev);
1005 static void rtsx_remove(struct pci_dev *pci)
1007 struct rtsx_dev *dev = pci_get_drvdata(pci);
1009 dev_info(&pci->dev, "rtsx_remove() called\n");
1011 quiesce_and_remove_host(dev);
1012 release_everything(dev);
1016 static const struct pci_device_id rtsx_ids[] = {
1017 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208),
1018 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1019 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288),
1020 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1024 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1026 /* pci_driver definition */
1027 static struct pci_driver rtsx_driver = {
1028 .name = CR_DRIVER_NAME,
1029 .id_table = rtsx_ids,
1030 .probe = rtsx_probe,
1031 .remove = rtsx_remove,
1033 .suspend = rtsx_suspend,
1034 .resume = rtsx_resume,
1036 .shutdown = rtsx_shutdown,
1039 module_pci_driver(rtsx_driver);