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