1
2
3
4
5
6
7
8
9
10
11
12#include <linux/blkdev.h>
13#include <linux/kthread.h>
14#include <linux/sched.h>
15#include <linux/workqueue.h>
16
17#include "rtsx.h"
18#include "ms.h"
19#include "sd.h"
20#include "xd.h"
21
22MODULE_DESCRIPTION("Realtek PCI-Express card reader rts5208/rts5288 driver");
23MODULE_LICENSE("GPL");
24
25static unsigned int delay_use = 1;
26module_param(delay_use, uint, 0644);
27MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
28
29static int ss_en;
30module_param(ss_en, int, 0644);
31MODULE_PARM_DESC(ss_en, "enable selective suspend");
32
33static int ss_interval = 50;
34module_param(ss_interval, int, 0644);
35MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
36
37static int auto_delink_en;
38module_param(auto_delink_en, int, 0644);
39MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
40
41static unsigned char aspm_l0s_l1_en;
42module_param(aspm_l0s_l1_en, byte, 0644);
43MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
44
45static int msi_en;
46module_param(msi_en, int, 0644);
47MODULE_PARM_DESC(msi_en, "enable msi");
48
49static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
50
51
52
53
54
55static const char *host_info(struct Scsi_Host *host)
56{
57 return "SCSI emulation for PCI-Express Mass Storage devices";
58}
59
60static int slave_alloc(struct scsi_device *sdev)
61{
62
63
64
65
66
67 sdev->inquiry_len = 36;
68 return 0;
69}
70
71static int slave_configure(struct scsi_device *sdev)
72{
73
74
75
76
77
78
79
80
81
82
83 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 if (sdev->scsi_level < SCSI_2) {
100 sdev->scsi_level = SCSI_2;
101 sdev->sdev_target->scsi_level = SCSI_2;
102 }
103
104 return 0;
105}
106
107
108
109
110
111
112#undef SPRINTF
113#define SPRINTF(args...) \
114 do { \
115 if (pos < buffer + length) \
116 pos += sprintf(pos, ## args); \
117 } while (0)
118
119
120
121static int queuecommand_lck(struct scsi_cmnd *srb,
122 void (*done)(struct scsi_cmnd *))
123{
124 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
125 struct rtsx_chip *chip = dev->chip;
126
127
128 if (chip->srb) {
129 dev_err(&dev->pci->dev, "Error: chip->srb = %p\n",
130 chip->srb);
131 return SCSI_MLQUEUE_HOST_BUSY;
132 }
133
134
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;
138 done(srb);
139 return 0;
140 }
141
142
143 srb->scsi_done = done;
144 chip->srb = srb;
145 complete(&dev->cmnd_ready);
146
147 return 0;
148}
149
150static DEF_SCSI_QCMD(queuecommand)
151
152
153
154
155
156
157static int command_abort(struct scsi_cmnd *srb)
158{
159 struct Scsi_Host *host = srb->device->host;
160 struct rtsx_dev *dev = host_to_rtsx(host);
161 struct rtsx_chip *chip = dev->chip;
162
163 dev_info(&dev->pci->dev, "%s called\n", __func__);
164
165 scsi_lock(host);
166
167
168 if (chip->srb != srb) {
169 scsi_unlock(host);
170 dev_info(&dev->pci->dev, "-- nothing to abort\n");
171 return FAILED;
172 }
173
174 rtsx_set_stat(chip, RTSX_STAT_ABORT);
175
176 scsi_unlock(host);
177
178
179 wait_for_completion(&dev->notify);
180
181 return SUCCESS;
182}
183
184
185
186
187
188static int device_reset(struct scsi_cmnd *srb)
189{
190 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
191
192 dev_info(&dev->pci->dev, "%s called\n", __func__);
193
194 return SUCCESS;
195}
196
197
198
199
200
201static struct scsi_host_template rtsx_host_template = {
202
203 .name = CR_DRIVER_NAME,
204 .proc_name = CR_DRIVER_NAME,
205 .info = host_info,
206
207
208 .queuecommand = queuecommand,
209
210
211 .eh_abort_handler = command_abort,
212 .eh_device_reset_handler = device_reset,
213
214
215 .can_queue = 1,
216
217
218 .this_id = -1,
219
220 .slave_alloc = slave_alloc,
221 .slave_configure = slave_configure,
222
223
224 .sg_tablesize = SG_ALL,
225
226
227 .max_sectors = 240,
228
229
230 .emulated = 1,
231
232
233 .skip_settle_delay = 1,
234
235
236 .module = THIS_MODULE
237};
238
239static int rtsx_acquire_irq(struct rtsx_dev *dev)
240{
241 struct rtsx_chip *chip = dev->chip;
242
243 dev_info(&dev->pci->dev, "%s: chip->msi_en = %d, pci->irq = %d\n",
244 __func__, chip->msi_en, dev->pci->irq);
245
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",
251 dev->pci->irq);
252 return -1;
253 }
254
255 dev->irq = dev->pci->irq;
256 pci_intx(dev->pci, !chip->msi_en);
257
258 return 0;
259}
260
261
262
263
264static int __maybe_unused rtsx_suspend(struct device *dev_d)
265{
266 struct pci_dev *pci = to_pci_dev(dev_d);
267 struct rtsx_dev *dev = pci_get_drvdata(pci);
268 struct rtsx_chip *chip;
269
270 if (!dev)
271 return 0;
272
273
274 mutex_lock(&dev->dev_mutex);
275
276 chip = dev->chip;
277
278 rtsx_do_before_power_down(chip, PM_S3);
279
280 if (dev->irq >= 0) {
281 free_irq(dev->irq, (void *)dev);
282 dev->irq = -1;
283 }
284
285 if (chip->msi_en)
286 pci_free_irq_vectors(pci);
287
288 device_wakeup_enable(dev_d);
289
290
291 mutex_unlock(&dev->dev_mutex);
292
293 return 0;
294}
295
296static int __maybe_unused rtsx_resume(struct device *dev_d)
297{
298 struct pci_dev *pci = to_pci_dev(dev_d);
299 struct rtsx_dev *dev = pci_get_drvdata(pci);
300 struct rtsx_chip *chip;
301
302 if (!dev)
303 return 0;
304
305 chip = dev->chip;
306
307
308 mutex_lock(&dev->dev_mutex);
309
310 pci_set_master(pci);
311
312 if (chip->msi_en) {
313 if (pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) < 0)
314 chip->msi_en = 0;
315 }
316
317 if (rtsx_acquire_irq(dev) < 0) {
318
319 mutex_unlock(&dev->dev_mutex);
320 return -EIO;
321 }
322
323 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
324 rtsx_init_chip(chip);
325
326
327 mutex_unlock(&dev->dev_mutex);
328
329 return 0;
330}
331
332static void rtsx_shutdown(struct pci_dev *pci)
333{
334 struct rtsx_dev *dev = pci_get_drvdata(pci);
335 struct rtsx_chip *chip;
336
337 if (!dev)
338 return;
339
340 chip = dev->chip;
341
342 rtsx_do_before_power_down(chip, PM_S1);
343
344 if (dev->irq >= 0) {
345 free_irq(dev->irq, (void *)dev);
346 dev->irq = -1;
347 }
348
349 if (chip->msi_en)
350 pci_free_irq_vectors(pci);
351
352 pci_disable_device(pci);
353}
354
355static int rtsx_control_thread(void *__dev)
356{
357 struct rtsx_dev *dev = __dev;
358 struct rtsx_chip *chip = dev->chip;
359 struct Scsi_Host *host = rtsx_to_host(dev);
360
361 for (;;) {
362 if (wait_for_completion_interruptible(&dev->cmnd_ready))
363 break;
364
365
366 mutex_lock(&dev->dev_mutex);
367
368
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);
372 break;
373 }
374
375
376 scsi_lock(host);
377
378
379 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
380 chip->srb->result = DID_ABORT << 16;
381 goto skip_for_abort;
382 }
383
384 scsi_unlock(host);
385
386
387
388
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;
392 }
393
394
395
396
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;
402 }
403
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;
409 }
410
411
412 else {
413 scsi_show_command(chip);
414 rtsx_invoke_transport(chip->srb, chip);
415 }
416
417
418 scsi_lock(host);
419
420
421 if (!chip->srb)
422 ;
423
424
425 else if (chip->srb->result != DID_ABORT << 16) {
426 chip->srb->scsi_done(chip->srb);
427 } else {
428skip_for_abort:
429 dev_err(&dev->pci->dev, "scsi command aborted\n");
430 }
431
432 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
433 complete(&dev->notify);
434
435 rtsx_set_stat(chip, RTSX_STAT_IDLE);
436 }
437
438
439 chip->srb = NULL;
440 scsi_unlock(host);
441
442
443 mutex_unlock(&dev->dev_mutex);
444 }
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460 complete_and_exit(&dev->control_exit, 0);
461}
462
463static int rtsx_polling_thread(void *__dev)
464{
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;
470
471 sd_card->cleanup_counter = 0;
472 xd_card->cleanup_counter = 0;
473 ms_card->cleanup_counter = 0;
474
475
476 wait_timeout((delay_use + 5) * 1000);
477
478 for (;;) {
479 set_current_state(TASK_INTERRUPTIBLE);
480 schedule_timeout(msecs_to_jiffies(POLLING_INTERVAL));
481
482
483 mutex_lock(&dev->dev_mutex);
484
485
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);
489 break;
490 }
491
492 mutex_unlock(&dev->dev_mutex);
493
494 mspro_polling_format_status(chip);
495
496
497 mutex_lock(&dev->dev_mutex);
498
499 rtsx_polling_func(chip);
500
501
502 mutex_unlock(&dev->dev_mutex);
503 }
504
505 complete_and_exit(&dev->polling_exit, 0);
506}
507
508
509
510
511static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
512{
513 struct rtsx_dev *dev = dev_id;
514 struct rtsx_chip *chip;
515 int retval;
516 u32 status;
517
518 if (dev)
519 chip = dev->chip;
520 else
521 return IRQ_NONE;
522
523 if (!chip)
524 return IRQ_NONE;
525
526 spin_lock(&dev->reg_lock);
527
528 retval = rtsx_pre_handle_interrupt(chip);
529 if (retval == STATUS_FAIL) {
530 spin_unlock(&dev->reg_lock);
531 if (chip->int_reg == 0xFFFFFFFF)
532 return IRQ_HANDLED;
533 return IRQ_NONE;
534 }
535
536 status = chip->int_reg;
537
538 if (dev->check_card_cd) {
539 if (!(dev->check_card_cd & status)) {
540
541 dev->trans_result = TRANS_RESULT_FAIL;
542 if (dev->done)
543 complete(dev->done);
544 goto exit;
545 }
546 }
547
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;
553 if (dev->done)
554 complete(dev->done);
555 } else if (status & TRANS_OK_INT) {
556 dev->trans_result = TRANS_RESULT_OK;
557 if (dev->done)
558 complete(dev->done);
559 } else if (status & DATA_DONE_INT) {
560 dev->trans_result = TRANS_NOT_READY;
561 if (dev->done && (dev->trans_state == STATE_TRANS_SG))
562 complete(dev->done);
563 }
564 }
565
566exit:
567 spin_unlock(&dev->reg_lock);
568 return IRQ_HANDLED;
569}
570
571
572static void rtsx_release_resources(struct rtsx_dev *dev)
573{
574 dev_info(&dev->pci->dev, "-- %s\n", __func__);
575
576
577
578
579
580 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
581 complete(&dev->cmnd_ready);
582 if (dev->ctl_thread)
583 wait_for_completion(&dev->control_exit);
584 if (dev->polling_thread)
585 wait_for_completion(&dev->polling_exit);
586
587 wait_timeout(200);
588
589 if (dev->rtsx_resv_buf) {
590 dev->chip->host_cmds_ptr = NULL;
591 dev->chip->host_sg_tbl_ptr = NULL;
592 }
593
594 if (dev->irq > 0)
595 free_irq(dev->irq, (void *)dev);
596 if (dev->chip->msi_en)
597 pci_free_irq_vectors(dev->pci);
598 if (dev->remap_addr)
599 iounmap(dev->remap_addr);
600
601 rtsx_release_chip(dev->chip);
602 kfree(dev->chip);
603}
604
605
606
607
608
609static void quiesce_and_remove_host(struct rtsx_dev *dev)
610{
611 struct Scsi_Host *host = rtsx_to_host(dev);
612 struct rtsx_chip *chip = dev->chip;
613
614
615
616
617
618 mutex_lock(&dev->dev_mutex);
619 scsi_lock(host);
620 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
621 scsi_unlock(host);
622 mutex_unlock(&dev->dev_mutex);
623 wake_up(&dev->delay_wait);
624 wait_for_completion(&dev->scanning_done);
625
626
627 wait_timeout(100);
628
629
630
631
632
633
634 mutex_lock(&dev->dev_mutex);
635 if (chip->srb) {
636 chip->srb->result = DID_NO_CONNECT << 16;
637 scsi_lock(host);
638 chip->srb->scsi_done(dev->chip->srb);
639 chip->srb = NULL;
640 scsi_unlock(host);
641 }
642 mutex_unlock(&dev->dev_mutex);
643
644
645 scsi_remove_host(host);
646}
647
648
649static void release_everything(struct rtsx_dev *dev)
650{
651 rtsx_release_resources(dev);
652
653
654
655
656
657 scsi_host_put(rtsx_to_host(dev));
658}
659
660
661static int rtsx_scan_thread(void *__dev)
662{
663 struct rtsx_dev *dev = __dev;
664 struct rtsx_chip *chip = dev->chip;
665
666
667 if (delay_use > 0) {
668 dev_info(&dev->pci->dev,
669 "%s: waiting for device to settle before scanning\n",
670 CR_DRIVER_NAME);
671 wait_event_interruptible_timeout
672 (dev->delay_wait,
673 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
674 delay_use * HZ);
675 }
676
677
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",
681 CR_DRIVER_NAME);
682
683
684 }
685
686 complete_and_exit(&dev->scanning_done, 0);
687}
688
689static void rtsx_init_options(struct rtsx_chip *chip)
690{
691 chip->vendor_id = chip->rtsx->pci->vendor;
692 chip->product_id = chip->rtsx->pci->device;
693 chip->adma_mode = 1;
694 chip->lun_mc = 0;
695 chip->driver_first_load = 1;
696#ifdef HW_AUTO_SWITCH_SD_BUS
697 chip->sdio_in_charge = 0;
698#endif
699
700 chip->mspro_formatter_enable = 1;
701 chip->ignore_sd = 0;
702 chip->use_hw_setting = 0;
703 chip->lun_mode = DEFAULT_SINGLE;
704 chip->auto_delink_en = auto_delink_en;
705 chip->ss_en = ss_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;
734 chip->ssc_en = 1;
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;
747
748 chip->sd_400mA_ocp_thd = 1;
749 chip->sd_800mA_ocp_thd = 5;
750 chip->ms_ocp_thd = 2;
751
752 chip->card_drive_sel = 0x55;
753 chip->sd30_drive_sel_1v8 = 0x03;
754 chip->sd30_drive_sel_3v3 = 0x01;
755
756 chip->do_delink_before_power_down = 1;
757 chip->auto_power_down = 1;
758 chip->polling_config = 0;
759
760 chip->force_clkreq_0 = 1;
761 chip->ft2_fast_mode = 0;
762
763 chip->sdio_retry_cnt = 1;
764
765 chip->xd_timeout = 2000;
766 chip->sd_timeout = 10000;
767 chip->ms_timeout = 2000;
768 chip->mspro_timeout = 15000;
769
770 chip->power_down_in_ss = 1;
771
772 chip->sdr104_en = 1;
773 chip->sdr50_en = 1;
774 chip->ddr50_en = 1;
775
776 chip->delink_stage1_step = 100;
777 chip->delink_stage2_step = 40;
778 chip->delink_stage3_step = 20;
779
780 chip->auto_delink_in_L1 = 1;
781 chip->blink_led = 1;
782 chip->msi_en = msi_en;
783 chip->hp_watch_bios_hotplug = 0;
784 chip->max_payload = 0;
785 chip->phy_voltage = 0;
786
787 chip->support_ms_8bit = 1;
788 chip->s3_pwr_off_delay = 1000;
789}
790
791static int rtsx_probe(struct pci_dev *pci,
792 const struct pci_device_id *pci_id)
793{
794 struct Scsi_Host *host;
795 struct rtsx_dev *dev;
796 int err = 0;
797 struct task_struct *th;
798
799 dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
800
801 err = pcim_enable_device(pci);
802 if (err < 0) {
803 dev_err(&pci->dev, "PCI enable device failed!\n");
804 return err;
805 }
806
807 err = pci_request_regions(pci, CR_DRIVER_NAME);
808 if (err < 0) {
809 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
810 CR_DRIVER_NAME);
811 return err;
812 }
813
814
815
816
817
818 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
819 if (!host) {
820 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
821 err = -ENOMEM;
822 goto scsi_host_alloc_fail;
823 }
824
825 dev = host_to_rtsx(host);
826 memset(dev, 0, sizeof(struct rtsx_dev));
827
828 dev->chip = kzalloc(sizeof(*dev->chip), GFP_KERNEL);
829 if (!dev->chip) {
830 err = -ENOMEM;
831 goto chip_alloc_fail;
832 }
833
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);
842
843 dev->pci = pci;
844 dev->irq = -1;
845
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");
852 err = -ENXIO;
853 goto ioremap_fail;
854 }
855
856
857
858
859
860 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
861 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
862
863 dev->rtsx_resv_buf = dmam_alloc_coherent(&pci->dev, RTSX_RESV_BUF_LEN,
864 &dev->rtsx_resv_buf_addr,
865 GFP_KERNEL);
866 if (!dev->rtsx_resv_buf) {
867 dev_err(&pci->dev, "alloc dma buffer fail\n");
868 err = -ENXIO;
869 goto dma_alloc_fail;
870 }
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 +
875 HOST_CMDS_BUF_LEN;
876
877 dev->chip->rtsx = dev;
878
879 rtsx_init_options(dev->chip);
880
881 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
882
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;
886 }
887
888 if (rtsx_acquire_irq(dev) < 0) {
889 err = -EBUSY;
890 goto irq_acquire_fail;
891 }
892
893 pci_set_master(pci);
894 synchronize_irq(dev->irq);
895
896 rtsx_init_chip(dev->chip);
897
898
899
900
901
902 host->max_id = 1;
903 host->max_lun = dev->chip->max_lun;
904
905
906 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
907 if (IS_ERR(th)) {
908 dev_err(&pci->dev, "Unable to start control thread\n");
909 err = PTR_ERR(th);
910 goto control_thread_fail;
911 }
912 dev->ctl_thread = th;
913
914 err = scsi_add_host(host, &pci->dev);
915 if (err) {
916 dev_err(&pci->dev, "Unable to add the scsi host\n");
917 goto scsi_add_host_fail;
918 }
919
920
921 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
922 if (IS_ERR(th)) {
923 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
924 complete(&dev->scanning_done);
925 err = PTR_ERR(th);
926 goto scan_thread_fail;
927 }
928
929
930 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
931 if (IS_ERR(th)) {
932 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
933 err = PTR_ERR(th);
934 goto scan_thread_fail;
935 }
936 dev->polling_thread = th;
937
938 pci_set_drvdata(pci, dev);
939
940 return 0;
941
942
943scan_thread_fail:
944 quiesce_and_remove_host(dev);
945scsi_add_host_fail:
946 complete(&dev->cmnd_ready);
947 wait_for_completion(&dev->control_exit);
948control_thread_fail:
949 free_irq(dev->irq, (void *)dev);
950 rtsx_release_chip(dev->chip);
951irq_acquire_fail:
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);
956dma_alloc_fail:
957 iounmap(dev->remap_addr);
958ioremap_fail:
959 kfree(dev->chip);
960chip_alloc_fail:
961 dev_err(&pci->dev, "%s failed\n", __func__);
962 scsi_host_put(host);
963scsi_host_alloc_fail:
964 pci_release_regions(pci);
965 return err;
966}
967
968static void rtsx_remove(struct pci_dev *pci)
969{
970 struct rtsx_dev *dev = pci_get_drvdata(pci);
971
972 dev_info(&pci->dev, "%s called\n", __func__);
973
974 quiesce_and_remove_host(dev);
975 release_everything(dev);
976 pci_release_regions(pci);
977}
978
979
980static 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 },
985 { 0, },
986};
987
988MODULE_DEVICE_TABLE(pci, rtsx_ids);
989
990static SIMPLE_DEV_PM_OPS(rtsx_pm_ops, rtsx_suspend, rtsx_resume);
991
992
993static struct pci_driver rtsx_driver = {
994 .name = CR_DRIVER_NAME,
995 .id_table = rtsx_ids,
996 .probe = rtsx_probe,
997 .remove = rtsx_remove,
998 .driver.pm = &rtsx_pm_ops,
999 .shutdown = rtsx_shutdown,
1000};
1001
1002module_pci_driver(rtsx_driver);
1003