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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/blkdev.h>
26 #include <linux/kthread.h>
27 #include <linux/sched.h>
28 #include <linux/workqueue.h>
31 #include "rtsx_chip.h"
32 #include "rtsx_transport.h"
33 #include "rtsx_scsi.h"
34 #include "rtsx_card.h"
41 MODULE_DESCRIPTION("Realtek PCI-Express card reader rts5208/rts5288 driver");
42 MODULE_LICENSE("GPL");
44 static unsigned int delay_use = 1;
45 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
46 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
49 module_param(ss_en, int, S_IRUGO | S_IWUSR);
50 MODULE_PARM_DESC(ss_en, "enable selective suspend");
52 static int ss_interval = 50;
53 module_param(ss_interval, int, S_IRUGO | S_IWUSR);
54 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
56 static int auto_delink_en;
57 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
60 static unsigned char aspm_l0s_l1_en;
61 module_param(aspm_l0s_l1_en, byte, S_IRUGO | S_IWUSR);
62 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
65 module_param(msi_en, int, S_IRUGO | S_IWUSR);
66 MODULE_PARM_DESC(msi_en, "enable msi");
68 static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
70 /***********************************************************************
72 ***********************************************************************/
74 static const char *host_info(struct Scsi_Host *host)
76 return "SCSI emulation for PCI-Express Mass Storage devices";
79 static int slave_alloc(struct scsi_device *sdev)
82 * Set the INQUIRY transfer length to 36. We don't use any of
83 * the extra data and many devices choke if asked for more or
86 sdev->inquiry_len = 36;
90 static int slave_configure(struct scsi_device *sdev)
92 /* Scatter-gather buffers (all but the last) must have a length
93 * divisible by the bulk maxpacket size. Otherwise a data packet
94 * would end up being short, causing a premature end to the data
95 * transfer. Since high-speed bulk pipes have a maxpacket size
96 * of 512, we'll use that as the scsi device queue's DMA alignment
97 * mask. Guaranteeing proper alignment of the first buffer will
98 * have the desired effect because, except at the beginning and
99 * the end, scatter-gather buffers follow page boundaries. */
100 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
102 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
103 * what is originally reported. We need this to avoid confusing
104 * the SCSI layer with devices that report 0 or 1, but need 10-byte
105 * commands (ala ATAPI devices behind certain bridges, or devices
106 * which simply have broken INQUIRY data).
108 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
109 * actual information. This seems to be the preference for
110 * programs like that.
112 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
113 * the actual value or the modified one, depending on where the
116 if (sdev->scsi_level < SCSI_2)
117 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
123 /***********************************************************************
124 * /proc/scsi/ functions
125 ***********************************************************************/
127 /* we use this macro to help us write into the buffer */
129 #define SPRINTF(args...) \
130 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
132 /* queue a command */
133 /* This is always called with scsi_lock(host) held */
134 static int queuecommand_lck(struct scsi_cmnd *srb,
135 void (*done)(struct scsi_cmnd *))
137 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
138 struct rtsx_chip *chip = dev->chip;
140 /* check for state-transition errors */
141 if (chip->srb != NULL) {
142 dev_err(&dev->pci->dev, "Error in %s: chip->srb = %p\n",
143 __func__, chip->srb);
144 return SCSI_MLQUEUE_HOST_BUSY;
147 /* fail the command if we are disconnecting */
148 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
149 dev_info(&dev->pci->dev, "Fail command during disconnect\n");
150 srb->result = DID_NO_CONNECT << 16;
155 /* enqueue the command and wake up the control thread */
156 srb->scsi_done = done;
158 complete(&dev->cmnd_ready);
163 static DEF_SCSI_QCMD(queuecommand)
165 /***********************************************************************
166 * Error handling functions
167 ***********************************************************************/
169 /* Command timeout and abort */
170 static int command_abort(struct scsi_cmnd *srb)
172 struct Scsi_Host *host = srb->device->host;
173 struct rtsx_dev *dev = host_to_rtsx(host);
174 struct rtsx_chip *chip = dev->chip;
176 dev_info(&dev->pci->dev, "%s called\n", __func__);
180 /* Is this command still active? */
181 if (chip->srb != srb) {
183 dev_info(&dev->pci->dev, "-- nothing to abort\n");
187 rtsx_set_stat(chip, RTSX_STAT_ABORT);
191 /* Wait for the aborted command to finish */
192 wait_for_completion(&dev->notify);
197 /* This invokes the transport reset mechanism to reset the state of the
199 static int device_reset(struct scsi_cmnd *srb)
202 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
204 dev_info(&dev->pci->dev, "%s called\n", __func__);
206 return result < 0 ? FAILED : SUCCESS;
209 /* Simulate a SCSI bus reset by resetting the device's USB port. */
210 static int bus_reset(struct scsi_cmnd *srb)
213 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
215 dev_info(&dev->pci->dev, "%s called\n", __func__);
217 return result < 0 ? FAILED : SUCCESS;
222 * this defines our host template, with which we'll allocate hosts
225 static struct scsi_host_template rtsx_host_template = {
226 /* basic userland interface stuff */
227 .name = CR_DRIVER_NAME,
228 .proc_name = CR_DRIVER_NAME,
231 /* command interface -- queued only */
232 .queuecommand = queuecommand,
234 /* error and abort handlers */
235 .eh_abort_handler = command_abort,
236 .eh_device_reset_handler = device_reset,
237 .eh_bus_reset_handler = bus_reset,
239 /* queue commands only, only one command per LUN */
243 /* unknown initiator id */
246 .slave_alloc = slave_alloc,
247 .slave_configure = slave_configure,
249 /* lots of sg segments can be handled */
250 .sg_tablesize = SG_ALL,
252 /* limit the total size of a transfer to 120 KB */
255 /* merge commands... this seems to help performance, but
256 * periodically someone should test to see which setting is more
264 /* we do our own delay after a device or bus reset */
265 .skip_settle_delay = 1,
267 /* module management */
268 .module = THIS_MODULE
272 static int rtsx_acquire_irq(struct rtsx_dev *dev)
274 struct rtsx_chip *chip = dev->chip;
276 dev_info(&dev->pci->dev, "%s: chip->msi_en = %d, pci->irq = %d\n",
277 __func__, chip->msi_en, dev->pci->irq);
279 if (request_irq(dev->pci->irq, rtsx_interrupt,
280 chip->msi_en ? 0 : IRQF_SHARED,
281 CR_DRIVER_NAME, dev)) {
282 dev_err(&dev->pci->dev,
283 "rtsx: unable to grab IRQ %d, disabling device\n",
288 dev->irq = dev->pci->irq;
289 pci_intx(dev->pci, !chip->msi_en);
295 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val)
297 struct pci_dev *pdev;
299 u8 devfn = (dev << 3) | func;
301 pdev = pci_get_bus_and_slot(bus, devfn);
305 pci_read_config_byte(pdev, offset, &data);
316 static int rtsx_suspend(struct pci_dev *pci, pm_message_t state)
318 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
319 struct rtsx_chip *chip;
324 /* lock the device pointers */
325 mutex_lock(&(dev->dev_mutex));
329 rtsx_do_before_power_down(chip, PM_S3);
332 synchronize_irq(dev->irq);
333 free_irq(dev->irq, (void *)dev);
338 pci_disable_msi(pci);
341 pci_enable_wake(pci, pci_choose_state(pci, state), 1);
342 pci_disable_device(pci);
343 pci_set_power_state(pci, pci_choose_state(pci, state));
345 /* unlock the device pointers */
346 mutex_unlock(&dev->dev_mutex);
351 static int rtsx_resume(struct pci_dev *pci)
353 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
354 struct rtsx_chip *chip;
361 /* lock the device pointers */
362 mutex_lock(&(dev->dev_mutex));
364 pci_set_power_state(pci, PCI_D0);
365 pci_restore_state(pci);
366 if (pci_enable_device(pci) < 0) {
367 dev_err(&dev->pci->dev,
368 "%s: pci_enable_device failed, disabling device\n",
370 /* unlock the device pointers */
371 mutex_unlock(&dev->dev_mutex);
377 if (pci_enable_msi(pci) < 0)
381 if (rtsx_acquire_irq(dev) < 0) {
382 /* unlock the device pointers */
383 mutex_unlock(&dev->dev_mutex);
387 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
388 rtsx_init_chip(chip);
390 /* unlock the device pointers */
391 mutex_unlock(&dev->dev_mutex);
395 #endif /* CONFIG_PM */
397 static void rtsx_shutdown(struct pci_dev *pci)
399 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
400 struct rtsx_chip *chip;
407 rtsx_do_before_power_down(chip, PM_S1);
410 synchronize_irq(dev->irq);
411 free_irq(dev->irq, (void *)dev);
416 pci_disable_msi(pci);
418 pci_disable_device(pci);
423 static int rtsx_control_thread(void *__dev)
425 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
426 struct rtsx_chip *chip = dev->chip;
427 struct Scsi_Host *host = rtsx_to_host(dev);
430 if (wait_for_completion_interruptible(&dev->cmnd_ready))
433 /* lock the device pointers */
434 mutex_lock(&(dev->dev_mutex));
436 /* if the device has disconnected, we are free to exit */
437 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
438 dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
439 mutex_unlock(&dev->dev_mutex);
443 /* lock access to the state */
446 /* has the command aborted ? */
447 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
448 chip->srb->result = DID_ABORT << 16;
454 /* reject the command if the direction indicator
457 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
458 dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
459 chip->srb->result = DID_ERROR << 16;
462 /* reject if target != 0 or if LUN is higher than
463 * the maximum known LUN
465 else if (chip->srb->device->id) {
466 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
467 chip->srb->device->id,
468 chip->srb->device->lun);
469 chip->srb->result = DID_BAD_TARGET << 16;
472 else if (chip->srb->device->lun > chip->max_lun) {
473 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
474 chip->srb->device->id,
475 chip->srb->device->lun);
476 chip->srb->result = DID_BAD_TARGET << 16;
479 /* we've got a command, let's do it! */
481 RTSX_DEBUG(scsi_show_command(chip->srb));
482 rtsx_invoke_transport(chip->srb, chip);
485 /* lock access to the state */
488 /* did the command already complete because of a disconnect? */
490 ; /* nothing to do */
492 /* indicate that the command is done */
493 else if (chip->srb->result != DID_ABORT << 16) {
494 chip->srb->scsi_done(chip->srb);
497 dev_err(&dev->pci->dev, "scsi command aborted\n");
500 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
501 complete(&(dev->notify));
503 rtsx_set_stat(chip, RTSX_STAT_IDLE);
506 /* finished working on this command */
510 /* unlock the device pointers */
511 mutex_unlock(&dev->dev_mutex);
514 /* notify the exit routine that we're actually exiting now
516 * complete()/wait_for_completion() is similar to up()/down(),
517 * except that complete() is safe in the case where the structure
518 * is getting deleted in a parallel mode of execution (i.e. just
519 * after the down() -- that's necessary for the thread-shutdown
522 * complete_and_exit() goes even further than this -- it is safe in
523 * the case that the thread of the caller is going away (not just
524 * the structure) -- this is necessary for the module-remove case.
525 * This is important in preemption kernels, which transfer the flow
526 * of execution immediately upon a complete().
528 complete_and_exit(&dev->control_exit, 0);
532 static int rtsx_polling_thread(void *__dev)
534 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
535 struct rtsx_chip *chip = dev->chip;
536 struct sd_info *sd_card = &(chip->sd_card);
537 struct xd_info *xd_card = &(chip->xd_card);
538 struct ms_info *ms_card = &(chip->ms_card);
540 sd_card->cleanup_counter = 0;
541 xd_card->cleanup_counter = 0;
542 ms_card->cleanup_counter = 0;
544 /* Wait until SCSI scan finished */
545 wait_timeout((delay_use + 5) * 1000);
549 set_current_state(TASK_INTERRUPTIBLE);
550 schedule_timeout(POLLING_INTERVAL);
552 /* lock the device pointers */
553 mutex_lock(&(dev->dev_mutex));
555 /* if the device has disconnected, we are free to exit */
556 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
557 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
558 mutex_unlock(&dev->dev_mutex);
562 mutex_unlock(&dev->dev_mutex);
564 mspro_polling_format_status(chip);
566 /* lock the device pointers */
567 mutex_lock(&(dev->dev_mutex));
569 rtsx_polling_func(chip);
571 /* unlock the device pointers */
572 mutex_unlock(&dev->dev_mutex);
575 complete_and_exit(&dev->polling_exit, 0);
581 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
583 struct rtsx_dev *dev = dev_id;
584 struct rtsx_chip *chip;
596 spin_lock(&dev->reg_lock);
598 retval = rtsx_pre_handle_interrupt(chip);
599 if (retval == STATUS_FAIL) {
600 spin_unlock(&dev->reg_lock);
601 if (chip->int_reg == 0xFFFFFFFF)
607 status = chip->int_reg;
609 if (dev->check_card_cd) {
610 if (!(dev->check_card_cd & status)) {
611 /* card not exist, return TRANS_RESULT_FAIL */
612 dev->trans_result = TRANS_RESULT_FAIL;
619 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
620 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
621 if (status & DELINK_INT)
622 RTSX_SET_DELINK(chip);
623 dev->trans_result = TRANS_RESULT_FAIL;
626 } else if (status & TRANS_OK_INT) {
627 dev->trans_result = TRANS_RESULT_OK;
630 } else if (status & DATA_DONE_INT) {
631 dev->trans_result = TRANS_NOT_READY;
632 if (dev->done && (dev->trans_state == STATE_TRANS_SG))
638 spin_unlock(&dev->reg_lock);
643 /* Release all our dynamic resources */
644 static void rtsx_release_resources(struct rtsx_dev *dev)
646 dev_info(&dev->pci->dev, "-- %s\n", __func__);
648 /* Tell the control thread to exit. The SCSI host must
649 * already have been removed so it won't try to queue
652 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
653 complete(&dev->cmnd_ready);
655 wait_for_completion(&dev->control_exit);
656 if (dev->polling_thread)
657 wait_for_completion(&dev->polling_exit);
661 if (dev->rtsx_resv_buf) {
662 dma_free_coherent(&(dev->pci->dev), RTSX_RESV_BUF_LEN,
663 dev->rtsx_resv_buf, dev->rtsx_resv_buf_addr);
664 dev->chip->host_cmds_ptr = NULL;
665 dev->chip->host_sg_tbl_ptr = NULL;
669 free_irq(dev->irq, (void *)dev);
670 if (dev->chip->msi_en)
671 pci_disable_msi(dev->pci);
673 iounmap(dev->remap_addr);
675 pci_disable_device(dev->pci);
676 pci_release_regions(dev->pci);
678 rtsx_release_chip(dev->chip);
682 /* First stage of disconnect processing: stop all commands and remove
684 static void quiesce_and_remove_host(struct rtsx_dev *dev)
686 struct Scsi_Host *host = rtsx_to_host(dev);
687 struct rtsx_chip *chip = dev->chip;
689 /* Prevent new transfers, stop the current command, and
690 * interrupt a SCSI-scan or device-reset delay */
691 mutex_lock(&dev->dev_mutex);
693 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
695 mutex_unlock(&dev->dev_mutex);
696 wake_up(&dev->delay_wait);
697 wait_for_completion(&dev->scanning_done);
699 /* Wait some time to let other threads exist */
702 /* queuecommand won't accept any new commands and the control
703 * thread won't execute a previously-queued command. If there
704 * is such a command pending, complete it with an error. */
705 mutex_lock(&dev->dev_mutex);
707 chip->srb->result = DID_NO_CONNECT << 16;
709 chip->srb->scsi_done(dev->chip->srb);
713 mutex_unlock(&dev->dev_mutex);
715 /* Now we own no commands so it's safe to remove the SCSI host */
716 scsi_remove_host(host);
719 /* Second stage of disconnect processing: deallocate all resources */
720 static void release_everything(struct rtsx_dev *dev)
722 rtsx_release_resources(dev);
724 /* Drop our reference to the host; the SCSI core will free it
725 * when the refcount becomes 0. */
726 scsi_host_put(rtsx_to_host(dev));
729 /* Thread to carry out delayed SCSI-device scanning */
730 static int rtsx_scan_thread(void *__dev)
732 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
733 struct rtsx_chip *chip = dev->chip;
735 /* Wait for the timeout to expire or for a disconnect */
737 dev_info(&dev->pci->dev,
738 "%s: waiting for device to settle before scanning\n",
740 wait_event_interruptible_timeout(dev->delay_wait,
741 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
745 /* If the device is still connected, perform the scanning */
746 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
747 scsi_scan_host(rtsx_to_host(dev));
748 dev_info(&dev->pci->dev, "%s: device scan complete\n",
751 /* Should we unbind if no devices were detected? */
754 complete_and_exit(&dev->scanning_done, 0);
757 static void rtsx_init_options(struct rtsx_chip *chip)
759 chip->vendor_id = chip->rtsx->pci->vendor;
760 chip->product_id = chip->rtsx->pci->device;
763 chip->driver_first_load = 1;
764 #ifdef HW_AUTO_SWITCH_SD_BUS
765 chip->sdio_in_charge = 0;
768 chip->mspro_formatter_enable = 1;
770 chip->use_hw_setting = 0;
771 chip->lun_mode = DEFAULT_SINGLE;
772 chip->auto_delink_en = auto_delink_en;
774 chip->ss_idle_period = ss_interval * 1000;
775 chip->remote_wakeup_en = 0;
776 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
777 chip->dynamic_aspm = 1;
778 chip->fpga_sd_sdr104_clk = CLK_200;
779 chip->fpga_sd_ddr50_clk = CLK_100;
780 chip->fpga_sd_sdr50_clk = CLK_100;
781 chip->fpga_sd_hs_clk = CLK_100;
782 chip->fpga_mmc_52m_clk = CLK_80;
783 chip->fpga_ms_hg_clk = CLK_80;
784 chip->fpga_ms_4bit_clk = CLK_80;
785 chip->fpga_ms_1bit_clk = CLK_40;
786 chip->asic_sd_sdr104_clk = 203;
787 chip->asic_sd_sdr50_clk = 98;
788 chip->asic_sd_ddr50_clk = 98;
789 chip->asic_sd_hs_clk = 98;
790 chip->asic_mmc_52m_clk = 98;
791 chip->asic_ms_hg_clk = 117;
792 chip->asic_ms_4bit_clk = 78;
793 chip->asic_ms_1bit_clk = 39;
794 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
795 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
796 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
797 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
798 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
799 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
800 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
801 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
803 chip->sd_speed_prior = 0x01040203;
804 chip->sd_current_prior = 0x00010203;
805 chip->sd_ctl = SD_PUSH_POINT_AUTO |
806 SD_SAMPLE_POINT_AUTO |
807 SUPPORT_MMC_DDR_MODE;
808 chip->sd_ddr_tx_phase = 0;
809 chip->mmc_ddr_tx_phase = 1;
810 chip->sd_default_tx_phase = 15;
811 chip->sd_default_rx_phase = 15;
812 chip->pmos_pwr_on_interval = 200;
813 chip->sd_voltage_switch_delay = 1000;
814 chip->ms_power_class_en = 3;
816 chip->sd_400mA_ocp_thd = 1;
817 chip->sd_800mA_ocp_thd = 5;
818 chip->ms_ocp_thd = 2;
820 chip->card_drive_sel = 0x55;
821 chip->sd30_drive_sel_1v8 = 0x03;
822 chip->sd30_drive_sel_3v3 = 0x01;
824 chip->do_delink_before_power_down = 1;
825 chip->auto_power_down = 1;
826 chip->polling_config = 0;
828 chip->force_clkreq_0 = 1;
829 chip->ft2_fast_mode = 0;
831 chip->sdio_retry_cnt = 1;
833 chip->xd_timeout = 2000;
834 chip->sd_timeout = 10000;
835 chip->ms_timeout = 2000;
836 chip->mspro_timeout = 15000;
838 chip->power_down_in_ss = 1;
844 chip->delink_stage1_step = 100;
845 chip->delink_stage2_step = 40;
846 chip->delink_stage3_step = 20;
848 chip->auto_delink_in_L1 = 1;
850 chip->msi_en = msi_en;
851 chip->hp_watch_bios_hotplug = 0;
852 chip->max_payload = 0;
853 chip->phy_voltage = 0;
855 chip->support_ms_8bit = 1;
856 chip->s3_pwr_off_delay = 1000;
859 static int rtsx_probe(struct pci_dev *pci,
860 const struct pci_device_id *pci_id)
862 struct Scsi_Host *host;
863 struct rtsx_dev *dev;
865 struct task_struct *th;
867 RTSX_DEBUGP("Realtek PCI-E card reader detected\n");
869 err = pci_enable_device(pci);
871 dev_err(&pci->dev, "PCI enable device failed!\n");
875 err = pci_request_regions(pci, CR_DRIVER_NAME);
877 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
879 pci_disable_device(pci);
884 * Ask the SCSI layer to allocate a host structure, with extra
885 * space at the end for our private rtsx_dev structure.
887 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
889 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
890 pci_release_regions(pci);
891 pci_disable_device(pci);
895 dev = host_to_rtsx(host);
896 memset(dev, 0, sizeof(struct rtsx_dev));
898 dev->chip = kzalloc(sizeof(struct rtsx_chip), GFP_KERNEL);
899 if (dev->chip == NULL)
902 spin_lock_init(&dev->reg_lock);
903 mutex_init(&(dev->dev_mutex));
904 init_completion(&dev->cmnd_ready);
905 init_completion(&dev->control_exit);
906 init_completion(&dev->polling_exit);
907 init_completion(&(dev->notify));
908 init_completion(&dev->scanning_done);
909 init_waitqueue_head(&dev->delay_wait);
914 dev_info(&pci->dev, "Resource length: 0x%x\n",
915 (unsigned int)pci_resource_len(pci, 0));
916 dev->addr = pci_resource_start(pci, 0);
917 dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
918 if (dev->remap_addr == NULL) {
919 dev_err(&pci->dev, "ioremap error\n");
925 * Using "unsigned long" cast here to eliminate gcc warning in
928 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
929 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
931 dev->rtsx_resv_buf = dma_alloc_coherent(&(pci->dev), RTSX_RESV_BUF_LEN,
932 &(dev->rtsx_resv_buf_addr), GFP_KERNEL);
933 if (dev->rtsx_resv_buf == NULL) {
934 dev_err(&pci->dev, "alloc dma buffer fail\n");
938 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
939 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
940 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
941 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
944 dev->chip->rtsx = dev;
946 rtsx_init_options(dev->chip);
948 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
950 if (dev->chip->msi_en) {
951 if (pci_enable_msi(pci) < 0)
952 dev->chip->msi_en = 0;
955 if (rtsx_acquire_irq(dev) < 0) {
961 synchronize_irq(dev->irq);
963 rtsx_init_chip(dev->chip);
965 /* set the supported max_lun and max_id for the scsi host
966 * NOTE: the minimal value of max_id is 1 */
968 host->max_lun = dev->chip->max_lun;
970 /* Start up our control thread */
971 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
973 dev_err(&pci->dev, "Unable to start control thread\n");
977 dev->ctl_thread = th;
979 err = scsi_add_host(host, &pci->dev);
981 dev_err(&pci->dev, "Unable to add the scsi host\n");
985 /* Start up the thread for delayed SCSI-device scanning */
986 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
988 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
989 complete(&dev->scanning_done);
990 quiesce_and_remove_host(dev);
995 /* Start up the thread for polling thread */
996 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
998 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
999 quiesce_and_remove_host(dev);
1003 dev->polling_thread = th;
1005 pci_set_drvdata(pci, dev);
1009 /* We come here if there are any problems */
1011 dev_err(&pci->dev, "rtsx_probe() failed\n");
1012 release_everything(dev);
1018 static void rtsx_remove(struct pci_dev *pci)
1020 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
1022 dev_info(&pci->dev, "rtsx_remove() called\n");
1024 quiesce_and_remove_host(dev);
1025 release_everything(dev);
1027 pci_set_drvdata(pci, NULL);
1031 static DEFINE_PCI_DEVICE_TABLE(rtsx_ids) = {
1032 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208), PCI_CLASS_OTHERS << 16, 0xFF0000 },
1033 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288), PCI_CLASS_OTHERS << 16, 0xFF0000 },
1037 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1039 /* pci_driver definition */
1040 static struct pci_driver driver = {
1041 .name = CR_DRIVER_NAME,
1042 .id_table = rtsx_ids,
1043 .probe = rtsx_probe,
1044 .remove = rtsx_remove,
1046 .suspend = rtsx_suspend,
1047 .resume = rtsx_resume,
1049 .shutdown = rtsx_shutdown,
1052 static int __init rtsx_init(void)
1054 pr_info("Initializing Realtek PCIE storage driver...\n");
1056 return pci_register_driver(&driver);
1059 static void __exit rtsx_exit(void)
1061 pr_info("rtsx_exit() called\n");
1063 pci_unregister_driver(&driver);
1065 pr_info("%s module exit\n", CR_DRIVER_NAME);
1068 module_init(rtsx_init)
1069 module_exit(rtsx_exit)