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