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