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, S_IRUGO | S_IWUSR);
38 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
41 module_param(ss_en, int, S_IRUGO | S_IWUSR);
42 MODULE_PARM_DESC(ss_en, "enable selective suspend");
44 static int ss_interval = 50;
45 module_param(ss_interval, int, S_IRUGO | S_IWUSR);
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, S_IRUGO | S_IWUSR);
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, S_IRUGO | S_IWUSR);
54 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
57 module_param(msi_en, int, S_IRUGO | S_IWUSR);
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)
84 /* Scatter-gather buffers (all but the last) must have a length
85 * divisible by the bulk maxpacket size. Otherwise a data packet
86 * would end up being short, causing a premature end to the data
87 * transfer. Since high-speed bulk pipes have a maxpacket size
88 * of 512, we'll use that as the scsi device queue's DMA alignment
89 * mask. Guaranteeing proper alignment of the first buffer will
90 * have the desired effect because, except at the beginning and
91 * the end, scatter-gather buffers follow page boundaries. */
92 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
94 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
95 * what is originally reported. We need this to avoid confusing
96 * the SCSI layer with devices that report 0 or 1, but need 10-byte
97 * commands (ala ATAPI devices behind certain bridges, or devices
98 * which simply have broken INQUIRY data).
100 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
101 * actual information. This seems to be the preference for
102 * programs like that.
104 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
105 * the actual value or the modified one, depending on where the
108 if (sdev->scsi_level < SCSI_2)
109 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
115 /***********************************************************************
116 * /proc/scsi/ functions
117 ***********************************************************************/
119 /* we use this macro to help us write into the buffer */
121 #define SPRINTF(args...) \
122 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
124 /* queue a command */
125 /* This is always called with scsi_lock(host) held */
126 static int queuecommand_lck(struct scsi_cmnd *srb,
127 void (*done)(struct scsi_cmnd *))
129 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
130 struct rtsx_chip *chip = dev->chip;
132 /* check for state-transition errors */
133 if (chip->srb != NULL) {
134 dev_err(&dev->pci->dev, "Error: chip->srb = %p\n",
136 return SCSI_MLQUEUE_HOST_BUSY;
139 /* fail the command if we are disconnecting */
140 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
141 dev_info(&dev->pci->dev, "Fail command during disconnect\n");
142 srb->result = DID_NO_CONNECT << 16;
147 /* enqueue the command and wake up the control thread */
148 srb->scsi_done = done;
150 complete(&dev->cmnd_ready);
155 static DEF_SCSI_QCMD(queuecommand)
157 /***********************************************************************
158 * Error handling functions
159 ***********************************************************************/
161 /* Command timeout and abort */
162 static int command_abort(struct scsi_cmnd *srb)
164 struct Scsi_Host *host = srb->device->host;
165 struct rtsx_dev *dev = host_to_rtsx(host);
166 struct rtsx_chip *chip = dev->chip;
168 dev_info(&dev->pci->dev, "%s called\n", __func__);
172 /* Is this command still active? */
173 if (chip->srb != srb) {
175 dev_info(&dev->pci->dev, "-- nothing to abort\n");
179 rtsx_set_stat(chip, RTSX_STAT_ABORT);
183 /* Wait for the aborted command to finish */
184 wait_for_completion(&dev->notify);
189 /* This invokes the transport reset mechanism to reset the state of the
191 static int device_reset(struct scsi_cmnd *srb)
194 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
196 dev_info(&dev->pci->dev, "%s called\n", __func__);
198 return result < 0 ? FAILED : SUCCESS;
201 /* Simulate a SCSI bus reset by resetting the device's USB port. */
202 static int bus_reset(struct scsi_cmnd *srb)
205 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
207 dev_info(&dev->pci->dev, "%s called\n", __func__);
209 return result < 0 ? FAILED : SUCCESS;
214 * this defines our host template, with which we'll allocate hosts
217 static struct scsi_host_template rtsx_host_template = {
218 /* basic userland interface stuff */
219 .name = CR_DRIVER_NAME,
220 .proc_name = CR_DRIVER_NAME,
223 /* command interface -- queued only */
224 .queuecommand = queuecommand,
226 /* error and abort handlers */
227 .eh_abort_handler = command_abort,
228 .eh_device_reset_handler = device_reset,
229 .eh_bus_reset_handler = bus_reset,
231 /* queue commands only, only one command per LUN */
235 /* unknown initiator id */
238 .slave_alloc = slave_alloc,
239 .slave_configure = slave_configure,
241 /* lots of sg segments can be handled */
242 .sg_tablesize = SG_ALL,
244 /* limit the total size of a transfer to 120 KB */
247 /* merge commands... this seems to help performance, but
248 * periodically someone should test to see which setting is more
256 /* we do our own delay after a device or bus reset */
257 .skip_settle_delay = 1,
259 /* module management */
260 .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);
287 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val)
289 struct pci_dev *pdev;
291 u8 devfn = (dev << 3) | func;
293 pdev = pci_get_bus_and_slot(bus, devfn);
297 pci_read_config_byte(pdev, offset, &data);
308 static int rtsx_suspend(struct pci_dev *pci, pm_message_t state)
310 struct rtsx_dev *dev = pci_get_drvdata(pci);
311 struct rtsx_chip *chip;
316 /* lock the device pointers */
317 mutex_lock(&(dev->dev_mutex));
321 rtsx_do_before_power_down(chip, PM_S3);
324 synchronize_irq(dev->irq);
325 free_irq(dev->irq, (void *)dev);
330 pci_disable_msi(pci);
333 pci_enable_wake(pci, pci_choose_state(pci, state), 1);
334 pci_disable_device(pci);
335 pci_set_power_state(pci, pci_choose_state(pci, state));
337 /* unlock the device pointers */
338 mutex_unlock(&dev->dev_mutex);
343 static int rtsx_resume(struct pci_dev *pci)
345 struct rtsx_dev *dev = pci_get_drvdata(pci);
346 struct rtsx_chip *chip;
353 /* lock the device pointers */
354 mutex_lock(&(dev->dev_mutex));
356 pci_set_power_state(pci, PCI_D0);
357 pci_restore_state(pci);
358 if (pci_enable_device(pci) < 0) {
359 dev_err(&dev->pci->dev,
360 "%s: pci_enable_device failed, disabling device\n",
362 /* unlock the device pointers */
363 mutex_unlock(&dev->dev_mutex);
369 if (pci_enable_msi(pci) < 0)
373 if (rtsx_acquire_irq(dev) < 0) {
374 /* unlock the device pointers */
375 mutex_unlock(&dev->dev_mutex);
379 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
380 rtsx_init_chip(chip);
382 /* unlock the device pointers */
383 mutex_unlock(&dev->dev_mutex);
387 #endif /* CONFIG_PM */
389 static void rtsx_shutdown(struct pci_dev *pci)
391 struct rtsx_dev *dev = pci_get_drvdata(pci);
392 struct rtsx_chip *chip;
399 rtsx_do_before_power_down(chip, PM_S1);
402 synchronize_irq(dev->irq);
403 free_irq(dev->irq, (void *)dev);
408 pci_disable_msi(pci);
410 pci_disable_device(pci);
413 static int rtsx_control_thread(void *__dev)
415 struct rtsx_dev *dev = __dev;
416 struct rtsx_chip *chip = dev->chip;
417 struct Scsi_Host *host = rtsx_to_host(dev);
420 if (wait_for_completion_interruptible(&dev->cmnd_ready))
423 /* lock the device pointers */
424 mutex_lock(&(dev->dev_mutex));
426 /* if the device has disconnected, we are free to exit */
427 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
428 dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
429 mutex_unlock(&dev->dev_mutex);
433 /* lock access to the state */
436 /* has the command aborted ? */
437 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
438 chip->srb->result = DID_ABORT << 16;
444 /* reject the command if the direction indicator
447 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
448 dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
449 chip->srb->result = DID_ERROR << 16;
452 /* reject if target != 0 or if LUN is higher than
453 * the maximum known LUN
455 else if (chip->srb->device->id) {
456 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
457 chip->srb->device->id,
458 (u8)chip->srb->device->lun);
459 chip->srb->result = DID_BAD_TARGET << 16;
462 else if (chip->srb->device->lun > chip->max_lun) {
463 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
464 chip->srb->device->id,
465 (u8)chip->srb->device->lun);
466 chip->srb->result = DID_BAD_TARGET << 16;
469 /* we've got a command, let's do it! */
471 scsi_show_command(chip);
472 rtsx_invoke_transport(chip->srb, chip);
475 /* lock access to the state */
478 /* did the command already complete because of a disconnect? */
480 ; /* nothing to do */
482 /* indicate that the command is done */
483 else if (chip->srb->result != DID_ABORT << 16) {
484 chip->srb->scsi_done(chip->srb);
487 dev_err(&dev->pci->dev, "scsi command aborted\n");
490 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
491 complete(&(dev->notify));
493 rtsx_set_stat(chip, RTSX_STAT_IDLE);
496 /* finished working on this command */
500 /* unlock the device pointers */
501 mutex_unlock(&dev->dev_mutex);
504 /* notify the exit routine that we're actually exiting now
506 * complete()/wait_for_completion() is similar to up()/down(),
507 * except that complete() is safe in the case where the structure
508 * is getting deleted in a parallel mode of execution (i.e. just
509 * after the down() -- that's necessary for the thread-shutdown
512 * complete_and_exit() goes even further than this -- it is safe in
513 * the case that the thread of the caller is going away (not just
514 * the structure) -- this is necessary for the module-remove case.
515 * This is important in preemption kernels, which transfer the flow
516 * of execution immediately upon a complete().
518 complete_and_exit(&dev->control_exit, 0);
522 static int rtsx_polling_thread(void *__dev)
524 struct rtsx_dev *dev = __dev;
525 struct rtsx_chip *chip = dev->chip;
526 struct sd_info *sd_card = &(chip->sd_card);
527 struct xd_info *xd_card = &(chip->xd_card);
528 struct ms_info *ms_card = &(chip->ms_card);
530 sd_card->cleanup_counter = 0;
531 xd_card->cleanup_counter = 0;
532 ms_card->cleanup_counter = 0;
534 /* Wait until SCSI scan finished */
535 wait_timeout((delay_use + 5) * 1000);
539 set_current_state(TASK_INTERRUPTIBLE);
540 schedule_timeout(POLLING_INTERVAL);
542 /* lock the device pointers */
543 mutex_lock(&(dev->dev_mutex));
545 /* if the device has disconnected, we are free to exit */
546 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
547 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
548 mutex_unlock(&dev->dev_mutex);
552 mutex_unlock(&dev->dev_mutex);
554 mspro_polling_format_status(chip);
556 /* lock the device pointers */
557 mutex_lock(&(dev->dev_mutex));
559 rtsx_polling_func(chip);
561 /* unlock the device pointers */
562 mutex_unlock(&dev->dev_mutex);
565 complete_and_exit(&dev->polling_exit, 0);
571 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
573 struct rtsx_dev *dev = dev_id;
574 struct rtsx_chip *chip;
586 spin_lock(&dev->reg_lock);
588 retval = rtsx_pre_handle_interrupt(chip);
589 if (retval == STATUS_FAIL) {
590 spin_unlock(&dev->reg_lock);
591 if (chip->int_reg == 0xFFFFFFFF)
596 status = chip->int_reg;
598 if (dev->check_card_cd) {
599 if (!(dev->check_card_cd & status)) {
600 /* card not exist, return TRANS_RESULT_FAIL */
601 dev->trans_result = TRANS_RESULT_FAIL;
608 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
609 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
610 if (status & DELINK_INT)
611 RTSX_SET_DELINK(chip);
612 dev->trans_result = TRANS_RESULT_FAIL;
615 } else if (status & TRANS_OK_INT) {
616 dev->trans_result = TRANS_RESULT_OK;
619 } else if (status & DATA_DONE_INT) {
620 dev->trans_result = TRANS_NOT_READY;
621 if (dev->done && (dev->trans_state == STATE_TRANS_SG))
627 spin_unlock(&dev->reg_lock);
632 /* Release all our dynamic resources */
633 static void rtsx_release_resources(struct rtsx_dev *dev)
635 dev_info(&dev->pci->dev, "-- %s\n", __func__);
637 /* Tell the control thread to exit. The SCSI host must
638 * already have been removed so it won't try to queue
641 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
642 complete(&dev->cmnd_ready);
644 wait_for_completion(&dev->control_exit);
645 if (dev->polling_thread)
646 wait_for_completion(&dev->polling_exit);
650 if (dev->rtsx_resv_buf) {
651 dma_free_coherent(&(dev->pci->dev), RTSX_RESV_BUF_LEN,
652 dev->rtsx_resv_buf, dev->rtsx_resv_buf_addr);
653 dev->chip->host_cmds_ptr = NULL;
654 dev->chip->host_sg_tbl_ptr = NULL;
658 free_irq(dev->irq, (void *)dev);
659 if (dev->chip->msi_en)
660 pci_disable_msi(dev->pci);
662 iounmap(dev->remap_addr);
664 pci_disable_device(dev->pci);
665 pci_release_regions(dev->pci);
667 rtsx_release_chip(dev->chip);
671 /* First stage of disconnect processing: stop all commands and remove
673 static void quiesce_and_remove_host(struct rtsx_dev *dev)
675 struct Scsi_Host *host = rtsx_to_host(dev);
676 struct rtsx_chip *chip = dev->chip;
678 /* Prevent new transfers, stop the current command, and
679 * interrupt a SCSI-scan or device-reset delay */
680 mutex_lock(&dev->dev_mutex);
682 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
684 mutex_unlock(&dev->dev_mutex);
685 wake_up(&dev->delay_wait);
686 wait_for_completion(&dev->scanning_done);
688 /* Wait some time to let other threads exist */
691 /* queuecommand won't accept any new commands and the control
692 * thread won't execute a previously-queued command. If there
693 * is such a command pending, complete it with an error. */
694 mutex_lock(&dev->dev_mutex);
696 chip->srb->result = DID_NO_CONNECT << 16;
698 chip->srb->scsi_done(dev->chip->srb);
702 mutex_unlock(&dev->dev_mutex);
704 /* Now we own no commands so it's safe to remove the SCSI host */
705 scsi_remove_host(host);
708 /* Second stage of disconnect processing: deallocate all resources */
709 static void release_everything(struct rtsx_dev *dev)
711 rtsx_release_resources(dev);
713 /* Drop our reference to the host; the SCSI core will free it
714 * when the refcount becomes 0. */
715 scsi_host_put(rtsx_to_host(dev));
718 /* Thread to carry out delayed SCSI-device scanning */
719 static int rtsx_scan_thread(void *__dev)
721 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
722 struct rtsx_chip *chip = dev->chip;
724 /* Wait for the timeout to expire or for a disconnect */
726 dev_info(&dev->pci->dev,
727 "%s: waiting for device to settle before scanning\n",
729 wait_event_interruptible_timeout(dev->delay_wait,
730 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
734 /* If the device is still connected, perform the scanning */
735 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
736 scsi_scan_host(rtsx_to_host(dev));
737 dev_info(&dev->pci->dev, "%s: device scan complete\n",
740 /* Should we unbind if no devices were detected? */
743 complete_and_exit(&dev->scanning_done, 0);
746 static void rtsx_init_options(struct rtsx_chip *chip)
748 chip->vendor_id = chip->rtsx->pci->vendor;
749 chip->product_id = chip->rtsx->pci->device;
752 chip->driver_first_load = 1;
753 #ifdef HW_AUTO_SWITCH_SD_BUS
754 chip->sdio_in_charge = 0;
757 chip->mspro_formatter_enable = 1;
759 chip->use_hw_setting = 0;
760 chip->lun_mode = DEFAULT_SINGLE;
761 chip->auto_delink_en = auto_delink_en;
763 chip->ss_idle_period = ss_interval * 1000;
764 chip->remote_wakeup_en = 0;
765 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
766 chip->dynamic_aspm = 1;
767 chip->fpga_sd_sdr104_clk = CLK_200;
768 chip->fpga_sd_ddr50_clk = CLK_100;
769 chip->fpga_sd_sdr50_clk = CLK_100;
770 chip->fpga_sd_hs_clk = CLK_100;
771 chip->fpga_mmc_52m_clk = CLK_80;
772 chip->fpga_ms_hg_clk = CLK_80;
773 chip->fpga_ms_4bit_clk = CLK_80;
774 chip->fpga_ms_1bit_clk = CLK_40;
775 chip->asic_sd_sdr104_clk = 203;
776 chip->asic_sd_sdr50_clk = 98;
777 chip->asic_sd_ddr50_clk = 98;
778 chip->asic_sd_hs_clk = 98;
779 chip->asic_mmc_52m_clk = 98;
780 chip->asic_ms_hg_clk = 117;
781 chip->asic_ms_4bit_clk = 78;
782 chip->asic_ms_1bit_clk = 39;
783 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
784 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
785 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
786 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
787 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
788 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
789 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
790 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
792 chip->sd_speed_prior = 0x01040203;
793 chip->sd_current_prior = 0x00010203;
794 chip->sd_ctl = SD_PUSH_POINT_AUTO |
795 SD_SAMPLE_POINT_AUTO |
796 SUPPORT_MMC_DDR_MODE;
797 chip->sd_ddr_tx_phase = 0;
798 chip->mmc_ddr_tx_phase = 1;
799 chip->sd_default_tx_phase = 15;
800 chip->sd_default_rx_phase = 15;
801 chip->pmos_pwr_on_interval = 200;
802 chip->sd_voltage_switch_delay = 1000;
803 chip->ms_power_class_en = 3;
805 chip->sd_400mA_ocp_thd = 1;
806 chip->sd_800mA_ocp_thd = 5;
807 chip->ms_ocp_thd = 2;
809 chip->card_drive_sel = 0x55;
810 chip->sd30_drive_sel_1v8 = 0x03;
811 chip->sd30_drive_sel_3v3 = 0x01;
813 chip->do_delink_before_power_down = 1;
814 chip->auto_power_down = 1;
815 chip->polling_config = 0;
817 chip->force_clkreq_0 = 1;
818 chip->ft2_fast_mode = 0;
820 chip->sdio_retry_cnt = 1;
822 chip->xd_timeout = 2000;
823 chip->sd_timeout = 10000;
824 chip->ms_timeout = 2000;
825 chip->mspro_timeout = 15000;
827 chip->power_down_in_ss = 1;
833 chip->delink_stage1_step = 100;
834 chip->delink_stage2_step = 40;
835 chip->delink_stage3_step = 20;
837 chip->auto_delink_in_L1 = 1;
839 chip->msi_en = msi_en;
840 chip->hp_watch_bios_hotplug = 0;
841 chip->max_payload = 0;
842 chip->phy_voltage = 0;
844 chip->support_ms_8bit = 1;
845 chip->s3_pwr_off_delay = 1000;
848 static int rtsx_probe(struct pci_dev *pci,
849 const struct pci_device_id *pci_id)
851 struct Scsi_Host *host;
852 struct rtsx_dev *dev;
854 struct task_struct *th;
856 dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
858 err = pci_enable_device(pci);
860 dev_err(&pci->dev, "PCI enable device failed!\n");
864 err = pci_request_regions(pci, CR_DRIVER_NAME);
866 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
868 pci_disable_device(pci);
873 * Ask the SCSI layer to allocate a host structure, with extra
874 * space at the end for our private rtsx_dev structure.
876 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
878 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
879 pci_release_regions(pci);
880 pci_disable_device(pci);
884 dev = host_to_rtsx(host);
885 memset(dev, 0, sizeof(struct rtsx_dev));
887 dev->chip = kzalloc(sizeof(struct rtsx_chip), GFP_KERNEL);
888 if (dev->chip == NULL) {
893 spin_lock_init(&dev->reg_lock);
894 mutex_init(&(dev->dev_mutex));
895 init_completion(&dev->cmnd_ready);
896 init_completion(&dev->control_exit);
897 init_completion(&dev->polling_exit);
898 init_completion(&(dev->notify));
899 init_completion(&dev->scanning_done);
900 init_waitqueue_head(&dev->delay_wait);
905 dev_info(&pci->dev, "Resource length: 0x%x\n",
906 (unsigned int)pci_resource_len(pci, 0));
907 dev->addr = pci_resource_start(pci, 0);
908 dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
909 if (dev->remap_addr == NULL) {
910 dev_err(&pci->dev, "ioremap error\n");
916 * Using "unsigned long" cast here to eliminate gcc warning in
919 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
920 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
922 dev->rtsx_resv_buf = dma_alloc_coherent(&(pci->dev), RTSX_RESV_BUF_LEN,
923 &(dev->rtsx_resv_buf_addr), GFP_KERNEL);
924 if (dev->rtsx_resv_buf == NULL) {
925 dev_err(&pci->dev, "alloc dma buffer fail\n");
929 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
930 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
931 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
932 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
935 dev->chip->rtsx = dev;
937 rtsx_init_options(dev->chip);
939 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
941 if (dev->chip->msi_en) {
942 if (pci_enable_msi(pci) < 0)
943 dev->chip->msi_en = 0;
946 if (rtsx_acquire_irq(dev) < 0) {
952 synchronize_irq(dev->irq);
954 rtsx_init_chip(dev->chip);
956 /* set the supported max_lun and max_id for the scsi host
957 * NOTE: the minimal value of max_id is 1 */
959 host->max_lun = dev->chip->max_lun;
961 /* Start up our control thread */
962 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
964 dev_err(&pci->dev, "Unable to start control thread\n");
968 dev->ctl_thread = th;
970 err = scsi_add_host(host, &pci->dev);
972 dev_err(&pci->dev, "Unable to add the scsi host\n");
976 /* Start up the thread for delayed SCSI-device scanning */
977 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
979 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
980 complete(&dev->scanning_done);
981 quiesce_and_remove_host(dev);
986 /* Start up the thread for polling thread */
987 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
989 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
990 quiesce_and_remove_host(dev);
994 dev->polling_thread = th;
996 pci_set_drvdata(pci, dev);
1000 /* We come here if there are any problems */
1002 dev_err(&pci->dev, "rtsx_probe() failed\n");
1003 release_everything(dev);
1009 static void rtsx_remove(struct pci_dev *pci)
1011 struct rtsx_dev *dev = pci_get_drvdata(pci);
1013 dev_info(&pci->dev, "rtsx_remove() called\n");
1015 quiesce_and_remove_host(dev);
1016 release_everything(dev);
1018 pci_set_drvdata(pci, NULL);
1022 static const struct pci_device_id rtsx_ids[] = {
1023 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208),
1024 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1025 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288),
1026 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1030 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1032 /* pci_driver definition */
1033 static struct pci_driver rtsx_driver = {
1034 .name = CR_DRIVER_NAME,
1035 .id_table = rtsx_ids,
1036 .probe = rtsx_probe,
1037 .remove = rtsx_remove,
1039 .suspend = rtsx_suspend,
1040 .resume = rtsx_resume,
1042 .shutdown = rtsx_shutdown,
1045 module_pci_driver(rtsx_driver);