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