iwlwifi: remove useless enum values
[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                 free_irq(dev->irq, (void *)dev);
324                 dev->irq = -1;
325         }
326
327         if (chip->msi_en)
328                 pci_disable_msi(pci);
329
330         pci_save_state(pci);
331         pci_enable_wake(pci, pci_choose_state(pci, state), 1);
332         pci_disable_device(pci);
333         pci_set_power_state(pci, pci_choose_state(pci, state));
334
335         /* unlock the device pointers */
336         mutex_unlock(&dev->dev_mutex);
337
338         return 0;
339 }
340
341 static int rtsx_resume(struct pci_dev *pci)
342 {
343         struct rtsx_dev *dev = pci_get_drvdata(pci);
344         struct rtsx_chip *chip;
345
346         if (!dev)
347                 return 0;
348
349         chip = dev->chip;
350
351         /* lock the device pointers */
352         mutex_lock(&(dev->dev_mutex));
353
354         pci_set_power_state(pci, PCI_D0);
355         pci_restore_state(pci);
356         if (pci_enable_device(pci) < 0) {
357                 dev_err(&dev->pci->dev,
358                         "%s: pci_enable_device failed, disabling device\n",
359                         CR_DRIVER_NAME);
360                 /* unlock the device pointers */
361                 mutex_unlock(&dev->dev_mutex);
362                 return -EIO;
363         }
364         pci_set_master(pci);
365
366         if (chip->msi_en) {
367                 if (pci_enable_msi(pci) < 0)
368                         chip->msi_en = 0;
369         }
370
371         if (rtsx_acquire_irq(dev) < 0) {
372                 /* unlock the device pointers */
373                 mutex_unlock(&dev->dev_mutex);
374                 return -EIO;
375         }
376
377         rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
378         rtsx_init_chip(chip);
379
380         /* unlock the device pointers */
381         mutex_unlock(&dev->dev_mutex);
382
383         return 0;
384 }
385 #endif /* CONFIG_PM */
386
387 static void rtsx_shutdown(struct pci_dev *pci)
388 {
389         struct rtsx_dev *dev = pci_get_drvdata(pci);
390         struct rtsx_chip *chip;
391
392         if (!dev)
393                 return;
394
395         chip = dev->chip;
396
397         rtsx_do_before_power_down(chip, PM_S1);
398
399         if (dev->irq >= 0) {
400                 free_irq(dev->irq, (void *)dev);
401                 dev->irq = -1;
402         }
403
404         if (chip->msi_en)
405                 pci_disable_msi(pci);
406
407         pci_disable_device(pci);
408 }
409
410 static int rtsx_control_thread(void *__dev)
411 {
412         struct rtsx_dev *dev = __dev;
413         struct rtsx_chip *chip = dev->chip;
414         struct Scsi_Host *host = rtsx_to_host(dev);
415
416         for (;;) {
417                 if (wait_for_completion_interruptible(&dev->cmnd_ready))
418                         break;
419
420                 /* lock the device pointers */
421                 mutex_lock(&(dev->dev_mutex));
422
423                 /* if the device has disconnected, we are free to exit */
424                 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
425                         dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
426                         mutex_unlock(&dev->dev_mutex);
427                         break;
428                 }
429
430                 /* lock access to the state */
431                 scsi_lock(host);
432
433                 /* has the command aborted ? */
434                 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
435                         chip->srb->result = DID_ABORT << 16;
436                         goto SkipForAbort;
437                 }
438
439                 scsi_unlock(host);
440
441                 /* reject the command if the direction indicator
442                  * is UNKNOWN
443                  */
444                 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
445                         dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
446                         chip->srb->result = DID_ERROR << 16;
447                 }
448
449                 /* reject if target != 0 or if LUN is higher than
450                  * the maximum known LUN
451                  */
452                 else if (chip->srb->device->id) {
453                         dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
454                                 chip->srb->device->id,
455                                 (u8)chip->srb->device->lun);
456                         chip->srb->result = DID_BAD_TARGET << 16;
457                 }
458
459                 else if (chip->srb->device->lun > chip->max_lun) {
460                         dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
461                                 chip->srb->device->id,
462                                 (u8)chip->srb->device->lun);
463                         chip->srb->result = DID_BAD_TARGET << 16;
464                 }
465
466                 /* we've got a command, let's do it! */
467                 else {
468                         scsi_show_command(chip);
469                         rtsx_invoke_transport(chip->srb, chip);
470                 }
471
472                 /* lock access to the state */
473                 scsi_lock(host);
474
475                 /* did the command already complete because of a disconnect? */
476                 if (!chip->srb)
477                         ;               /* nothing to do */
478
479                 /* indicate that the command is done */
480                 else if (chip->srb->result != DID_ABORT << 16) {
481                         chip->srb->scsi_done(chip->srb);
482                 } else {
483 SkipForAbort:
484                         dev_err(&dev->pci->dev, "scsi command aborted\n");
485                 }
486
487                 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
488                         complete(&(dev->notify));
489
490                         rtsx_set_stat(chip, RTSX_STAT_IDLE);
491                 }
492
493                 /* finished working on this command */
494                 chip->srb = NULL;
495                 scsi_unlock(host);
496
497                 /* unlock the device pointers */
498                 mutex_unlock(&dev->dev_mutex);
499         } /* for (;;) */
500
501         /* notify the exit routine that we're actually exiting now
502          *
503          * complete()/wait_for_completion() is similar to up()/down(),
504          * except that complete() is safe in the case where the structure
505          * is getting deleted in a parallel mode of execution (i.e. just
506          * after the down() -- that's necessary for the thread-shutdown
507          * case.
508          *
509          * complete_and_exit() goes even further than this -- it is safe in
510          * the case that the thread of the caller is going away (not just
511          * the structure) -- this is necessary for the module-remove case.
512          * This is important in preemption kernels, which transfer the flow
513          * of execution immediately upon a complete().
514          */
515         complete_and_exit(&dev->control_exit, 0);
516 }
517
518
519 static int rtsx_polling_thread(void *__dev)
520 {
521         struct rtsx_dev *dev = __dev;
522         struct rtsx_chip *chip = dev->chip;
523         struct sd_info *sd_card = &(chip->sd_card);
524         struct xd_info *xd_card = &(chip->xd_card);
525         struct ms_info *ms_card = &(chip->ms_card);
526
527         sd_card->cleanup_counter = 0;
528         xd_card->cleanup_counter = 0;
529         ms_card->cleanup_counter = 0;
530
531         /* Wait until SCSI scan finished */
532         wait_timeout((delay_use + 5) * 1000);
533
534         for (;;) {
535
536                 set_current_state(TASK_INTERRUPTIBLE);
537                 schedule_timeout(msecs_to_jiffies(POLLING_INTERVAL));
538
539                 /* lock the device pointers */
540                 mutex_lock(&(dev->dev_mutex));
541
542                 /* if the device has disconnected, we are free to exit */
543                 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
544                         dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
545                         mutex_unlock(&dev->dev_mutex);
546                         break;
547                 }
548
549                 mutex_unlock(&dev->dev_mutex);
550
551                 mspro_polling_format_status(chip);
552
553                 /* lock the device pointers */
554                 mutex_lock(&(dev->dev_mutex));
555
556                 rtsx_polling_func(chip);
557
558                 /* unlock the device pointers */
559                 mutex_unlock(&dev->dev_mutex);
560         }
561
562         complete_and_exit(&dev->polling_exit, 0);
563 }
564
565 /*
566  * interrupt handler
567  */
568 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
569 {
570         struct rtsx_dev *dev = dev_id;
571         struct rtsx_chip *chip;
572         int retval;
573         u32 status;
574
575         if (dev)
576                 chip = dev->chip;
577         else
578                 return IRQ_NONE;
579
580         if (!chip)
581                 return IRQ_NONE;
582
583         spin_lock(&dev->reg_lock);
584
585         retval = rtsx_pre_handle_interrupt(chip);
586         if (retval == STATUS_FAIL) {
587                 spin_unlock(&dev->reg_lock);
588                 if (chip->int_reg == 0xFFFFFFFF)
589                         return IRQ_HANDLED;
590                 return IRQ_NONE;
591         }
592
593         status = chip->int_reg;
594
595         if (dev->check_card_cd) {
596                 if (!(dev->check_card_cd & status)) {
597                         /* card not exist, return TRANS_RESULT_FAIL */
598                         dev->trans_result = TRANS_RESULT_FAIL;
599                         if (dev->done)
600                                 complete(dev->done);
601                         goto Exit;
602                 }
603         }
604
605         if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
606                 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
607                         if (status & DELINK_INT)
608                                 RTSX_SET_DELINK(chip);
609                         dev->trans_result = TRANS_RESULT_FAIL;
610                         if (dev->done)
611                                 complete(dev->done);
612                 } else if (status & TRANS_OK_INT) {
613                         dev->trans_result = TRANS_RESULT_OK;
614                         if (dev->done)
615                                 complete(dev->done);
616                 } else if (status & DATA_DONE_INT) {
617                         dev->trans_result = TRANS_NOT_READY;
618                         if (dev->done && (dev->trans_state == STATE_TRANS_SG))
619                                 complete(dev->done);
620                 }
621         }
622
623 Exit:
624         spin_unlock(&dev->reg_lock);
625         return IRQ_HANDLED;
626 }
627
628
629 /* Release all our dynamic resources */
630 static void rtsx_release_resources(struct rtsx_dev *dev)
631 {
632         dev_info(&dev->pci->dev, "-- %s\n", __func__);
633
634         /* Tell the control thread to exit.  The SCSI host must
635          * already have been removed so it won't try to queue
636          * any more commands.
637          */
638         dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
639         complete(&dev->cmnd_ready);
640         if (dev->ctl_thread)
641                 wait_for_completion(&dev->control_exit);
642         if (dev->polling_thread)
643                 wait_for_completion(&dev->polling_exit);
644
645         wait_timeout(200);
646
647         if (dev->rtsx_resv_buf) {
648                 dev->chip->host_cmds_ptr = NULL;
649                 dev->chip->host_sg_tbl_ptr = NULL;
650         }
651
652         if (dev->irq > 0)
653                 free_irq(dev->irq, (void *)dev);
654         if (dev->chip->msi_en)
655                 pci_disable_msi(dev->pci);
656         if (dev->remap_addr)
657                 iounmap(dev->remap_addr);
658
659         rtsx_release_chip(dev->chip);
660         kfree(dev->chip);
661 }
662
663 /* First stage of disconnect processing: stop all commands and remove
664  * the host */
665 static void quiesce_and_remove_host(struct rtsx_dev *dev)
666 {
667         struct Scsi_Host *host = rtsx_to_host(dev);
668         struct rtsx_chip *chip = dev->chip;
669
670         /* Prevent new transfers, stop the current command, and
671          * interrupt a SCSI-scan or device-reset delay */
672         mutex_lock(&dev->dev_mutex);
673         scsi_lock(host);
674         rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
675         scsi_unlock(host);
676         mutex_unlock(&dev->dev_mutex);
677         wake_up(&dev->delay_wait);
678         wait_for_completion(&dev->scanning_done);
679
680         /* Wait some time to let other threads exist */
681         wait_timeout(100);
682
683         /* queuecommand won't accept any new commands and the control
684          * thread won't execute a previously-queued command.  If there
685          * is such a command pending, complete it with an error. */
686         mutex_lock(&dev->dev_mutex);
687         if (chip->srb) {
688                 chip->srb->result = DID_NO_CONNECT << 16;
689                 scsi_lock(host);
690                 chip->srb->scsi_done(dev->chip->srb);
691                 chip->srb = NULL;
692                 scsi_unlock(host);
693         }
694         mutex_unlock(&dev->dev_mutex);
695
696         /* Now we own no commands so it's safe to remove the SCSI host */
697         scsi_remove_host(host);
698 }
699
700 /* Second stage of disconnect processing: deallocate all resources */
701 static void release_everything(struct rtsx_dev *dev)
702 {
703         rtsx_release_resources(dev);
704
705         /* Drop our reference to the host; the SCSI core will free it
706          * when the refcount becomes 0. */
707         scsi_host_put(rtsx_to_host(dev));
708 }
709
710 /* Thread to carry out delayed SCSI-device scanning */
711 static int rtsx_scan_thread(void *__dev)
712 {
713         struct rtsx_dev *dev = __dev;
714         struct rtsx_chip *chip = dev->chip;
715
716         /* Wait for the timeout to expire or for a disconnect */
717         if (delay_use > 0) {
718                 dev_info(&dev->pci->dev,
719                          "%s: waiting for device to settle before scanning\n",
720                          CR_DRIVER_NAME);
721                 wait_event_interruptible_timeout(dev->delay_wait,
722                                 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
723                                 delay_use * HZ);
724         }
725
726         /* If the device is still connected, perform the scanning */
727         if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
728                 scsi_scan_host(rtsx_to_host(dev));
729                 dev_info(&dev->pci->dev, "%s: device scan complete\n",
730                          CR_DRIVER_NAME);
731
732                 /* Should we unbind if no devices were detected? */
733         }
734
735         complete_and_exit(&dev->scanning_done, 0);
736 }
737
738 static void rtsx_init_options(struct rtsx_chip *chip)
739 {
740         chip->vendor_id = chip->rtsx->pci->vendor;
741         chip->product_id = chip->rtsx->pci->device;
742         chip->adma_mode = 1;
743         chip->lun_mc = 0;
744         chip->driver_first_load = 1;
745 #ifdef HW_AUTO_SWITCH_SD_BUS
746         chip->sdio_in_charge = 0;
747 #endif
748
749         chip->mspro_formatter_enable = 1;
750         chip->ignore_sd = 0;
751         chip->use_hw_setting = 0;
752         chip->lun_mode = DEFAULT_SINGLE;
753         chip->auto_delink_en = auto_delink_en;
754         chip->ss_en = ss_en;
755         chip->ss_idle_period = ss_interval * 1000;
756         chip->remote_wakeup_en = 0;
757         chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
758         chip->dynamic_aspm = 1;
759         chip->fpga_sd_sdr104_clk = CLK_200;
760         chip->fpga_sd_ddr50_clk = CLK_100;
761         chip->fpga_sd_sdr50_clk = CLK_100;
762         chip->fpga_sd_hs_clk = CLK_100;
763         chip->fpga_mmc_52m_clk = CLK_80;
764         chip->fpga_ms_hg_clk = CLK_80;
765         chip->fpga_ms_4bit_clk = CLK_80;
766         chip->fpga_ms_1bit_clk = CLK_40;
767         chip->asic_sd_sdr104_clk = 203;
768         chip->asic_sd_sdr50_clk = 98;
769         chip->asic_sd_ddr50_clk = 98;
770         chip->asic_sd_hs_clk = 98;
771         chip->asic_mmc_52m_clk = 98;
772         chip->asic_ms_hg_clk = 117;
773         chip->asic_ms_4bit_clk = 78;
774         chip->asic_ms_1bit_clk = 39;
775         chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
776         chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
777         chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
778         chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
779         chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
780         chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
781         chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
782         chip->ssc_depth_low_speed = SSC_DEPTH_512K;
783         chip->ssc_en = 1;
784         chip->sd_speed_prior = 0x01040203;
785         chip->sd_current_prior = 0x00010203;
786         chip->sd_ctl = SD_PUSH_POINT_AUTO |
787                        SD_SAMPLE_POINT_AUTO |
788                        SUPPORT_MMC_DDR_MODE;
789         chip->sd_ddr_tx_phase = 0;
790         chip->mmc_ddr_tx_phase = 1;
791         chip->sd_default_tx_phase = 15;
792         chip->sd_default_rx_phase = 15;
793         chip->pmos_pwr_on_interval = 200;
794         chip->sd_voltage_switch_delay = 1000;
795         chip->ms_power_class_en = 3;
796
797         chip->sd_400mA_ocp_thd = 1;
798         chip->sd_800mA_ocp_thd = 5;
799         chip->ms_ocp_thd = 2;
800
801         chip->card_drive_sel = 0x55;
802         chip->sd30_drive_sel_1v8 = 0x03;
803         chip->sd30_drive_sel_3v3 = 0x01;
804
805         chip->do_delink_before_power_down = 1;
806         chip->auto_power_down = 1;
807         chip->polling_config = 0;
808
809         chip->force_clkreq_0 = 1;
810         chip->ft2_fast_mode = 0;
811
812         chip->sdio_retry_cnt = 1;
813
814         chip->xd_timeout = 2000;
815         chip->sd_timeout = 10000;
816         chip->ms_timeout = 2000;
817         chip->mspro_timeout = 15000;
818
819         chip->power_down_in_ss = 1;
820
821         chip->sdr104_en = 1;
822         chip->sdr50_en = 1;
823         chip->ddr50_en = 1;
824
825         chip->delink_stage1_step = 100;
826         chip->delink_stage2_step = 40;
827         chip->delink_stage3_step = 20;
828
829         chip->auto_delink_in_L1 = 1;
830         chip->blink_led = 1;
831         chip->msi_en = msi_en;
832         chip->hp_watch_bios_hotplug = 0;
833         chip->max_payload = 0;
834         chip->phy_voltage = 0;
835
836         chip->support_ms_8bit = 1;
837         chip->s3_pwr_off_delay = 1000;
838 }
839
840 static int rtsx_probe(struct pci_dev *pci,
841                                 const struct pci_device_id *pci_id)
842 {
843         struct Scsi_Host *host;
844         struct rtsx_dev *dev;
845         int err = 0;
846         struct task_struct *th;
847
848         dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
849
850         err = pcim_enable_device(pci);
851         if (err < 0) {
852                 dev_err(&pci->dev, "PCI enable device failed!\n");
853                 return err;
854         }
855
856         err = pci_request_regions(pci, CR_DRIVER_NAME);
857         if (err < 0) {
858                 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
859                         CR_DRIVER_NAME);
860                 return err;
861         }
862
863         /*
864          * Ask the SCSI layer to allocate a host structure, with extra
865          * space at the end for our private rtsx_dev structure.
866          */
867         host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
868         if (!host) {
869                 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
870                 return -ENOMEM;
871         }
872
873         dev = host_to_rtsx(host);
874         memset(dev, 0, sizeof(struct rtsx_dev));
875
876         dev->chip = kzalloc(sizeof(struct rtsx_chip), GFP_KERNEL);
877         if (!dev->chip) {
878                 err = -ENOMEM;
879                 goto errout;
880         }
881
882         spin_lock_init(&dev->reg_lock);
883         mutex_init(&(dev->dev_mutex));
884         init_completion(&dev->cmnd_ready);
885         init_completion(&dev->control_exit);
886         init_completion(&dev->polling_exit);
887         init_completion(&(dev->notify));
888         init_completion(&dev->scanning_done);
889         init_waitqueue_head(&dev->delay_wait);
890
891         dev->pci = pci;
892         dev->irq = -1;
893
894         dev_info(&pci->dev, "Resource length: 0x%x\n",
895                  (unsigned int)pci_resource_len(pci, 0));
896         dev->addr = pci_resource_start(pci, 0);
897         dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
898         if (!dev->remap_addr) {
899                 dev_err(&pci->dev, "ioremap error\n");
900                 err = -ENXIO;
901                 goto errout;
902         }
903
904         /*
905          * Using "unsigned long" cast here to eliminate gcc warning in
906          * 64-bit system
907          */
908         dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
909                  (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
910
911         dev->rtsx_resv_buf = dmam_alloc_coherent(&pci->dev, RTSX_RESV_BUF_LEN,
912                         &dev->rtsx_resv_buf_addr, GFP_KERNEL);
913         if (!dev->rtsx_resv_buf) {
914                 dev_err(&pci->dev, "alloc dma buffer fail\n");
915                 err = -ENXIO;
916                 goto errout;
917         }
918         dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
919         dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
920         dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
921         dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
922                                       HOST_CMDS_BUF_LEN;
923
924         dev->chip->rtsx = dev;
925
926         rtsx_init_options(dev->chip);
927
928         dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
929
930         if (dev->chip->msi_en) {
931                 if (pci_enable_msi(pci) < 0)
932                         dev->chip->msi_en = 0;
933         }
934
935         if (rtsx_acquire_irq(dev) < 0) {
936                 err = -EBUSY;
937                 goto errout;
938         }
939
940         pci_set_master(pci);
941         synchronize_irq(dev->irq);
942
943         rtsx_init_chip(dev->chip);
944
945         /* set the supported max_lun and max_id for the scsi host
946          * NOTE: the minimal value of max_id is 1 */
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, "rtsx_probe() failed\n");
992         release_everything(dev);
993
994         return err;
995 }
996
997
998 static void rtsx_remove(struct pci_dev *pci)
999 {
1000         struct rtsx_dev *dev = pci_get_drvdata(pci);
1001
1002         dev_info(&pci->dev, "rtsx_remove() called\n");
1003
1004         quiesce_and_remove_host(dev);
1005         release_everything(dev);
1006 }
1007
1008 /* PCI IDs */
1009 static const struct pci_device_id rtsx_ids[] = {
1010         { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208),
1011                 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1012         { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288),
1013                 PCI_CLASS_OTHERS << 16, 0xFF0000 },
1014         { 0, },
1015 };
1016
1017 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1018
1019 /* pci_driver definition */
1020 static struct pci_driver rtsx_driver = {
1021         .name = CR_DRIVER_NAME,
1022         .id_table = rtsx_ids,
1023         .probe = rtsx_probe,
1024         .remove = rtsx_remove,
1025 #ifdef CONFIG_PM
1026         .suspend = rtsx_suspend,
1027         .resume = rtsx_resume,
1028 #endif
1029         .shutdown = rtsx_shutdown,
1030 };
1031
1032 module_pci_driver(rtsx_driver);