rtsx.c 26 KB

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