1 /* Driver for Realtek PCI-Express card reader
2 *
3 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
4 *
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
8 * later version.
9 *
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.
14 *
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/>.
17 *
18 * Author:
19 * Wei WANG (wei_wang@realsil.com.cn)
20 * Micky Ching (micky_ching@realsil.com.cn)
21 */
22
23 #include <linux/blkdev.h>
24 #include <linux/kthread.h>
25 #include <linux/sched.h>
26 #include <linux/workqueue.h>
27
28 #include "rtsx.h"
29 #include "ms.h"
30 #include "sd.h"
31 #include "xd.h"
32
33 MODULE_DESCRIPTION("Realtek PCI-Express card reader rts5208/rts5288 driver");
34 MODULE_LICENSE("GPL");
35
36 static unsigned int delay_use = 1;
37 module_param(delay_use, uint, 0644);
38 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
39
40 static int ss_en;
41 module_param(ss_en, int, 0644);
42 MODULE_PARM_DESC(ss_en, "enable selective suspend");
43
44 static int ss_interval = 50;
45 module_param(ss_interval, int, 0644);
46 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
47
48 static int auto_delink_en;
49 module_param(auto_delink_en, int, 0644);
50 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
51
52 static unsigned char aspm_l0s_l1_en;
53 module_param(aspm_l0s_l1_en, byte, 0644);
54 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
55
56 static int msi_en;
57 module_param(msi_en, int, 0644);
58 MODULE_PARM_DESC(msi_en, "enable msi");
59
60 static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
61
62 /***********************************************************************
63 * Host functions
64 ***********************************************************************/
65
host_info(struct Scsi_Host * host)66 static const char *host_info(struct Scsi_Host *host)
67 {
68 return "SCSI emulation for PCI-Express Mass Storage devices";
69 }
70
slave_alloc(struct scsi_device * sdev)71 static int slave_alloc(struct scsi_device *sdev)
72 {
73 /*
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
76 * less than 36 bytes.
77 */
78 sdev->inquiry_len = 36;
79 return 0;
80 }
81
slave_configure(struct scsi_device * sdev)82 static int slave_configure(struct scsi_device *sdev)
83 {
84 /*
85 * Scatter-gather buffers (all but the last) must have a length
86 * divisible by the bulk maxpacket size. Otherwise a data packet
87 * would end up being short, causing a premature end to the data
88 * transfer. Since high-speed bulk pipes have a maxpacket size
89 * of 512, we'll use that as the scsi device queue's DMA alignment
90 * mask. Guaranteeing proper alignment of the first buffer will
91 * have the desired effect because, except at the beginning and
92 * the end, scatter-gather buffers follow page boundaries.
93 */
94 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
95
96 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
97 * what is originally reported. We need this to avoid confusing
98 * the SCSI layer with devices that report 0 or 1, but need 10-byte
99 * commands (ala ATAPI devices behind certain bridges, or devices
100 * which simply have broken INQUIRY data).
101 *
102 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
103 * actual information. This seems to be the preference for
104 * programs like that.
105 *
106 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
107 * the actual value or the modified one, depending on where the
108 * data comes from.
109 */
110 if (sdev->scsi_level < SCSI_2) {
111 sdev->scsi_level = SCSI_2;
112 sdev->sdev_target->scsi_level = SCSI_2;
113 }
114
115 return 0;
116 }
117
118 /***********************************************************************
119 * /proc/scsi/ functions
120 ***********************************************************************/
121
122 /* we use this macro to help us write into the buffer */
123 #undef SPRINTF
124 #define SPRINTF(args...) \
125 do { \
126 if (pos < buffer + length) \
127 pos += sprintf(pos, ## args); \
128 } while (0)
129
130 /* queue a command */
131 /* This is always called with scsi_lock(host) held */
queuecommand_lck(struct scsi_cmnd * srb,void (* done)(struct scsi_cmnd *))132 static int queuecommand_lck(struct scsi_cmnd *srb,
133 void (*done)(struct scsi_cmnd *))
134 {
135 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
136 struct rtsx_chip *chip = dev->chip;
137
138 /* check for state-transition errors */
139 if (chip->srb) {
140 dev_err(&dev->pci->dev, "Error: chip->srb = %p\n",
141 chip->srb);
142 return SCSI_MLQUEUE_HOST_BUSY;
143 }
144
145 /* fail the command if we are disconnecting */
146 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
147 dev_info(&dev->pci->dev, "Fail command during disconnect\n");
148 srb->result = DID_NO_CONNECT << 16;
149 done(srb);
150 return 0;
151 }
152
153 /* enqueue the command and wake up the control thread */
154 srb->scsi_done = done;
155 chip->srb = srb;
156 complete(&dev->cmnd_ready);
157
158 return 0;
159 }
160
DEF_SCSI_QCMD(queuecommand)161 static DEF_SCSI_QCMD(queuecommand)
162
163 /***********************************************************************
164 * Error handling functions
165 ***********************************************************************/
166
167 /* Command timeout and abort */
168 static int command_abort(struct scsi_cmnd *srb)
169 {
170 struct Scsi_Host *host = srb->device->host;
171 struct rtsx_dev *dev = host_to_rtsx(host);
172 struct rtsx_chip *chip = dev->chip;
173
174 dev_info(&dev->pci->dev, "%s called\n", __func__);
175
176 scsi_lock(host);
177
178 /* Is this command still active? */
179 if (chip->srb != srb) {
180 scsi_unlock(host);
181 dev_info(&dev->pci->dev, "-- nothing to abort\n");
182 return FAILED;
183 }
184
185 rtsx_set_stat(chip, RTSX_STAT_ABORT);
186
187 scsi_unlock(host);
188
189 /* Wait for the aborted command to finish */
190 wait_for_completion(&dev->notify);
191
192 return SUCCESS;
193 }
194
195 /*
196 * This invokes the transport reset mechanism to reset the state of the
197 * device
198 */
device_reset(struct scsi_cmnd * srb)199 static int device_reset(struct scsi_cmnd *srb)
200 {
201 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
202
203 dev_info(&dev->pci->dev, "%s called\n", __func__);
204
205 return SUCCESS;
206 }
207
208 /*
209 * this defines our host template, with which we'll allocate hosts
210 */
211
212 static struct scsi_host_template rtsx_host_template = {
213 /* basic userland interface stuff */
214 .name = CR_DRIVER_NAME,
215 .proc_name = CR_DRIVER_NAME,
216 .info = host_info,
217
218 /* command interface -- queued only */
219 .queuecommand = queuecommand,
220
221 /* error and abort handlers */
222 .eh_abort_handler = command_abort,
223 .eh_device_reset_handler = device_reset,
224
225 /* queue commands only, only one command per LUN */
226 .can_queue = 1,
227
228 /* unknown initiator id */
229 .this_id = -1,
230
231 .slave_alloc = slave_alloc,
232 .slave_configure = slave_configure,
233
234 /* lots of sg segments can be handled */
235 .sg_tablesize = SG_ALL,
236
237 /* limit the total size of a transfer to 120 KB */
238 .max_sectors = 240,
239
240 /* merge commands... this seems to help performance, but
241 * periodically someone should test to see which setting is more
242 * optimal.
243 */
244 .use_clustering = 1,
245
246 /* emulated HBA */
247 .emulated = 1,
248
249 /* we do our own delay after a device or bus reset */
250 .skip_settle_delay = 1,
251
252 /* module management */
253 .module = THIS_MODULE
254 };
255
rtsx_acquire_irq(struct rtsx_dev * dev)256 static int rtsx_acquire_irq(struct rtsx_dev *dev)
257 {
258 struct rtsx_chip *chip = dev->chip;
259
260 dev_info(&dev->pci->dev, "%s: chip->msi_en = %d, pci->irq = %d\n",
261 __func__, chip->msi_en, dev->pci->irq);
262
263 if (request_irq(dev->pci->irq, rtsx_interrupt,
264 chip->msi_en ? 0 : IRQF_SHARED,
265 CR_DRIVER_NAME, dev)) {
266 dev_err(&dev->pci->dev,
267 "rtsx: unable to grab IRQ %d, disabling device\n",
268 dev->pci->irq);
269 return -1;
270 }
271
272 dev->irq = dev->pci->irq;
273 pci_intx(dev->pci, !chip->msi_en);
274
275 return 0;
276 }
277
rtsx_read_pci_cfg_byte(u8 bus,u8 dev,u8 func,u8 offset,u8 * val)278 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val)
279 {
280 struct pci_dev *pdev;
281 u8 data;
282 u8 devfn = (dev << 3) | func;
283
284 pdev = pci_get_bus_and_slot(bus, devfn);
285 if (!pdev)
286 return -1;
287
288 pci_read_config_byte(pdev, offset, &data);
289 if (val)
290 *val = data;
291
292 return 0;
293 }
294
295 #ifdef CONFIG_PM
296 /*
297 * power management
298 */
rtsx_suspend(struct pci_dev * pci,pm_message_t state)299 static int rtsx_suspend(struct pci_dev *pci, pm_message_t state)
300 {
301 struct rtsx_dev *dev = pci_get_drvdata(pci);
302 struct rtsx_chip *chip;
303
304 if (!dev)
305 return 0;
306
307 /* lock the device pointers */
308 mutex_lock(&dev->dev_mutex);
309
310 chip = dev->chip;
311
312 rtsx_do_before_power_down(chip, PM_S3);
313
314 if (dev->irq >= 0) {
315 free_irq(dev->irq, (void *)dev);
316 dev->irq = -1;
317 }
318
319 if (chip->msi_en)
320 pci_disable_msi(pci);
321
322 pci_save_state(pci);
323 pci_enable_wake(pci, pci_choose_state(pci, state), 1);
324 pci_disable_device(pci);
325 pci_set_power_state(pci, pci_choose_state(pci, state));
326
327 /* unlock the device pointers */
328 mutex_unlock(&dev->dev_mutex);
329
330 return 0;
331 }
332
rtsx_resume(struct pci_dev * pci)333 static int rtsx_resume(struct pci_dev *pci)
334 {
335 struct rtsx_dev *dev = pci_get_drvdata(pci);
336 struct rtsx_chip *chip;
337
338 if (!dev)
339 return 0;
340
341 chip = dev->chip;
342
343 /* lock the device pointers */
344 mutex_lock(&dev->dev_mutex);
345
346 pci_set_power_state(pci, PCI_D0);
347 pci_restore_state(pci);
348 if (pci_enable_device(pci) < 0) {
349 dev_err(&dev->pci->dev,
350 "%s: pci_enable_device failed, disabling device\n",
351 CR_DRIVER_NAME);
352 /* unlock the device pointers */
353 mutex_unlock(&dev->dev_mutex);
354 return -EIO;
355 }
356 pci_set_master(pci);
357
358 if (chip->msi_en) {
359 if (pci_enable_msi(pci) < 0)
360 chip->msi_en = 0;
361 }
362
363 if (rtsx_acquire_irq(dev) < 0) {
364 /* unlock the device pointers */
365 mutex_unlock(&dev->dev_mutex);
366 return -EIO;
367 }
368
369 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
370 rtsx_init_chip(chip);
371
372 /* unlock the device pointers */
373 mutex_unlock(&dev->dev_mutex);
374
375 return 0;
376 }
377 #endif /* CONFIG_PM */
378
rtsx_shutdown(struct pci_dev * pci)379 static void rtsx_shutdown(struct pci_dev *pci)
380 {
381 struct rtsx_dev *dev = pci_get_drvdata(pci);
382 struct rtsx_chip *chip;
383
384 if (!dev)
385 return;
386
387 chip = dev->chip;
388
389 rtsx_do_before_power_down(chip, PM_S1);
390
391 if (dev->irq >= 0) {
392 free_irq(dev->irq, (void *)dev);
393 dev->irq = -1;
394 }
395
396 if (chip->msi_en)
397 pci_disable_msi(pci);
398
399 pci_disable_device(pci);
400 }
401
rtsx_control_thread(void * __dev)402 static int rtsx_control_thread(void *__dev)
403 {
404 struct rtsx_dev *dev = __dev;
405 struct rtsx_chip *chip = dev->chip;
406 struct Scsi_Host *host = rtsx_to_host(dev);
407
408 for (;;) {
409 if (wait_for_completion_interruptible(&dev->cmnd_ready))
410 break;
411
412 /* lock the device pointers */
413 mutex_lock(&dev->dev_mutex);
414
415 /* if the device has disconnected, we are free to exit */
416 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
417 dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
418 mutex_unlock(&dev->dev_mutex);
419 break;
420 }
421
422 /* lock access to the state */
423 scsi_lock(host);
424
425 /* has the command aborted ? */
426 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
427 chip->srb->result = DID_ABORT << 16;
428 goto skip_for_abort;
429 }
430
431 scsi_unlock(host);
432
433 /* reject the command if the direction indicator
434 * is UNKNOWN
435 */
436 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
437 dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
438 chip->srb->result = DID_ERROR << 16;
439 }
440
441 /* reject if target != 0 or if LUN is higher than
442 * the maximum known LUN
443 */
444 else if (chip->srb->device->id) {
445 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
446 chip->srb->device->id,
447 (u8)chip->srb->device->lun);
448 chip->srb->result = DID_BAD_TARGET << 16;
449 }
450
451 else if (chip->srb->device->lun > chip->max_lun) {
452 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
453 chip->srb->device->id,
454 (u8)chip->srb->device->lun);
455 chip->srb->result = DID_BAD_TARGET << 16;
456 }
457
458 /* we've got a command, let's do it! */
459 else {
460 scsi_show_command(chip);
461 rtsx_invoke_transport(chip->srb, chip);
462 }
463
464 /* lock access to the state */
465 scsi_lock(host);
466
467 /* did the command already complete because of a disconnect? */
468 if (!chip->srb)
469 ; /* nothing to do */
470
471 /* indicate that the command is done */
472 else if (chip->srb->result != DID_ABORT << 16) {
473 chip->srb->scsi_done(chip->srb);
474 } else {
475 skip_for_abort:
476 dev_err(&dev->pci->dev, "scsi command aborted\n");
477 }
478
479 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
480 complete(&dev->notify);
481
482 rtsx_set_stat(chip, RTSX_STAT_IDLE);
483 }
484
485 /* finished working on this command */
486 chip->srb = NULL;
487 scsi_unlock(host);
488
489 /* unlock the device pointers */
490 mutex_unlock(&dev->dev_mutex);
491 } /* for (;;) */
492
493 /* notify the exit routine that we're actually exiting now
494 *
495 * complete()/wait_for_completion() is similar to up()/down(),
496 * except that complete() is safe in the case where the structure
497 * is getting deleted in a parallel mode of execution (i.e. just
498 * after the down() -- that's necessary for the thread-shutdown
499 * case.
500 *
501 * complete_and_exit() goes even further than this -- it is safe in
502 * the case that the thread of the caller is going away (not just
503 * the structure) -- this is necessary for the module-remove case.
504 * This is important in preemption kernels, which transfer the flow
505 * of execution immediately upon a complete().
506 */
507 complete_and_exit(&dev->control_exit, 0);
508 }
509
rtsx_polling_thread(void * __dev)510 static int rtsx_polling_thread(void *__dev)
511 {
512 struct rtsx_dev *dev = __dev;
513 struct rtsx_chip *chip = dev->chip;
514 struct sd_info *sd_card = &chip->sd_card;
515 struct xd_info *xd_card = &chip->xd_card;
516 struct ms_info *ms_card = &chip->ms_card;
517
518 sd_card->cleanup_counter = 0;
519 xd_card->cleanup_counter = 0;
520 ms_card->cleanup_counter = 0;
521
522 /* Wait until SCSI scan finished */
523 wait_timeout((delay_use + 5) * 1000);
524
525 for (;;) {
526 set_current_state(TASK_INTERRUPTIBLE);
527 schedule_timeout(msecs_to_jiffies(POLLING_INTERVAL));
528
529 /* lock the device pointers */
530 mutex_lock(&dev->dev_mutex);
531
532 /* if the device has disconnected, we are free to exit */
533 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
534 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
535 mutex_unlock(&dev->dev_mutex);
536 break;
537 }
538
539 mutex_unlock(&dev->dev_mutex);
540
541 mspro_polling_format_status(chip);
542
543 /* lock the device pointers */
544 mutex_lock(&dev->dev_mutex);
545
546 rtsx_polling_func(chip);
547
548 /* unlock the device pointers */
549 mutex_unlock(&dev->dev_mutex);
550 }
551
552 complete_and_exit(&dev->polling_exit, 0);
553 }
554
555 /*
556 * interrupt handler
557 */
rtsx_interrupt(int irq,void * dev_id)558 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
559 {
560 struct rtsx_dev *dev = dev_id;
561 struct rtsx_chip *chip;
562 int retval;
563 u32 status;
564
565 if (dev)
566 chip = dev->chip;
567 else
568 return IRQ_NONE;
569
570 if (!chip)
571 return IRQ_NONE;
572
573 spin_lock(&dev->reg_lock);
574
575 retval = rtsx_pre_handle_interrupt(chip);
576 if (retval == STATUS_FAIL) {
577 spin_unlock(&dev->reg_lock);
578 if (chip->int_reg == 0xFFFFFFFF)
579 return IRQ_HANDLED;
580 return IRQ_NONE;
581 }
582
583 status = chip->int_reg;
584
585 if (dev->check_card_cd) {
586 if (!(dev->check_card_cd & status)) {
587 /* card not exist, return TRANS_RESULT_FAIL */
588 dev->trans_result = TRANS_RESULT_FAIL;
589 if (dev->done)
590 complete(dev->done);
591 goto exit;
592 }
593 }
594
595 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
596 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
597 if (status & DELINK_INT)
598 RTSX_SET_DELINK(chip);
599 dev->trans_result = TRANS_RESULT_FAIL;
600 if (dev->done)
601 complete(dev->done);
602 } else if (status & TRANS_OK_INT) {
603 dev->trans_result = TRANS_RESULT_OK;
604 if (dev->done)
605 complete(dev->done);
606 } else if (status & DATA_DONE_INT) {
607 dev->trans_result = TRANS_NOT_READY;
608 if (dev->done && (dev->trans_state == STATE_TRANS_SG))
609 complete(dev->done);
610 }
611 }
612
613 exit:
614 spin_unlock(&dev->reg_lock);
615 return IRQ_HANDLED;
616 }
617
618 /* Release all our dynamic resources */
rtsx_release_resources(struct rtsx_dev * dev)619 static void rtsx_release_resources(struct rtsx_dev *dev)
620 {
621 dev_info(&dev->pci->dev, "-- %s\n", __func__);
622
623 /* Tell the control thread to exit. The SCSI host must
624 * already have been removed so it won't try to queue
625 * any more commands.
626 */
627 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
628 complete(&dev->cmnd_ready);
629 if (dev->ctl_thread)
630 wait_for_completion(&dev->control_exit);
631 if (dev->polling_thread)
632 wait_for_completion(&dev->polling_exit);
633
634 wait_timeout(200);
635
636 if (dev->rtsx_resv_buf) {
637 dev->chip->host_cmds_ptr = NULL;
638 dev->chip->host_sg_tbl_ptr = NULL;
639 }
640
641 if (dev->irq > 0)
642 free_irq(dev->irq, (void *)dev);
643 if (dev->chip->msi_en)
644 pci_disable_msi(dev->pci);
645 if (dev->remap_addr)
646 iounmap(dev->remap_addr);
647
648 rtsx_release_chip(dev->chip);
649 kfree(dev->chip);
650 }
651
652 /*
653 * First stage of disconnect processing: stop all commands and remove
654 * the host
655 */
quiesce_and_remove_host(struct rtsx_dev * dev)656 static void quiesce_and_remove_host(struct rtsx_dev *dev)
657 {
658 struct Scsi_Host *host = rtsx_to_host(dev);
659 struct rtsx_chip *chip = dev->chip;
660
661 /*
662 * Prevent new transfers, stop the current command, and
663 * interrupt a SCSI-scan or device-reset delay
664 */
665 mutex_lock(&dev->dev_mutex);
666 scsi_lock(host);
667 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
668 scsi_unlock(host);
669 mutex_unlock(&dev->dev_mutex);
670 wake_up(&dev->delay_wait);
671 wait_for_completion(&dev->scanning_done);
672
673 /* Wait some time to let other threads exist */
674 wait_timeout(100);
675
676 /*
677 * queuecommand won't accept any new commands and the control
678 * thread won't execute a previously-queued command. If there
679 * is such a command pending, complete it with an error.
680 */
681 mutex_lock(&dev->dev_mutex);
682 if (chip->srb) {
683 chip->srb->result = DID_NO_CONNECT << 16;
684 scsi_lock(host);
685 chip->srb->scsi_done(dev->chip->srb);
686 chip->srb = NULL;
687 scsi_unlock(host);
688 }
689 mutex_unlock(&dev->dev_mutex);
690
691 /* Now we own no commands so it's safe to remove the SCSI host */
692 scsi_remove_host(host);
693 }
694
695 /* Second stage of disconnect processing: deallocate all resources */
release_everything(struct rtsx_dev * dev)696 static void release_everything(struct rtsx_dev *dev)
697 {
698 rtsx_release_resources(dev);
699
700 /*
701 * Drop our reference to the host; the SCSI core will free it
702 * when the refcount becomes 0.
703 */
704 scsi_host_put(rtsx_to_host(dev));
705 }
706
707 /* Thread to carry out delayed SCSI-device scanning */
rtsx_scan_thread(void * __dev)708 static int rtsx_scan_thread(void *__dev)
709 {
710 struct rtsx_dev *dev = __dev;
711 struct rtsx_chip *chip = dev->chip;
712
713 /* Wait for the timeout to expire or for a disconnect */
714 if (delay_use > 0) {
715 dev_info(&dev->pci->dev,
716 "%s: waiting for device to settle before scanning\n",
717 CR_DRIVER_NAME);
718 wait_event_interruptible_timeout
719 (dev->delay_wait,
720 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
721 delay_use * HZ);
722 }
723
724 /* If the device is still connected, perform the scanning */
725 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
726 scsi_scan_host(rtsx_to_host(dev));
727 dev_info(&dev->pci->dev, "%s: device scan complete\n",
728 CR_DRIVER_NAME);
729
730 /* Should we unbind if no devices were detected? */
731 }
732
733 complete_and_exit(&dev->scanning_done, 0);
734 }
735
rtsx_init_options(struct rtsx_chip * chip)736 static void rtsx_init_options(struct rtsx_chip *chip)
737 {
738 chip->vendor_id = chip->rtsx->pci->vendor;
739 chip->product_id = chip->rtsx->pci->device;
740 chip->adma_mode = 1;
741 chip->lun_mc = 0;
742 chip->driver_first_load = 1;
743 #ifdef HW_AUTO_SWITCH_SD_BUS
744 chip->sdio_in_charge = 0;
745 #endif
746
747 chip->mspro_formatter_enable = 1;
748 chip->ignore_sd = 0;
749 chip->use_hw_setting = 0;
750 chip->lun_mode = DEFAULT_SINGLE;
751 chip->auto_delink_en = auto_delink_en;
752 chip->ss_en = ss_en;
753 chip->ss_idle_period = ss_interval * 1000;
754 chip->remote_wakeup_en = 0;
755 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
756 chip->dynamic_aspm = 1;
757 chip->fpga_sd_sdr104_clk = CLK_200;
758 chip->fpga_sd_ddr50_clk = CLK_100;
759 chip->fpga_sd_sdr50_clk = CLK_100;
760 chip->fpga_sd_hs_clk = CLK_100;
761 chip->fpga_mmc_52m_clk = CLK_80;
762 chip->fpga_ms_hg_clk = CLK_80;
763 chip->fpga_ms_4bit_clk = CLK_80;
764 chip->fpga_ms_1bit_clk = CLK_40;
765 chip->asic_sd_sdr104_clk = 203;
766 chip->asic_sd_sdr50_clk = 98;
767 chip->asic_sd_ddr50_clk = 98;
768 chip->asic_sd_hs_clk = 98;
769 chip->asic_mmc_52m_clk = 98;
770 chip->asic_ms_hg_clk = 117;
771 chip->asic_ms_4bit_clk = 78;
772 chip->asic_ms_1bit_clk = 39;
773 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
774 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
775 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
776 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
777 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
778 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
779 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
780 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
781 chip->ssc_en = 1;
782 chip->sd_speed_prior = 0x01040203;
783 chip->sd_current_prior = 0x00010203;
784 chip->sd_ctl = SD_PUSH_POINT_AUTO |
785 SD_SAMPLE_POINT_AUTO |
786 SUPPORT_MMC_DDR_MODE;
787 chip->sd_ddr_tx_phase = 0;
788 chip->mmc_ddr_tx_phase = 1;
789 chip->sd_default_tx_phase = 15;
790 chip->sd_default_rx_phase = 15;
791 chip->pmos_pwr_on_interval = 200;
792 chip->sd_voltage_switch_delay = 1000;
793 chip->ms_power_class_en = 3;
794
795 chip->sd_400mA_ocp_thd = 1;
796 chip->sd_800mA_ocp_thd = 5;
797 chip->ms_ocp_thd = 2;
798
799 chip->card_drive_sel = 0x55;
800 chip->sd30_drive_sel_1v8 = 0x03;
801 chip->sd30_drive_sel_3v3 = 0x01;
802
803 chip->do_delink_before_power_down = 1;
804 chip->auto_power_down = 1;
805 chip->polling_config = 0;
806
807 chip->force_clkreq_0 = 1;
808 chip->ft2_fast_mode = 0;
809
810 chip->sdio_retry_cnt = 1;
811
812 chip->xd_timeout = 2000;
813 chip->sd_timeout = 10000;
814 chip->ms_timeout = 2000;
815 chip->mspro_timeout = 15000;
816
817 chip->power_down_in_ss = 1;
818
819 chip->sdr104_en = 1;
820 chip->sdr50_en = 1;
821 chip->ddr50_en = 1;
822
823 chip->delink_stage1_step = 100;
824 chip->delink_stage2_step = 40;
825 chip->delink_stage3_step = 20;
826
827 chip->auto_delink_in_L1 = 1;
828 chip->blink_led = 1;
829 chip->msi_en = msi_en;
830 chip->hp_watch_bios_hotplug = 0;
831 chip->max_payload = 0;
832 chip->phy_voltage = 0;
833
834 chip->support_ms_8bit = 1;
835 chip->s3_pwr_off_delay = 1000;
836 }
837
rtsx_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)838 static int rtsx_probe(struct pci_dev *pci,
839 const struct pci_device_id *pci_id)
840 {
841 struct Scsi_Host *host;
842 struct rtsx_dev *dev;
843 int err = 0;
844 struct task_struct *th;
845
846 dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
847
848 err = pcim_enable_device(pci);
849 if (err < 0) {
850 dev_err(&pci->dev, "PCI enable device failed!\n");
851 return err;
852 }
853
854 err = pci_request_regions(pci, CR_DRIVER_NAME);
855 if (err < 0) {
856 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
857 CR_DRIVER_NAME);
858 return err;
859 }
860
861 /*
862 * Ask the SCSI layer to allocate a host structure, with extra
863 * space at the end for our private rtsx_dev structure.
864 */
865 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
866 if (!host) {
867 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
868 return -ENOMEM;
869 }
870
871 dev = host_to_rtsx(host);
872 memset(dev, 0, sizeof(struct rtsx_dev));
873
874 dev->chip = kzalloc(sizeof(*dev->chip), GFP_KERNEL);
875 if (!dev->chip) {
876 err = -ENOMEM;
877 goto errout;
878 }
879
880 spin_lock_init(&dev->reg_lock);
881 mutex_init(&dev->dev_mutex);
882 init_completion(&dev->cmnd_ready);
883 init_completion(&dev->control_exit);
884 init_completion(&dev->polling_exit);
885 init_completion(&dev->notify);
886 init_completion(&dev->scanning_done);
887 init_waitqueue_head(&dev->delay_wait);
888
889 dev->pci = pci;
890 dev->irq = -1;
891
892 dev_info(&pci->dev, "Resource length: 0x%x\n",
893 (unsigned int)pci_resource_len(pci, 0));
894 dev->addr = pci_resource_start(pci, 0);
895 dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
896 if (!dev->remap_addr) {
897 dev_err(&pci->dev, "ioremap error\n");
898 err = -ENXIO;
899 goto errout;
900 }
901
902 /*
903 * Using "unsigned long" cast here to eliminate gcc warning in
904 * 64-bit system
905 */
906 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
907 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
908
909 dev->rtsx_resv_buf = dmam_alloc_coherent(&pci->dev, RTSX_RESV_BUF_LEN,
910 &dev->rtsx_resv_buf_addr, GFP_KERNEL);
911 if (!dev->rtsx_resv_buf) {
912 dev_err(&pci->dev, "alloc dma buffer fail\n");
913 err = -ENXIO;
914 goto errout;
915 }
916 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
917 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
918 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
919 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
920 HOST_CMDS_BUF_LEN;
921
922 dev->chip->rtsx = dev;
923
924 rtsx_init_options(dev->chip);
925
926 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
927
928 if (dev->chip->msi_en) {
929 if (pci_enable_msi(pci) < 0)
930 dev->chip->msi_en = 0;
931 }
932
933 if (rtsx_acquire_irq(dev) < 0) {
934 err = -EBUSY;
935 goto errout;
936 }
937
938 pci_set_master(pci);
939 synchronize_irq(dev->irq);
940
941 rtsx_init_chip(dev->chip);
942
943 /*
944 * set the supported max_lun and max_id for the scsi host
945 * NOTE: the minimal value of max_id is 1
946 */
947 host->max_id = 1;
948 host->max_lun = dev->chip->max_lun;
949
950 /* Start up our control thread */
951 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
952 if (IS_ERR(th)) {
953 dev_err(&pci->dev, "Unable to start control thread\n");
954 err = PTR_ERR(th);
955 goto errout;
956 }
957 dev->ctl_thread = th;
958
959 err = scsi_add_host(host, &pci->dev);
960 if (err) {
961 dev_err(&pci->dev, "Unable to add the scsi host\n");
962 goto errout;
963 }
964
965 /* Start up the thread for delayed SCSI-device scanning */
966 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
967 if (IS_ERR(th)) {
968 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
969 complete(&dev->scanning_done);
970 quiesce_and_remove_host(dev);
971 err = PTR_ERR(th);
972 goto errout;
973 }
974
975 /* Start up the thread for polling thread */
976 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
977 if (IS_ERR(th)) {
978 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
979 quiesce_and_remove_host(dev);
980 err = PTR_ERR(th);
981 goto errout;
982 }
983 dev->polling_thread = th;
984
985 pci_set_drvdata(pci, dev);
986
987 return 0;
988
989 /* We come here if there are any problems */
990 errout:
991 dev_err(&pci->dev, "%s failed\n", __func__);
992 release_everything(dev);
993
994 return err;
995 }
996
rtsx_remove(struct pci_dev * pci)997 static void rtsx_remove(struct pci_dev *pci)
998 {
999 struct rtsx_dev *dev = pci_get_drvdata(pci);
1000
1001 dev_info(&pci->dev, "%s called\n", __func__);
1002
1003 quiesce_and_remove_host(dev);
1004 release_everything(dev);
1005 }
1006
1007 /* PCI IDs */
1008 static const struct pci_device_id rtsx_ids[] = {
1009 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208),
1010 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1011 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288),
1012 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1013 { 0, },
1014 };
1015
1016 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1017
1018 /* pci_driver definition */
1019 static struct pci_driver rtsx_driver = {
1020 .name = CR_DRIVER_NAME,
1021 .id_table = rtsx_ids,
1022 .probe = rtsx_probe,
1023 .remove = rtsx_remove,
1024 #ifdef CONFIG_PM
1025 .suspend = rtsx_suspend,
1026 .resume = rtsx_resume,
1027 #endif
1028 .shutdown = rtsx_shutdown,
1029 };
1030
1031 module_pci_driver(rtsx_driver);
1032