rtsx.c 27 KB

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