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.
8 * Wei WANG (wei_wang@realsil.com.cn)
9 * Micky Ching (micky_ching@realsil.com.cn)
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
,
122 void (*done
)(struct scsi_cmnd
*))
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 */
143 srb
->scsi_done
= done
;
145 complete(&dev
->cmnd_ready
);
150 static DEF_SCSI_QCMD(queuecommand
)
152 /***********************************************************************
153 * Error handling functions
154 ***********************************************************************/
156 /* Command timeout and abort */
157 static int command_abort(struct scsi_cmnd
*srb
)
159 struct Scsi_Host
*host
= srb
->device
->host
;
160 struct rtsx_dev
*dev
= host_to_rtsx(host
);
161 struct rtsx_chip
*chip
= dev
->chip
;
163 dev_info(&dev
->pci
->dev
, "%s called\n", __func__
);
167 /* Is this command still active? */
168 if (chip
->srb
!= srb
) {
170 dev_info(&dev
->pci
->dev
, "-- nothing to abort\n");
174 rtsx_set_stat(chip
, RTSX_STAT_ABORT
);
178 /* Wait for the aborted command to finish */
179 wait_for_completion(&dev
->notify
);
185 * This invokes the transport reset mechanism to reset the state of the
188 static int device_reset(struct scsi_cmnd
*srb
)
190 struct rtsx_dev
*dev
= host_to_rtsx(srb
->device
->host
);
192 dev_info(&dev
->pci
->dev
, "%s called\n", __func__
);
198 * this defines our host template, with which we'll allocate hosts
201 static struct scsi_host_template rtsx_host_template
= {
202 /* basic userland interface stuff */
203 .name
= CR_DRIVER_NAME
,
204 .proc_name
= CR_DRIVER_NAME
,
207 /* command interface -- queued only */
208 .queuecommand
= queuecommand
,
210 /* error and abort handlers */
211 .eh_abort_handler
= command_abort
,
212 .eh_device_reset_handler
= device_reset
,
214 /* queue commands only, only one command per LUN */
217 /* unknown initiator id */
220 .slave_alloc
= slave_alloc
,
221 .slave_configure
= slave_configure
,
223 /* lots of sg segments can be handled */
224 .sg_tablesize
= SG_ALL
,
226 /* limit the total size of a transfer to 120 KB */
232 /* we do our own delay after a device or bus reset */
233 .skip_settle_delay
= 1,
235 /* module management */
236 .module
= THIS_MODULE
239 static int rtsx_acquire_irq(struct rtsx_dev
*dev
)
241 struct rtsx_chip
*chip
= dev
->chip
;
243 dev_info(&dev
->pci
->dev
, "%s: chip->msi_en = %d, pci->irq = %d\n",
244 __func__
, chip
->msi_en
, dev
->pci
->irq
);
246 if (request_irq(dev
->pci
->irq
, rtsx_interrupt
,
247 chip
->msi_en
? 0 : IRQF_SHARED
,
248 CR_DRIVER_NAME
, dev
)) {
249 dev_err(&dev
->pci
->dev
,
250 "rtsx: unable to grab IRQ %d, disabling device\n",
255 dev
->irq
= dev
->pci
->irq
;
256 pci_intx(dev
->pci
, !chip
->msi_en
);
264 static int __maybe_unused
rtsx_suspend(struct device
*dev_d
)
266 struct pci_dev
*pci
= to_pci_dev(dev_d
);
267 struct rtsx_dev
*dev
= pci_get_drvdata(pci
);
268 struct rtsx_chip
*chip
;
273 /* lock the device pointers */
274 mutex_lock(&dev
->dev_mutex
);
278 rtsx_do_before_power_down(chip
, PM_S3
);
281 free_irq(dev
->irq
, (void *)dev
);
286 pci_free_irq_vectors(pci
);
288 device_wakeup_enable(dev_d
);
290 /* unlock the device pointers */
291 mutex_unlock(&dev
->dev_mutex
);
296 static int __maybe_unused
rtsx_resume(struct device
*dev_d
)
298 struct pci_dev
*pci
= to_pci_dev(dev_d
);
299 struct rtsx_dev
*dev
= pci_get_drvdata(pci
);
300 struct rtsx_chip
*chip
;
307 /* lock the device pointers */
308 mutex_lock(&dev
->dev_mutex
);
313 if (pci_alloc_irq_vectors(pci
, 1, 1, PCI_IRQ_MSI
) < 0)
317 if (rtsx_acquire_irq(dev
) < 0) {
318 /* unlock the device pointers */
319 mutex_unlock(&dev
->dev_mutex
);
323 rtsx_write_register(chip
, HOST_SLEEP_STATE
, 0x03, 0x00);
324 rtsx_init_chip(chip
);
326 /* unlock the device pointers */
327 mutex_unlock(&dev
->dev_mutex
);
332 static void rtsx_shutdown(struct pci_dev
*pci
)
334 struct rtsx_dev
*dev
= pci_get_drvdata(pci
);
335 struct rtsx_chip
*chip
;
342 rtsx_do_before_power_down(chip
, PM_S1
);
345 free_irq(dev
->irq
, (void *)dev
);
350 pci_free_irq_vectors(pci
);
352 pci_disable_device(pci
);
355 static int rtsx_control_thread(void *__dev
)
357 struct rtsx_dev
*dev
= __dev
;
358 struct rtsx_chip
*chip
= dev
->chip
;
359 struct Scsi_Host
*host
= rtsx_to_host(dev
);
362 if (wait_for_completion_interruptible(&dev
->cmnd_ready
))
365 /* lock the device pointers */
366 mutex_lock(&dev
->dev_mutex
);
368 /* if the device has disconnected, we are free to exit */
369 if (rtsx_chk_stat(chip
, RTSX_STAT_DISCONNECT
)) {
370 dev_info(&dev
->pci
->dev
, "-- rtsx-control exiting\n");
371 mutex_unlock(&dev
->dev_mutex
);
375 /* lock access to the state */
378 /* has the command aborted ? */
379 if (rtsx_chk_stat(chip
, RTSX_STAT_ABORT
)) {
380 chip
->srb
->result
= DID_ABORT
<< 16;
386 /* reject the command if the direction indicator
389 if (chip
->srb
->sc_data_direction
== DMA_BIDIRECTIONAL
) {
390 dev_err(&dev
->pci
->dev
, "UNKNOWN data direction\n");
391 chip
->srb
->result
= DID_ERROR
<< 16;
394 /* reject if target != 0 or if LUN is higher than
395 * the maximum known LUN
397 else if (chip
->srb
->device
->id
) {
398 dev_err(&dev
->pci
->dev
, "Bad target number (%d:%d)\n",
399 chip
->srb
->device
->id
,
400 (u8
)chip
->srb
->device
->lun
);
401 chip
->srb
->result
= DID_BAD_TARGET
<< 16;
404 else if (chip
->srb
->device
->lun
> chip
->max_lun
) {
405 dev_err(&dev
->pci
->dev
, "Bad LUN (%d:%d)\n",
406 chip
->srb
->device
->id
,
407 (u8
)chip
->srb
->device
->lun
);
408 chip
->srb
->result
= DID_BAD_TARGET
<< 16;
411 /* we've got a command, let's do it! */
413 scsi_show_command(chip
);
414 rtsx_invoke_transport(chip
->srb
, chip
);
417 /* lock access to the state */
420 /* did the command already complete because of a disconnect? */
422 ; /* nothing to do */
424 /* indicate that the command is done */
425 else if (chip
->srb
->result
!= DID_ABORT
<< 16) {
426 chip
->srb
->scsi_done(chip
->srb
);
429 dev_err(&dev
->pci
->dev
, "scsi command aborted\n");
432 if (rtsx_chk_stat(chip
, RTSX_STAT_ABORT
)) {
433 complete(&dev
->notify
);
435 rtsx_set_stat(chip
, RTSX_STAT_IDLE
);
438 /* finished working on this command */
442 /* unlock the device pointers */
443 mutex_unlock(&dev
->dev_mutex
);
446 /* notify the exit routine that we're actually exiting now
448 * complete()/wait_for_completion() is similar to up()/down(),
449 * except that complete() is safe in the case where the structure
450 * is getting deleted in a parallel mode of execution (i.e. just
451 * after the down() -- that's necessary for the thread-shutdown
454 * complete_and_exit() goes even further than this -- it is safe in
455 * the case that the thread of the caller is going away (not just
456 * the structure) -- this is necessary for the module-remove case.
457 * This is important in preemption kernels, which transfer the flow
458 * of execution immediately upon a complete().
460 complete_and_exit(&dev
->control_exit
, 0);
463 static int rtsx_polling_thread(void *__dev
)
465 struct rtsx_dev
*dev
= __dev
;
466 struct rtsx_chip
*chip
= dev
->chip
;
467 struct sd_info
*sd_card
= &chip
->sd_card
;
468 struct xd_info
*xd_card
= &chip
->xd_card
;
469 struct ms_info
*ms_card
= &chip
->ms_card
;
471 sd_card
->cleanup_counter
= 0;
472 xd_card
->cleanup_counter
= 0;
473 ms_card
->cleanup_counter
= 0;
475 /* Wait until SCSI scan finished */
476 wait_timeout((delay_use
+ 5) * 1000);
479 set_current_state(TASK_INTERRUPTIBLE
);
480 schedule_timeout(msecs_to_jiffies(POLLING_INTERVAL
));
482 /* lock the device pointers */
483 mutex_lock(&dev
->dev_mutex
);
485 /* if the device has disconnected, we are free to exit */
486 if (rtsx_chk_stat(chip
, RTSX_STAT_DISCONNECT
)) {
487 dev_info(&dev
->pci
->dev
, "-- rtsx-polling exiting\n");
488 mutex_unlock(&dev
->dev_mutex
);
492 mutex_unlock(&dev
->dev_mutex
);
494 mspro_polling_format_status(chip
);
496 /* lock the device pointers */
497 mutex_lock(&dev
->dev_mutex
);
499 rtsx_polling_func(chip
);
501 /* unlock the device pointers */
502 mutex_unlock(&dev
->dev_mutex
);
505 complete_and_exit(&dev
->polling_exit
, 0);
511 static irqreturn_t
rtsx_interrupt(int irq
, void *dev_id
)
513 struct rtsx_dev
*dev
= dev_id
;
514 struct rtsx_chip
*chip
;
526 spin_lock(&dev
->reg_lock
);
528 retval
= rtsx_pre_handle_interrupt(chip
);
529 if (retval
== STATUS_FAIL
) {
530 spin_unlock(&dev
->reg_lock
);
531 if (chip
->int_reg
== 0xFFFFFFFF)
536 status
= chip
->int_reg
;
538 if (dev
->check_card_cd
) {
539 if (!(dev
->check_card_cd
& status
)) {
540 /* card not exist, return TRANS_RESULT_FAIL */
541 dev
->trans_result
= TRANS_RESULT_FAIL
;
548 if (status
& (NEED_COMPLETE_INT
| DELINK_INT
)) {
549 if (status
& (TRANS_FAIL_INT
| DELINK_INT
)) {
550 if (status
& DELINK_INT
)
551 RTSX_SET_DELINK(chip
);
552 dev
->trans_result
= TRANS_RESULT_FAIL
;
555 } else if (status
& TRANS_OK_INT
) {
556 dev
->trans_result
= TRANS_RESULT_OK
;
559 } else if (status
& DATA_DONE_INT
) {
560 dev
->trans_result
= TRANS_NOT_READY
;
561 if (dev
->done
&& (dev
->trans_state
== STATE_TRANS_SG
))
567 spin_unlock(&dev
->reg_lock
);
571 /* Release all our dynamic resources */
572 static void rtsx_release_resources(struct rtsx_dev
*dev
)
574 dev_info(&dev
->pci
->dev
, "-- %s\n", __func__
);
576 /* Tell the control thread to exit. The SCSI host must
577 * already have been removed so it won't try to queue
580 dev_info(&dev
->pci
->dev
, "-- sending exit command to thread\n");
581 complete(&dev
->cmnd_ready
);
583 wait_for_completion(&dev
->control_exit
);
584 if (dev
->polling_thread
)
585 wait_for_completion(&dev
->polling_exit
);
589 if (dev
->rtsx_resv_buf
) {
590 dev
->chip
->host_cmds_ptr
= NULL
;
591 dev
->chip
->host_sg_tbl_ptr
= NULL
;
595 free_irq(dev
->irq
, (void *)dev
);
596 if (dev
->chip
->msi_en
)
597 pci_free_irq_vectors(dev
->pci
);
599 iounmap(dev
->remap_addr
);
601 rtsx_release_chip(dev
->chip
);
606 * First stage of disconnect processing: stop all commands and remove
609 static void quiesce_and_remove_host(struct rtsx_dev
*dev
)
611 struct Scsi_Host
*host
= rtsx_to_host(dev
);
612 struct rtsx_chip
*chip
= dev
->chip
;
615 * Prevent new transfers, stop the current command, and
616 * interrupt a SCSI-scan or device-reset delay
618 mutex_lock(&dev
->dev_mutex
);
620 rtsx_set_stat(chip
, RTSX_STAT_DISCONNECT
);
622 mutex_unlock(&dev
->dev_mutex
);
623 wake_up(&dev
->delay_wait
);
624 wait_for_completion(&dev
->scanning_done
);
626 /* Wait some time to let other threads exist */
630 * queuecommand won't accept any new commands and the control
631 * thread won't execute a previously-queued command. If there
632 * is such a command pending, complete it with an error.
634 mutex_lock(&dev
->dev_mutex
);
636 chip
->srb
->result
= DID_NO_CONNECT
<< 16;
638 chip
->srb
->scsi_done(dev
->chip
->srb
);
642 mutex_unlock(&dev
->dev_mutex
);
644 /* Now we own no commands so it's safe to remove the SCSI host */
645 scsi_remove_host(host
);
648 /* Second stage of disconnect processing: deallocate all resources */
649 static void release_everything(struct rtsx_dev
*dev
)
651 rtsx_release_resources(dev
);
654 * Drop our reference to the host; the SCSI core will free it
655 * when the refcount becomes 0.
657 scsi_host_put(rtsx_to_host(dev
));
660 /* Thread to carry out delayed SCSI-device scanning */
661 static int rtsx_scan_thread(void *__dev
)
663 struct rtsx_dev
*dev
= __dev
;
664 struct rtsx_chip
*chip
= dev
->chip
;
666 /* Wait for the timeout to expire or for a disconnect */
668 dev_info(&dev
->pci
->dev
,
669 "%s: waiting for device to settle before scanning\n",
671 wait_event_interruptible_timeout
673 rtsx_chk_stat(chip
, RTSX_STAT_DISCONNECT
),
677 /* If the device is still connected, perform the scanning */
678 if (!rtsx_chk_stat(chip
, RTSX_STAT_DISCONNECT
)) {
679 scsi_scan_host(rtsx_to_host(dev
));
680 dev_info(&dev
->pci
->dev
, "%s: device scan complete\n",
683 /* Should we unbind if no devices were detected? */
686 complete_and_exit(&dev
->scanning_done
, 0);
689 static void rtsx_init_options(struct rtsx_chip
*chip
)
691 chip
->vendor_id
= chip
->rtsx
->pci
->vendor
;
692 chip
->product_id
= chip
->rtsx
->pci
->device
;
695 chip
->driver_first_load
= 1;
696 #ifdef HW_AUTO_SWITCH_SD_BUS
697 chip
->sdio_in_charge
= 0;
700 chip
->mspro_formatter_enable
= 1;
702 chip
->use_hw_setting
= 0;
703 chip
->lun_mode
= DEFAULT_SINGLE
;
704 chip
->auto_delink_en
= auto_delink_en
;
706 chip
->ss_idle_period
= ss_interval
* 1000;
707 chip
->remote_wakeup_en
= 0;
708 chip
->aspm_l0s_l1_en
= aspm_l0s_l1_en
;
709 chip
->dynamic_aspm
= 1;
710 chip
->fpga_sd_sdr104_clk
= CLK_200
;
711 chip
->fpga_sd_ddr50_clk
= CLK_100
;
712 chip
->fpga_sd_sdr50_clk
= CLK_100
;
713 chip
->fpga_sd_hs_clk
= CLK_100
;
714 chip
->fpga_mmc_52m_clk
= CLK_80
;
715 chip
->fpga_ms_hg_clk
= CLK_80
;
716 chip
->fpga_ms_4bit_clk
= CLK_80
;
717 chip
->fpga_ms_1bit_clk
= CLK_40
;
718 chip
->asic_sd_sdr104_clk
= 203;
719 chip
->asic_sd_sdr50_clk
= 98;
720 chip
->asic_sd_ddr50_clk
= 98;
721 chip
->asic_sd_hs_clk
= 98;
722 chip
->asic_mmc_52m_clk
= 98;
723 chip
->asic_ms_hg_clk
= 117;
724 chip
->asic_ms_4bit_clk
= 78;
725 chip
->asic_ms_1bit_clk
= 39;
726 chip
->ssc_depth_sd_sdr104
= SSC_DEPTH_2M
;
727 chip
->ssc_depth_sd_sdr50
= SSC_DEPTH_2M
;
728 chip
->ssc_depth_sd_ddr50
= SSC_DEPTH_1M
;
729 chip
->ssc_depth_sd_hs
= SSC_DEPTH_1M
;
730 chip
->ssc_depth_mmc_52m
= SSC_DEPTH_1M
;
731 chip
->ssc_depth_ms_hg
= SSC_DEPTH_1M
;
732 chip
->ssc_depth_ms_4bit
= SSC_DEPTH_512K
;
733 chip
->ssc_depth_low_speed
= SSC_DEPTH_512K
;
735 chip
->sd_speed_prior
= 0x01040203;
736 chip
->sd_current_prior
= 0x00010203;
737 chip
->sd_ctl
= SD_PUSH_POINT_AUTO
|
738 SD_SAMPLE_POINT_AUTO
|
739 SUPPORT_MMC_DDR_MODE
;
740 chip
->sd_ddr_tx_phase
= 0;
741 chip
->mmc_ddr_tx_phase
= 1;
742 chip
->sd_default_tx_phase
= 15;
743 chip
->sd_default_rx_phase
= 15;
744 chip
->pmos_pwr_on_interval
= 200;
745 chip
->sd_voltage_switch_delay
= 1000;
746 chip
->ms_power_class_en
= 3;
748 chip
->sd_400mA_ocp_thd
= 1;
749 chip
->sd_800mA_ocp_thd
= 5;
750 chip
->ms_ocp_thd
= 2;
752 chip
->card_drive_sel
= 0x55;
753 chip
->sd30_drive_sel_1v8
= 0x03;
754 chip
->sd30_drive_sel_3v3
= 0x01;
756 chip
->do_delink_before_power_down
= 1;
757 chip
->auto_power_down
= 1;
758 chip
->polling_config
= 0;
760 chip
->force_clkreq_0
= 1;
761 chip
->ft2_fast_mode
= 0;
763 chip
->sdio_retry_cnt
= 1;
765 chip
->xd_timeout
= 2000;
766 chip
->sd_timeout
= 10000;
767 chip
->ms_timeout
= 2000;
768 chip
->mspro_timeout
= 15000;
770 chip
->power_down_in_ss
= 1;
776 chip
->delink_stage1_step
= 100;
777 chip
->delink_stage2_step
= 40;
778 chip
->delink_stage3_step
= 20;
780 chip
->auto_delink_in_L1
= 1;
782 chip
->msi_en
= msi_en
;
783 chip
->hp_watch_bios_hotplug
= 0;
784 chip
->max_payload
= 0;
785 chip
->phy_voltage
= 0;
787 chip
->support_ms_8bit
= 1;
788 chip
->s3_pwr_off_delay
= 1000;
791 static int rtsx_probe(struct pci_dev
*pci
,
792 const struct pci_device_id
*pci_id
)
794 struct Scsi_Host
*host
;
795 struct rtsx_dev
*dev
;
797 struct task_struct
*th
;
799 dev_dbg(&pci
->dev
, "Realtek PCI-E card reader detected\n");
801 err
= pcim_enable_device(pci
);
803 dev_err(&pci
->dev
, "PCI enable device failed!\n");
807 err
= pci_request_regions(pci
, CR_DRIVER_NAME
);
809 dev_err(&pci
->dev
, "PCI request regions for %s failed!\n",
815 * Ask the SCSI layer to allocate a host structure, with extra
816 * space at the end for our private rtsx_dev structure.
818 host
= scsi_host_alloc(&rtsx_host_template
, sizeof(*dev
));
820 dev_err(&pci
->dev
, "Unable to allocate the scsi host\n");
822 goto scsi_host_alloc_fail
;
825 dev
= host_to_rtsx(host
);
826 memset(dev
, 0, sizeof(struct rtsx_dev
));
828 dev
->chip
= kzalloc(sizeof(*dev
->chip
), GFP_KERNEL
);
831 goto chip_alloc_fail
;
834 spin_lock_init(&dev
->reg_lock
);
835 mutex_init(&dev
->dev_mutex
);
836 init_completion(&dev
->cmnd_ready
);
837 init_completion(&dev
->control_exit
);
838 init_completion(&dev
->polling_exit
);
839 init_completion(&dev
->notify
);
840 init_completion(&dev
->scanning_done
);
841 init_waitqueue_head(&dev
->delay_wait
);
846 dev_info(&pci
->dev
, "Resource length: 0x%x\n",
847 (unsigned int)pci_resource_len(pci
, 0));
848 dev
->addr
= pci_resource_start(pci
, 0);
849 dev
->remap_addr
= ioremap(dev
->addr
, pci_resource_len(pci
, 0));
850 if (!dev
->remap_addr
) {
851 dev_err(&pci
->dev
, "ioremap error\n");
857 * Using "unsigned long" cast here to eliminate gcc warning in
860 dev_info(&pci
->dev
, "Original address: 0x%lx, remapped address: 0x%lx\n",
861 (unsigned long)(dev
->addr
), (unsigned long)(dev
->remap_addr
));
863 dev
->rtsx_resv_buf
= dmam_alloc_coherent(&pci
->dev
, RTSX_RESV_BUF_LEN
,
864 &dev
->rtsx_resv_buf_addr
,
866 if (!dev
->rtsx_resv_buf
) {
867 dev_err(&pci
->dev
, "alloc dma buffer fail\n");
871 dev
->chip
->host_cmds_ptr
= dev
->rtsx_resv_buf
;
872 dev
->chip
->host_cmds_addr
= dev
->rtsx_resv_buf_addr
;
873 dev
->chip
->host_sg_tbl_ptr
= dev
->rtsx_resv_buf
+ HOST_CMDS_BUF_LEN
;
874 dev
->chip
->host_sg_tbl_addr
= dev
->rtsx_resv_buf_addr
+
877 dev
->chip
->rtsx
= dev
;
879 rtsx_init_options(dev
->chip
);
881 dev_info(&pci
->dev
, "pci->irq = %d\n", pci
->irq
);
883 if (dev
->chip
->msi_en
) {
884 if (pci_alloc_irq_vectors(pci
, 1, 1, PCI_IRQ_MSI
) < 0)
885 dev
->chip
->msi_en
= 0;
888 if (rtsx_acquire_irq(dev
) < 0) {
890 goto irq_acquire_fail
;
894 synchronize_irq(dev
->irq
);
896 rtsx_init_chip(dev
->chip
);
899 * set the supported max_lun and max_id for the scsi host
900 * NOTE: the minimal value of max_id is 1
903 host
->max_lun
= dev
->chip
->max_lun
;
905 /* Start up our control thread */
906 th
= kthread_run(rtsx_control_thread
, dev
, CR_DRIVER_NAME
);
908 dev_err(&pci
->dev
, "Unable to start control thread\n");
910 goto control_thread_fail
;
912 dev
->ctl_thread
= th
;
914 err
= scsi_add_host(host
, &pci
->dev
);
916 dev_err(&pci
->dev
, "Unable to add the scsi host\n");
917 goto scsi_add_host_fail
;
920 /* Start up the thread for delayed SCSI-device scanning */
921 th
= kthread_run(rtsx_scan_thread
, dev
, "rtsx-scan");
923 dev_err(&pci
->dev
, "Unable to start the device-scanning thread\n");
924 complete(&dev
->scanning_done
);
926 goto scan_thread_fail
;
929 /* Start up the thread for polling thread */
930 th
= kthread_run(rtsx_polling_thread
, dev
, "rtsx-polling");
932 dev_err(&pci
->dev
, "Unable to start the device-polling thread\n");
934 goto scan_thread_fail
;
936 dev
->polling_thread
= th
;
938 pci_set_drvdata(pci
, dev
);
942 /* We come here if there are any problems */
944 quiesce_and_remove_host(dev
);
946 complete(&dev
->cmnd_ready
);
947 wait_for_completion(&dev
->control_exit
);
949 free_irq(dev
->irq
, (void *)dev
);
950 rtsx_release_chip(dev
->chip
);
952 dev
->chip
->host_cmds_ptr
= NULL
;
953 dev
->chip
->host_sg_tbl_ptr
= NULL
;
954 if (dev
->chip
->msi_en
)
955 pci_free_irq_vectors(dev
->pci
);
957 iounmap(dev
->remap_addr
);
961 dev_err(&pci
->dev
, "%s failed\n", __func__
);
963 scsi_host_alloc_fail
:
964 pci_release_regions(pci
);
968 static void rtsx_remove(struct pci_dev
*pci
)
970 struct rtsx_dev
*dev
= pci_get_drvdata(pci
);
972 dev_info(&pci
->dev
, "%s called\n", __func__
);
974 quiesce_and_remove_host(dev
);
975 release_everything(dev
);
976 pci_release_regions(pci
);
980 static const struct pci_device_id rtsx_ids
[] = {
981 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK
, 0x5208),
982 PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
983 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK
, 0x5288),
984 PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
988 MODULE_DEVICE_TABLE(pci
, rtsx_ids
);
990 static SIMPLE_DEV_PM_OPS(rtsx_pm_ops
, rtsx_suspend
, rtsx_resume
);
992 /* pci_driver definition */
993 static struct pci_driver rtsx_driver
= {
994 .name
= CR_DRIVER_NAME
,
995 .id_table
= rtsx_ids
,
997 .remove
= rtsx_remove
,
998 .driver
.pm
= &rtsx_pm_ops
,
999 .shutdown
= rtsx_shutdown
,
1002 module_pci_driver(rtsx_driver
);