Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Driver for Realtek PCI-Express card reader
0004  *
0005  * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
0006  *
0007  * Author:
0008  *   Wei WANG (wei_wang@realsil.com.cn)
0009  *   Micky Ching (micky_ching@realsil.com.cn)
0010  */
0011 
0012 #include <linux/blkdev.h>
0013 #include <linux/kthread.h>
0014 #include <linux/sched.h>
0015 #include <linux/workqueue.h>
0016 
0017 #include "rtsx.h"
0018 #include "ms.h"
0019 #include "sd.h"
0020 #include "xd.h"
0021 
0022 MODULE_DESCRIPTION("Realtek PCI-Express card reader rts5208/rts5288 driver");
0023 MODULE_LICENSE("GPL");
0024 
0025 static unsigned int delay_use = 1;
0026 module_param(delay_use, uint, 0644);
0027 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
0028 
0029 static int ss_en;
0030 module_param(ss_en, int, 0644);
0031 MODULE_PARM_DESC(ss_en, "enable selective suspend");
0032 
0033 static int ss_interval = 50;
0034 module_param(ss_interval, int, 0644);
0035 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
0036 
0037 static int auto_delink_en;
0038 module_param(auto_delink_en, int, 0644);
0039 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
0040 
0041 static unsigned char aspm_l0s_l1_en;
0042 module_param(aspm_l0s_l1_en, byte, 0644);
0043 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
0044 
0045 static int msi_en;
0046 module_param(msi_en, int, 0644);
0047 MODULE_PARM_DESC(msi_en, "enable msi");
0048 
0049 static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
0050 
0051 /***********************************************************************
0052  * Host functions
0053  ***********************************************************************/
0054 
0055 static const char *host_info(struct Scsi_Host *host)
0056 {
0057     return "SCSI emulation for PCI-Express Mass Storage devices";
0058 }
0059 
0060 static int slave_alloc(struct scsi_device *sdev)
0061 {
0062     /*
0063      * Set the INQUIRY transfer length to 36.  We don't use any of
0064      * the extra data and many devices choke if asked for more or
0065      * less than 36 bytes.
0066      */
0067     sdev->inquiry_len = 36;
0068     return 0;
0069 }
0070 
0071 static int slave_configure(struct scsi_device *sdev)
0072 {
0073     /*
0074      * Scatter-gather buffers (all but the last) must have a length
0075      * divisible by the bulk maxpacket size.  Otherwise a data packet
0076      * would end up being short, causing a premature end to the data
0077      * transfer.  Since high-speed bulk pipes have a maxpacket size
0078      * of 512, we'll use that as the scsi device queue's DMA alignment
0079      * mask.  Guaranteeing proper alignment of the first buffer will
0080      * have the desired effect because, except at the beginning and
0081      * the end, scatter-gather buffers follow page boundaries.
0082      */
0083     blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
0084 
0085     /* Set the SCSI level to at least 2.  We'll leave it at 3 if that's
0086      * what is originally reported.  We need this to avoid confusing
0087      * the SCSI layer with devices that report 0 or 1, but need 10-byte
0088      * commands (ala ATAPI devices behind certain bridges, or devices
0089      * which simply have broken INQUIRY data).
0090      *
0091      * NOTE: This means /dev/sg programs (ala cdrecord) will get the
0092      * actual information.  This seems to be the preference for
0093      * programs like that.
0094      *
0095      * NOTE: This also means that /proc/scsi/scsi and sysfs may report
0096      * the actual value or the modified one, depending on where the
0097      * data comes from.
0098      */
0099     if (sdev->scsi_level < SCSI_2) {
0100         sdev->scsi_level = SCSI_2;
0101         sdev->sdev_target->scsi_level = SCSI_2;
0102     }
0103 
0104     return 0;
0105 }
0106 
0107 /***********************************************************************
0108  * /proc/scsi/ functions
0109  ***********************************************************************/
0110 
0111 /* we use this macro to help us write into the buffer */
0112 #undef SPRINTF
0113 #define SPRINTF(args...) \
0114     do { \
0115         if (pos < buffer + length) \
0116             pos += sprintf(pos, ## args); \
0117     } while (0)
0118 
0119 /* queue a command */
0120 /* This is always called with scsi_lock(host) held */
0121 static int queuecommand_lck(struct scsi_cmnd *srb)
0122 {
0123     void (*done)(struct scsi_cmnd *) = scsi_done;
0124     struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
0125     struct rtsx_chip *chip = dev->chip;
0126 
0127     /* check for state-transition errors */
0128     if (chip->srb) {
0129         dev_err(&dev->pci->dev, "Error: chip->srb = %p\n",
0130             chip->srb);
0131         return SCSI_MLQUEUE_HOST_BUSY;
0132     }
0133 
0134     /* fail the command if we are disconnecting */
0135     if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
0136         dev_info(&dev->pci->dev, "Fail command during disconnect\n");
0137         srb->result = DID_NO_CONNECT << 16;
0138         done(srb);
0139         return 0;
0140     }
0141 
0142     /* enqueue the command and wake up the control thread */
0143     chip->srb = srb;
0144     complete(&dev->cmnd_ready);
0145 
0146     return 0;
0147 }
0148 
0149 static DEF_SCSI_QCMD(queuecommand)
0150 
0151 /***********************************************************************
0152  * Error handling functions
0153  ***********************************************************************/
0154 
0155 /* Command timeout and abort */
0156 static int command_abort(struct scsi_cmnd *srb)
0157 {
0158     struct Scsi_Host *host = srb->device->host;
0159     struct rtsx_dev *dev = host_to_rtsx(host);
0160     struct rtsx_chip *chip = dev->chip;
0161 
0162     scsi_lock(host);
0163 
0164     /* Is this command still active? */
0165     if (chip->srb != srb) {
0166         scsi_unlock(host);
0167         dev_info(&dev->pci->dev, "-- nothing to abort\n");
0168         return FAILED;
0169     }
0170 
0171     rtsx_set_stat(chip, RTSX_STAT_ABORT);
0172 
0173     scsi_unlock(host);
0174 
0175     /* Wait for the aborted command to finish */
0176     wait_for_completion(&dev->notify);
0177 
0178     return SUCCESS;
0179 }
0180 
0181 /*
0182  * This invokes the transport reset mechanism to reset the state of the
0183  * device
0184  */
0185 static int device_reset(struct scsi_cmnd *srb)
0186 {
0187     return SUCCESS;
0188 }
0189 
0190 /*
0191  * this defines our host template, with which we'll allocate hosts
0192  */
0193 
0194 static struct scsi_host_template rtsx_host_template = {
0195     /* basic userland interface stuff */
0196     .name =             CR_DRIVER_NAME,
0197     .proc_name =            CR_DRIVER_NAME,
0198     .info =             host_info,
0199 
0200     /* command interface -- queued only */
0201     .queuecommand =         queuecommand,
0202 
0203     /* error and abort handlers */
0204     .eh_abort_handler =     command_abort,
0205     .eh_device_reset_handler =  device_reset,
0206 
0207     /* queue commands only, only one command per LUN */
0208     .can_queue =            1,
0209 
0210     /* unknown initiator id */
0211     .this_id =          -1,
0212 
0213     .slave_alloc =          slave_alloc,
0214     .slave_configure =      slave_configure,
0215 
0216     /* lots of sg segments can be handled */
0217     .sg_tablesize =         SG_ALL,
0218 
0219     /* limit the total size of a transfer to 120 KB */
0220     .max_sectors =                  240,
0221 
0222     /* emulated HBA */
0223     .emulated =         1,
0224 
0225     /* we do our own delay after a device or bus reset */
0226     .skip_settle_delay =        1,
0227 
0228     /* module management */
0229     .module =           THIS_MODULE
0230 };
0231 
0232 static int rtsx_acquire_irq(struct rtsx_dev *dev)
0233 {
0234     struct rtsx_chip *chip = dev->chip;
0235 
0236     dev_info(&dev->pci->dev, "%s: chip->msi_en = %d, pci->irq = %d\n",
0237          __func__, chip->msi_en, dev->pci->irq);
0238 
0239     if (request_irq(dev->pci->irq, rtsx_interrupt,
0240             chip->msi_en ? 0 : IRQF_SHARED,
0241             CR_DRIVER_NAME, dev)) {
0242         dev_err(&dev->pci->dev,
0243             "rtsx: unable to grab IRQ %d, disabling device\n",
0244             dev->pci->irq);
0245         return -1;
0246     }
0247 
0248     dev->irq = dev->pci->irq;
0249     pci_intx(dev->pci, !chip->msi_en);
0250 
0251     return 0;
0252 }
0253 
0254 /*
0255  * power management
0256  */
0257 static int __maybe_unused rtsx_suspend(struct device *dev_d)
0258 {
0259     struct pci_dev *pci = to_pci_dev(dev_d);
0260     struct rtsx_dev *dev = pci_get_drvdata(pci);
0261     struct rtsx_chip *chip;
0262 
0263     if (!dev)
0264         return 0;
0265 
0266     /* lock the device pointers */
0267     mutex_lock(&dev->dev_mutex);
0268 
0269     chip = dev->chip;
0270 
0271     rtsx_do_before_power_down(chip, PM_S3);
0272 
0273     if (dev->irq >= 0) {
0274         free_irq(dev->irq, (void *)dev);
0275         dev->irq = -1;
0276     }
0277 
0278     if (chip->msi_en)
0279         pci_free_irq_vectors(pci);
0280 
0281     device_wakeup_enable(dev_d);
0282 
0283     /* unlock the device pointers */
0284     mutex_unlock(&dev->dev_mutex);
0285 
0286     return 0;
0287 }
0288 
0289 static int __maybe_unused rtsx_resume(struct device *dev_d)
0290 {
0291     struct pci_dev *pci = to_pci_dev(dev_d);
0292     struct rtsx_dev *dev = pci_get_drvdata(pci);
0293     struct rtsx_chip *chip;
0294 
0295     if (!dev)
0296         return 0;
0297 
0298     chip = dev->chip;
0299 
0300     /* lock the device pointers */
0301     mutex_lock(&dev->dev_mutex);
0302 
0303     pci_set_master(pci);
0304 
0305     if (chip->msi_en) {
0306         if (pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) < 0)
0307             chip->msi_en = 0;
0308     }
0309 
0310     if (rtsx_acquire_irq(dev) < 0) {
0311         /* unlock the device pointers */
0312         mutex_unlock(&dev->dev_mutex);
0313         return -EIO;
0314     }
0315 
0316     rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
0317     rtsx_init_chip(chip);
0318 
0319     /* unlock the device pointers */
0320     mutex_unlock(&dev->dev_mutex);
0321 
0322     return 0;
0323 }
0324 
0325 static void rtsx_shutdown(struct pci_dev *pci)
0326 {
0327     struct rtsx_dev *dev = pci_get_drvdata(pci);
0328     struct rtsx_chip *chip;
0329 
0330     if (!dev)
0331         return;
0332 
0333     chip = dev->chip;
0334 
0335     rtsx_do_before_power_down(chip, PM_S1);
0336 
0337     if (dev->irq >= 0) {
0338         free_irq(dev->irq, (void *)dev);
0339         dev->irq = -1;
0340     }
0341 
0342     if (chip->msi_en)
0343         pci_free_irq_vectors(pci);
0344 
0345     pci_disable_device(pci);
0346 }
0347 
0348 static int rtsx_control_thread(void *__dev)
0349 {
0350     struct rtsx_dev *dev = __dev;
0351     struct rtsx_chip *chip = dev->chip;
0352     struct Scsi_Host *host = rtsx_to_host(dev);
0353 
0354     for (;;) {
0355         if (wait_for_completion_interruptible(&dev->cmnd_ready))
0356             break;
0357 
0358         /* lock the device pointers */
0359         mutex_lock(&dev->dev_mutex);
0360 
0361         /* if the device has disconnected, we are free to exit */
0362         if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
0363             dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
0364             mutex_unlock(&dev->dev_mutex);
0365             break;
0366         }
0367 
0368         /* lock access to the state */
0369         scsi_lock(host);
0370 
0371         /* has the command aborted ? */
0372         if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
0373             chip->srb->result = DID_ABORT << 16;
0374             goto skip_for_abort;
0375         }
0376 
0377         scsi_unlock(host);
0378 
0379         /* reject the command if the direction indicator
0380          * is UNKNOWN
0381          */
0382         if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
0383             dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
0384             chip->srb->result = DID_ERROR << 16;
0385         }
0386 
0387         /* reject if target != 0 or if LUN is higher than
0388          * the maximum known LUN
0389          */
0390         else if (chip->srb->device->id) {
0391             dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
0392                 chip->srb->device->id,
0393                 (u8)chip->srb->device->lun);
0394             chip->srb->result = DID_BAD_TARGET << 16;
0395         }
0396 
0397         else if (chip->srb->device->lun > chip->max_lun) {
0398             dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
0399                 chip->srb->device->id,
0400                 (u8)chip->srb->device->lun);
0401             chip->srb->result = DID_BAD_TARGET << 16;
0402         }
0403 
0404         /* we've got a command, let's do it! */
0405         else {
0406             scsi_show_command(chip);
0407             rtsx_invoke_transport(chip->srb, chip);
0408         }
0409 
0410         /* lock access to the state */
0411         scsi_lock(host);
0412 
0413         /* did the command already complete because of a disconnect? */
0414         if (!chip->srb)
0415             ;       /* nothing to do */
0416 
0417         /* indicate that the command is done */
0418         else if (chip->srb->result != DID_ABORT << 16) {
0419             scsi_done(chip->srb);
0420         } else {
0421 skip_for_abort:
0422             dev_err(&dev->pci->dev, "scsi command aborted\n");
0423         }
0424 
0425         if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
0426             complete(&dev->notify);
0427 
0428             rtsx_set_stat(chip, RTSX_STAT_IDLE);
0429         }
0430 
0431         /* finished working on this command */
0432         chip->srb = NULL;
0433         scsi_unlock(host);
0434 
0435         /* unlock the device pointers */
0436         mutex_unlock(&dev->dev_mutex);
0437     } /* for (;;) */
0438 
0439     /* notify the exit routine that we're actually exiting now
0440      *
0441      * complete()/wait_for_completion() is similar to up()/down(),
0442      * except that complete() is safe in the case where the structure
0443      * is getting deleted in a parallel mode of execution (i.e. just
0444      * after the down() -- that's necessary for the thread-shutdown
0445      * case.
0446      *
0447      * kthread_complete_and_exit() goes even further than this --
0448      * it is safe in the case that the thread of the caller is going away
0449      * (not just the structure) -- this is necessary for the module-remove
0450      * case.  This is important in preemption kernels, which transfer the
0451      * flow of execution immediately upon a complete().
0452      */
0453     kthread_complete_and_exit(&dev->control_exit, 0);
0454 }
0455 
0456 static int rtsx_polling_thread(void *__dev)
0457 {
0458     struct rtsx_dev *dev = __dev;
0459     struct rtsx_chip *chip = dev->chip;
0460     struct sd_info *sd_card = &chip->sd_card;
0461     struct xd_info *xd_card = &chip->xd_card;
0462     struct ms_info *ms_card = &chip->ms_card;
0463 
0464     sd_card->cleanup_counter = 0;
0465     xd_card->cleanup_counter = 0;
0466     ms_card->cleanup_counter = 0;
0467 
0468     /* Wait until SCSI scan finished */
0469     wait_timeout((delay_use + 5) * 1000);
0470 
0471     for (;;) {
0472         set_current_state(TASK_INTERRUPTIBLE);
0473         schedule_timeout(msecs_to_jiffies(POLLING_INTERVAL));
0474 
0475         /* lock the device pointers */
0476         mutex_lock(&dev->dev_mutex);
0477 
0478         /* if the device has disconnected, we are free to exit */
0479         if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
0480             dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
0481             mutex_unlock(&dev->dev_mutex);
0482             break;
0483         }
0484 
0485         mutex_unlock(&dev->dev_mutex);
0486 
0487         mspro_polling_format_status(chip);
0488 
0489         /* lock the device pointers */
0490         mutex_lock(&dev->dev_mutex);
0491 
0492         rtsx_polling_func(chip);
0493 
0494         /* unlock the device pointers */
0495         mutex_unlock(&dev->dev_mutex);
0496     }
0497 
0498     kthread_complete_and_exit(&dev->polling_exit, 0);
0499 }
0500 
0501 /*
0502  * interrupt handler
0503  */
0504 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
0505 {
0506     struct rtsx_dev *dev = dev_id;
0507     struct rtsx_chip *chip;
0508     int retval;
0509     u32 status;
0510 
0511     if (dev)
0512         chip = dev->chip;
0513     else
0514         return IRQ_NONE;
0515 
0516     if (!chip)
0517         return IRQ_NONE;
0518 
0519     spin_lock(&dev->reg_lock);
0520 
0521     retval = rtsx_pre_handle_interrupt(chip);
0522     if (retval == STATUS_FAIL) {
0523         spin_unlock(&dev->reg_lock);
0524         if (chip->int_reg == 0xFFFFFFFF)
0525             return IRQ_HANDLED;
0526         return IRQ_NONE;
0527     }
0528 
0529     status = chip->int_reg;
0530 
0531     if (dev->check_card_cd) {
0532         if (!(dev->check_card_cd & status)) {
0533             /* card not exist, return TRANS_RESULT_FAIL */
0534             dev->trans_result = TRANS_RESULT_FAIL;
0535             if (dev->done)
0536                 complete(dev->done);
0537             goto exit;
0538         }
0539     }
0540 
0541     if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
0542         if (status & (TRANS_FAIL_INT | DELINK_INT)) {
0543             if (status & DELINK_INT)
0544                 RTSX_SET_DELINK(chip);
0545             dev->trans_result = TRANS_RESULT_FAIL;
0546             if (dev->done)
0547                 complete(dev->done);
0548         } else if (status & TRANS_OK_INT) {
0549             dev->trans_result = TRANS_RESULT_OK;
0550             if (dev->done)
0551                 complete(dev->done);
0552         } else if (status & DATA_DONE_INT) {
0553             dev->trans_result = TRANS_NOT_READY;
0554             if (dev->done && dev->trans_state == STATE_TRANS_SG)
0555                 complete(dev->done);
0556         }
0557     }
0558 
0559 exit:
0560     spin_unlock(&dev->reg_lock);
0561     return IRQ_HANDLED;
0562 }
0563 
0564 /* Release all our dynamic resources */
0565 static void rtsx_release_resources(struct rtsx_dev *dev)
0566 {
0567     dev_info(&dev->pci->dev, "-- %s\n", __func__);
0568 
0569     /* Tell the control thread to exit.  The SCSI host must
0570      * already have been removed so it won't try to queue
0571      * any more commands.
0572      */
0573     dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
0574     complete(&dev->cmnd_ready);
0575     if (dev->ctl_thread)
0576         wait_for_completion(&dev->control_exit);
0577     if (dev->polling_thread)
0578         wait_for_completion(&dev->polling_exit);
0579 
0580     wait_timeout(200);
0581 
0582     if (dev->rtsx_resv_buf) {
0583         dev->chip->host_cmds_ptr = NULL;
0584         dev->chip->host_sg_tbl_ptr = NULL;
0585     }
0586 
0587     if (dev->irq > 0)
0588         free_irq(dev->irq, (void *)dev);
0589     if (dev->chip->msi_en)
0590         pci_free_irq_vectors(dev->pci);
0591     if (dev->remap_addr)
0592         iounmap(dev->remap_addr);
0593 
0594     rtsx_release_chip(dev->chip);
0595     kfree(dev->chip);
0596 }
0597 
0598 /*
0599  * First stage of disconnect processing: stop all commands and remove
0600  * the host
0601  */
0602 static void quiesce_and_remove_host(struct rtsx_dev *dev)
0603 {
0604     struct Scsi_Host *host = rtsx_to_host(dev);
0605     struct rtsx_chip *chip = dev->chip;
0606 
0607     /*
0608      * Prevent new transfers, stop the current command, and
0609      * interrupt a SCSI-scan or device-reset delay
0610      */
0611     mutex_lock(&dev->dev_mutex);
0612     scsi_lock(host);
0613     rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
0614     scsi_unlock(host);
0615     mutex_unlock(&dev->dev_mutex);
0616     wake_up(&dev->delay_wait);
0617     wait_for_completion(&dev->scanning_done);
0618 
0619     /* Wait some time to let other threads exist */
0620     wait_timeout(100);
0621 
0622     /*
0623      * queuecommand won't accept any new commands and the control
0624      * thread won't execute a previously-queued command.  If there
0625      * is such a command pending, complete it with an error.
0626      */
0627     mutex_lock(&dev->dev_mutex);
0628     if (chip->srb) {
0629         chip->srb->result = DID_NO_CONNECT << 16;
0630         scsi_lock(host);
0631         scsi_done(dev->chip->srb);
0632         chip->srb = NULL;
0633         scsi_unlock(host);
0634     }
0635     mutex_unlock(&dev->dev_mutex);
0636 
0637     /* Now we own no commands so it's safe to remove the SCSI host */
0638     scsi_remove_host(host);
0639 }
0640 
0641 /* Second stage of disconnect processing: deallocate all resources */
0642 static void release_everything(struct rtsx_dev *dev)
0643 {
0644     rtsx_release_resources(dev);
0645 
0646     /*
0647      * Drop our reference to the host; the SCSI core will free it
0648      * when the refcount becomes 0.
0649      */
0650     scsi_host_put(rtsx_to_host(dev));
0651 }
0652 
0653 /* Thread to carry out delayed SCSI-device scanning */
0654 static int rtsx_scan_thread(void *__dev)
0655 {
0656     struct rtsx_dev *dev = __dev;
0657     struct rtsx_chip *chip = dev->chip;
0658 
0659     /* Wait for the timeout to expire or for a disconnect */
0660     if (delay_use > 0) {
0661         dev_info(&dev->pci->dev,
0662              "%s: waiting for device to settle before scanning\n",
0663              CR_DRIVER_NAME);
0664         wait_event_interruptible_timeout
0665             (dev->delay_wait,
0666              rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
0667              delay_use * HZ);
0668     }
0669 
0670     /* If the device is still connected, perform the scanning */
0671     if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
0672         scsi_scan_host(rtsx_to_host(dev));
0673         dev_info(&dev->pci->dev, "%s: device scan complete\n",
0674              CR_DRIVER_NAME);
0675 
0676         /* Should we unbind if no devices were detected? */
0677     }
0678 
0679     kthread_complete_and_exit(&dev->scanning_done, 0);
0680 }
0681 
0682 static void rtsx_init_options(struct rtsx_chip *chip)
0683 {
0684     chip->vendor_id = chip->rtsx->pci->vendor;
0685     chip->product_id = chip->rtsx->pci->device;
0686     chip->adma_mode = 1;
0687     chip->lun_mc = 0;
0688     chip->driver_first_load = 1;
0689 #ifdef HW_AUTO_SWITCH_SD_BUS
0690     chip->sdio_in_charge = 0;
0691 #endif
0692 
0693     chip->mspro_formatter_enable = 1;
0694     chip->ignore_sd = 0;
0695     chip->use_hw_setting = 0;
0696     chip->lun_mode = DEFAULT_SINGLE;
0697     chip->auto_delink_en = auto_delink_en;
0698     chip->ss_en = ss_en;
0699     chip->ss_idle_period = ss_interval * 1000;
0700     chip->remote_wakeup_en = 0;
0701     chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
0702     chip->dynamic_aspm = 1;
0703     chip->fpga_sd_sdr104_clk = CLK_200;
0704     chip->fpga_sd_ddr50_clk = CLK_100;
0705     chip->fpga_sd_sdr50_clk = CLK_100;
0706     chip->fpga_sd_hs_clk = CLK_100;
0707     chip->fpga_mmc_52m_clk = CLK_80;
0708     chip->fpga_ms_hg_clk = CLK_80;
0709     chip->fpga_ms_4bit_clk = CLK_80;
0710     chip->fpga_ms_1bit_clk = CLK_40;
0711     chip->asic_sd_sdr104_clk = 203;
0712     chip->asic_sd_sdr50_clk = 98;
0713     chip->asic_sd_ddr50_clk = 98;
0714     chip->asic_sd_hs_clk = 98;
0715     chip->asic_mmc_52m_clk = 98;
0716     chip->asic_ms_hg_clk = 117;
0717     chip->asic_ms_4bit_clk = 78;
0718     chip->asic_ms_1bit_clk = 39;
0719     chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
0720     chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
0721     chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
0722     chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
0723     chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
0724     chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
0725     chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
0726     chip->ssc_depth_low_speed = SSC_DEPTH_512K;
0727     chip->ssc_en = 1;
0728     chip->sd_speed_prior = 0x01040203;
0729     chip->sd_current_prior = 0x00010203;
0730     chip->sd_ctl = SD_PUSH_POINT_AUTO |
0731                SD_SAMPLE_POINT_AUTO |
0732                SUPPORT_MMC_DDR_MODE;
0733     chip->sd_ddr_tx_phase = 0;
0734     chip->mmc_ddr_tx_phase = 1;
0735     chip->sd_default_tx_phase = 15;
0736     chip->sd_default_rx_phase = 15;
0737     chip->pmos_pwr_on_interval = 200;
0738     chip->sd_voltage_switch_delay = 1000;
0739     chip->ms_power_class_en = 3;
0740 
0741     chip->sd_400mA_ocp_thd = 1;
0742     chip->sd_800mA_ocp_thd = 5;
0743     chip->ms_ocp_thd = 2;
0744 
0745     chip->card_drive_sel = 0x55;
0746     chip->sd30_drive_sel_1v8 = 0x03;
0747     chip->sd30_drive_sel_3v3 = 0x01;
0748 
0749     chip->do_delink_before_power_down = 1;
0750     chip->auto_power_down = 1;
0751     chip->polling_config = 0;
0752 
0753     chip->force_clkreq_0 = 1;
0754     chip->ft2_fast_mode = 0;
0755 
0756     chip->sdio_retry_cnt = 1;
0757 
0758     chip->xd_timeout = 2000;
0759     chip->sd_timeout = 10000;
0760     chip->ms_timeout = 2000;
0761     chip->mspro_timeout = 15000;
0762 
0763     chip->power_down_in_ss = 1;
0764 
0765     chip->sdr104_en = 1;
0766     chip->sdr50_en = 1;
0767     chip->ddr50_en = 1;
0768 
0769     chip->delink_stage1_step = 100;
0770     chip->delink_stage2_step = 40;
0771     chip->delink_stage3_step = 20;
0772 
0773     chip->auto_delink_in_L1 = 1;
0774     chip->blink_led = 1;
0775     chip->msi_en = msi_en;
0776     chip->hp_watch_bios_hotplug = 0;
0777     chip->max_payload = 0;
0778     chip->phy_voltage = 0;
0779 
0780     chip->support_ms_8bit = 1;
0781     chip->s3_pwr_off_delay = 1000;
0782 }
0783 
0784 static int rtsx_probe(struct pci_dev *pci,
0785               const struct pci_device_id *pci_id)
0786 {
0787     struct Scsi_Host *host;
0788     struct rtsx_dev *dev;
0789     int err = 0;
0790     struct task_struct *th;
0791 
0792     dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n");
0793 
0794     err = pcim_enable_device(pci);
0795     if (err < 0) {
0796         dev_err(&pci->dev, "PCI enable device failed!\n");
0797         return err;
0798     }
0799 
0800     err = pci_request_regions(pci, CR_DRIVER_NAME);
0801     if (err < 0) {
0802         dev_err(&pci->dev, "PCI request regions for %s failed!\n",
0803             CR_DRIVER_NAME);
0804         return err;
0805     }
0806 
0807     /*
0808      * Ask the SCSI layer to allocate a host structure, with extra
0809      * space at the end for our private rtsx_dev structure.
0810      */
0811     host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
0812     if (!host) {
0813         dev_err(&pci->dev, "Unable to allocate the scsi host\n");
0814         err = -ENOMEM;
0815         goto scsi_host_alloc_fail;
0816     }
0817 
0818     dev = host_to_rtsx(host);
0819     memset(dev, 0, sizeof(struct rtsx_dev));
0820 
0821     dev->chip = kzalloc(sizeof(*dev->chip), GFP_KERNEL);
0822     if (!dev->chip) {
0823         err = -ENOMEM;
0824         goto chip_alloc_fail;
0825     }
0826 
0827     spin_lock_init(&dev->reg_lock);
0828     mutex_init(&dev->dev_mutex);
0829     init_completion(&dev->cmnd_ready);
0830     init_completion(&dev->control_exit);
0831     init_completion(&dev->polling_exit);
0832     init_completion(&dev->notify);
0833     init_completion(&dev->scanning_done);
0834     init_waitqueue_head(&dev->delay_wait);
0835 
0836     dev->pci = pci;
0837     dev->irq = -1;
0838 
0839     dev_info(&pci->dev, "Resource length: 0x%x\n",
0840          (unsigned int)pci_resource_len(pci, 0));
0841     dev->addr = pci_resource_start(pci, 0);
0842     dev->remap_addr = ioremap(dev->addr, pci_resource_len(pci, 0));
0843     if (!dev->remap_addr) {
0844         dev_err(&pci->dev, "ioremap error\n");
0845         err = -ENXIO;
0846         goto ioremap_fail;
0847     }
0848 
0849     /*
0850      * Using "unsigned long" cast here to eliminate gcc warning in
0851      * 64-bit system
0852      */
0853     dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
0854          (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
0855 
0856     dev->rtsx_resv_buf = dmam_alloc_coherent(&pci->dev, RTSX_RESV_BUF_LEN,
0857                          &dev->rtsx_resv_buf_addr,
0858                          GFP_KERNEL);
0859     if (!dev->rtsx_resv_buf) {
0860         dev_err(&pci->dev, "alloc dma buffer fail\n");
0861         err = -ENXIO;
0862         goto dma_alloc_fail;
0863     }
0864     dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
0865     dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
0866     dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
0867     dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
0868                       HOST_CMDS_BUF_LEN;
0869 
0870     dev->chip->rtsx = dev;
0871 
0872     rtsx_init_options(dev->chip);
0873 
0874     dev_info(&pci->dev, "pci->irq = %d\n", pci->irq);
0875 
0876     if (dev->chip->msi_en) {
0877         if (pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) < 0)
0878             dev->chip->msi_en = 0;
0879     }
0880 
0881     if (rtsx_acquire_irq(dev) < 0) {
0882         err = -EBUSY;
0883         goto irq_acquire_fail;
0884     }
0885 
0886     pci_set_master(pci);
0887     synchronize_irq(dev->irq);
0888 
0889     rtsx_init_chip(dev->chip);
0890 
0891     /*
0892      * set the supported max_lun and max_id for the scsi host
0893      * NOTE: the minimal value of max_id is 1
0894      */
0895     host->max_id = 1;
0896     host->max_lun = dev->chip->max_lun;
0897 
0898     /* Start up our control thread */
0899     th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
0900     if (IS_ERR(th)) {
0901         dev_err(&pci->dev, "Unable to start control thread\n");
0902         err = PTR_ERR(th);
0903         goto control_thread_fail;
0904     }
0905     dev->ctl_thread = th;
0906 
0907     err = scsi_add_host(host, &pci->dev);
0908     if (err) {
0909         dev_err(&pci->dev, "Unable to add the scsi host\n");
0910         goto scsi_add_host_fail;
0911     }
0912 
0913     /* Start up the thread for delayed SCSI-device scanning */
0914     th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
0915     if (IS_ERR(th)) {
0916         dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
0917         complete(&dev->scanning_done);
0918         err = PTR_ERR(th);
0919         goto scan_thread_fail;
0920     }
0921 
0922     /* Start up the thread for polling thread */
0923     th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
0924     if (IS_ERR(th)) {
0925         dev_err(&pci->dev, "Unable to start the device-polling thread\n");
0926         err = PTR_ERR(th);
0927         goto scan_thread_fail;
0928     }
0929     dev->polling_thread = th;
0930 
0931     pci_set_drvdata(pci, dev);
0932 
0933     return 0;
0934 
0935     /* We come here if there are any problems */
0936 scan_thread_fail:
0937     quiesce_and_remove_host(dev);
0938 scsi_add_host_fail:
0939     complete(&dev->cmnd_ready);
0940     wait_for_completion(&dev->control_exit);
0941 control_thread_fail:
0942     free_irq(dev->irq, (void *)dev);
0943     rtsx_release_chip(dev->chip);
0944 irq_acquire_fail:
0945     dev->chip->host_cmds_ptr = NULL;
0946     dev->chip->host_sg_tbl_ptr = NULL;
0947     if (dev->chip->msi_en)
0948         pci_free_irq_vectors(dev->pci);
0949 dma_alloc_fail:
0950     iounmap(dev->remap_addr);
0951 ioremap_fail:
0952     kfree(dev->chip);
0953 chip_alloc_fail:
0954     dev_err(&pci->dev, "%s failed\n", __func__);
0955     scsi_host_put(host);
0956 scsi_host_alloc_fail:
0957     pci_release_regions(pci);
0958     return err;
0959 }
0960 
0961 static void rtsx_remove(struct pci_dev *pci)
0962 {
0963     struct rtsx_dev *dev = pci_get_drvdata(pci);
0964 
0965     quiesce_and_remove_host(dev);
0966     release_everything(dev);
0967     pci_release_regions(pci);
0968 }
0969 
0970 /* PCI IDs */
0971 static const struct pci_device_id rtsx_ids[] = {
0972     { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208),
0973         PCI_CLASS_OTHERS << 16, 0xFF0000 },
0974     { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288),
0975         PCI_CLASS_OTHERS << 16, 0xFF0000 },
0976     { 0, },
0977 };
0978 
0979 MODULE_DEVICE_TABLE(pci, rtsx_ids);
0980 
0981 static SIMPLE_DEV_PM_OPS(rtsx_pm_ops, rtsx_suspend, rtsx_resume);
0982 
0983 /* pci_driver definition */
0984 static struct pci_driver rtsx_driver = {
0985     .name = CR_DRIVER_NAME,
0986     .id_table = rtsx_ids,
0987     .probe = rtsx_probe,
0988     .remove = rtsx_remove,
0989     .driver.pm = &rtsx_pm_ops,
0990     .shutdown = rtsx_shutdown,
0991 };
0992 
0993 module_pci_driver(rtsx_driver);