libata-pmp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * libata-pmp.c - libata port multiplier support
  3. *
  4. * Copyright (c) 2007 SUSE Linux Products GmbH
  5. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  6. *
  7. * This file is released under the GPLv2.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/libata.h>
  11. #include "libata.h"
  12. const struct ata_port_operations sata_pmp_port_ops = {
  13. .inherits = &sata_port_ops,
  14. .pmp_prereset = ata_std_prereset,
  15. .pmp_hardreset = sata_std_hardreset,
  16. .pmp_postreset = ata_std_postreset,
  17. .error_handler = sata_pmp_error_handler,
  18. };
  19. /**
  20. * sata_pmp_read - read PMP register
  21. * @link: link to read PMP register for
  22. * @reg: register to read
  23. * @r_val: resulting value
  24. *
  25. * Read PMP register.
  26. *
  27. * LOCKING:
  28. * Kernel thread context (may sleep).
  29. *
  30. * RETURNS:
  31. * 0 on success, AC_ERR_* mask on failure.
  32. */
  33. static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
  34. {
  35. struct ata_port *ap = link->ap;
  36. struct ata_device *pmp_dev = ap->link.device;
  37. struct ata_taskfile tf;
  38. unsigned int err_mask;
  39. ata_tf_init(pmp_dev, &tf);
  40. tf.command = ATA_CMD_PMP_READ;
  41. tf.protocol = ATA_PROT_NODATA;
  42. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
  43. tf.feature = reg;
  44. tf.device = link->pmp;
  45. err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
  46. SATA_PMP_RW_TIMEOUT);
  47. if (err_mask)
  48. return err_mask;
  49. *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
  50. return 0;
  51. }
  52. /**
  53. * sata_pmp_write - write PMP register
  54. * @link: link to write PMP register for
  55. * @reg: register to write
  56. * @r_val: value to write
  57. *
  58. * Write PMP register.
  59. *
  60. * LOCKING:
  61. * Kernel thread context (may sleep).
  62. *
  63. * RETURNS:
  64. * 0 on success, AC_ERR_* mask on failure.
  65. */
  66. static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
  67. {
  68. struct ata_port *ap = link->ap;
  69. struct ata_device *pmp_dev = ap->link.device;
  70. struct ata_taskfile tf;
  71. ata_tf_init(pmp_dev, &tf);
  72. tf.command = ATA_CMD_PMP_WRITE;
  73. tf.protocol = ATA_PROT_NODATA;
  74. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
  75. tf.feature = reg;
  76. tf.device = link->pmp;
  77. tf.nsect = val & 0xff;
  78. tf.lbal = (val >> 8) & 0xff;
  79. tf.lbam = (val >> 16) & 0xff;
  80. tf.lbah = (val >> 24) & 0xff;
  81. return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
  82. SATA_PMP_RW_TIMEOUT);
  83. }
  84. /**
  85. * sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
  86. * @qc: ATA command in question
  87. *
  88. * A host which has command switching PMP support cannot issue
  89. * commands to multiple links simultaneously.
  90. *
  91. * LOCKING:
  92. * spin_lock_irqsave(host lock)
  93. *
  94. * RETURNS:
  95. * ATA_DEFER_* if deferring is needed, 0 otherwise.
  96. */
  97. int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
  98. {
  99. struct ata_link *link = qc->dev->link;
  100. struct ata_port *ap = link->ap;
  101. if (ap->excl_link == NULL || ap->excl_link == link) {
  102. if (ap->nr_active_links == 0 || ata_link_active(link)) {
  103. qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
  104. return ata_std_qc_defer(qc);
  105. }
  106. ap->excl_link = link;
  107. }
  108. return ATA_DEFER_PORT;
  109. }
  110. /**
  111. * sata_pmp_scr_read - read PSCR
  112. * @link: ATA link to read PSCR for
  113. * @reg: PSCR to read
  114. * @r_val: resulting value
  115. *
  116. * Read PSCR @reg into @r_val for @link, to be called from
  117. * ata_scr_read().
  118. *
  119. * LOCKING:
  120. * Kernel thread context (may sleep).
  121. *
  122. * RETURNS:
  123. * 0 on success, -errno on failure.
  124. */
  125. int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
  126. {
  127. unsigned int err_mask;
  128. if (reg > SATA_PMP_PSCR_CONTROL)
  129. return -EINVAL;
  130. err_mask = sata_pmp_read(link, reg, r_val);
  131. if (err_mask) {
  132. ata_link_printk(link, KERN_WARNING, "failed to read SCR %d "
  133. "(Emask=0x%x)\n", reg, err_mask);
  134. return -EIO;
  135. }
  136. return 0;
  137. }
  138. /**
  139. * sata_pmp_scr_write - write PSCR
  140. * @link: ATA link to write PSCR for
  141. * @reg: PSCR to write
  142. * @val: value to be written
  143. *
  144. * Write @val to PSCR @reg for @link, to be called from
  145. * ata_scr_write() and ata_scr_write_flush().
  146. *
  147. * LOCKING:
  148. * Kernel thread context (may sleep).
  149. *
  150. * RETURNS:
  151. * 0 on success, -errno on failure.
  152. */
  153. int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
  154. {
  155. unsigned int err_mask;
  156. if (reg > SATA_PMP_PSCR_CONTROL)
  157. return -EINVAL;
  158. err_mask = sata_pmp_write(link, reg, val);
  159. if (err_mask) {
  160. ata_link_printk(link, KERN_WARNING, "failed to write SCR %d "
  161. "(Emask=0x%x)\n", reg, err_mask);
  162. return -EIO;
  163. }
  164. return 0;
  165. }
  166. /**
  167. * sata_pmp_read_gscr - read GSCR block of SATA PMP
  168. * @dev: PMP device
  169. * @gscr: buffer to read GSCR block into
  170. *
  171. * Read selected PMP GSCRs from the PMP at @dev. This will serve
  172. * as configuration and identification info for the PMP.
  173. *
  174. * LOCKING:
  175. * Kernel thread context (may sleep).
  176. *
  177. * RETURNS:
  178. * 0 on success, -errno on failure.
  179. */
  180. static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
  181. {
  182. static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
  183. int i;
  184. for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
  185. int reg = gscr_to_read[i];
  186. unsigned int err_mask;
  187. err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
  188. if (err_mask) {
  189. ata_dev_printk(dev, KERN_ERR, "failed to read PMP "
  190. "GSCR[%d] (Emask=0x%x)\n", reg, err_mask);
  191. return -EIO;
  192. }
  193. }
  194. return 0;
  195. }
  196. static const char *sata_pmp_spec_rev_str(const u32 *gscr)
  197. {
  198. u32 rev = gscr[SATA_PMP_GSCR_REV];
  199. if (rev & (1 << 2))
  200. return "1.1";
  201. if (rev & (1 << 1))
  202. return "1.0";
  203. return "<unknown>";
  204. }
  205. static int sata_pmp_configure(struct ata_device *dev, int print_info)
  206. {
  207. struct ata_port *ap = dev->link->ap;
  208. u32 *gscr = dev->gscr;
  209. unsigned int err_mask = 0;
  210. const char *reason;
  211. int nr_ports, rc;
  212. nr_ports = sata_pmp_gscr_ports(gscr);
  213. if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
  214. rc = -EINVAL;
  215. reason = "invalid nr_ports";
  216. goto fail;
  217. }
  218. if ((ap->flags & ATA_FLAG_AN) &&
  219. (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
  220. dev->flags |= ATA_DFLAG_AN;
  221. /* monitor SERR_PHYRDY_CHG on fan-out ports */
  222. err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
  223. SERR_PHYRDY_CHG);
  224. if (err_mask) {
  225. rc = -EIO;
  226. reason = "failed to write GSCR_ERROR_EN";
  227. goto fail;
  228. }
  229. if (print_info) {
  230. ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
  231. "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
  232. sata_pmp_spec_rev_str(gscr),
  233. sata_pmp_gscr_vendor(gscr),
  234. sata_pmp_gscr_devid(gscr),
  235. sata_pmp_gscr_rev(gscr),
  236. nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
  237. gscr[SATA_PMP_GSCR_FEAT]);
  238. if (!(dev->flags & ATA_DFLAG_AN))
  239. ata_dev_printk(dev, KERN_INFO,
  240. "Asynchronous notification not supported, "
  241. "hotplug won't\n work on fan-out "
  242. "ports. Use warm-plug instead.\n");
  243. }
  244. return 0;
  245. fail:
  246. ata_dev_printk(dev, KERN_ERR,
  247. "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
  248. reason, err_mask);
  249. return rc;
  250. }
  251. static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
  252. {
  253. struct ata_link *pmp_link = ap->pmp_link;
  254. int i;
  255. if (!pmp_link) {
  256. pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
  257. GFP_NOIO);
  258. if (!pmp_link)
  259. return -ENOMEM;
  260. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  261. ata_link_init(ap, &pmp_link[i], i);
  262. ap->pmp_link = pmp_link;
  263. }
  264. for (i = 0; i < nr_ports; i++) {
  265. struct ata_link *link = &pmp_link[i];
  266. struct ata_eh_context *ehc = &link->eh_context;
  267. link->flags = 0;
  268. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  269. ehc->i.action |= ATA_EH_RESET;
  270. }
  271. return 0;
  272. }
  273. static void sata_pmp_quirks(struct ata_port *ap)
  274. {
  275. u32 *gscr = ap->link.device->gscr;
  276. u16 vendor = sata_pmp_gscr_vendor(gscr);
  277. u16 devid = sata_pmp_gscr_devid(gscr);
  278. struct ata_link *link;
  279. if (vendor == 0x1095 && devid == 0x3726) {
  280. /* sil3726 quirks */
  281. ata_port_for_each_link(link, ap) {
  282. /* Class code report is unreliable and SRST
  283. * times out under certain configurations.
  284. */
  285. if (link->pmp < 5)
  286. link->flags |= ATA_LFLAG_NO_SRST |
  287. ATA_LFLAG_ASSUME_ATA;
  288. /* port 5 is for SEMB device and it doesn't like SRST */
  289. if (link->pmp == 5)
  290. link->flags |= ATA_LFLAG_NO_SRST |
  291. ATA_LFLAG_ASSUME_SEMB;
  292. }
  293. } else if (vendor == 0x1095 && devid == 0x4723) {
  294. /* sil4723 quirks */
  295. ata_port_for_each_link(link, ap) {
  296. /* class code report is unreliable */
  297. if (link->pmp < 2)
  298. link->flags |= ATA_LFLAG_ASSUME_ATA;
  299. /* the config device at port 2 locks up on SRST */
  300. if (link->pmp == 2)
  301. link->flags |= ATA_LFLAG_NO_SRST |
  302. ATA_LFLAG_ASSUME_ATA;
  303. }
  304. } else if (vendor == 0x1095 && devid == 0x4726) {
  305. /* sil4726 quirks */
  306. ata_port_for_each_link(link, ap) {
  307. /* Class code report is unreliable and SRST
  308. * times out under certain configurations.
  309. * Config device can be at port 0 or 5 and
  310. * locks up on SRST.
  311. */
  312. if (link->pmp <= 5)
  313. link->flags |= ATA_LFLAG_NO_SRST |
  314. ATA_LFLAG_ASSUME_ATA;
  315. /* Port 6 is for SEMB device which doesn't
  316. * like SRST either.
  317. */
  318. if (link->pmp == 6)
  319. link->flags |= ATA_LFLAG_NO_SRST |
  320. ATA_LFLAG_ASSUME_SEMB;
  321. }
  322. } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
  323. devid == 0x5734 || devid == 0x5744)) {
  324. /* sil5723/5744 quirks */
  325. /* sil5723/5744 has either two or three downstream
  326. * ports depending on operation mode. The last port
  327. * is empty if any actual IO device is available or
  328. * occupied by a pseudo configuration device
  329. * otherwise. Don't try hard to recover it.
  330. */
  331. ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
  332. }
  333. }
  334. /**
  335. * sata_pmp_attach - attach a SATA PMP device
  336. * @dev: SATA PMP device to attach
  337. *
  338. * Configure and attach SATA PMP device @dev. This function is
  339. * also responsible for allocating and initializing PMP links.
  340. *
  341. * LOCKING:
  342. * Kernel thread context (may sleep).
  343. *
  344. * RETURNS:
  345. * 0 on success, -errno on failure.
  346. */
  347. int sata_pmp_attach(struct ata_device *dev)
  348. {
  349. struct ata_link *link = dev->link;
  350. struct ata_port *ap = link->ap;
  351. unsigned long flags;
  352. struct ata_link *tlink;
  353. int rc;
  354. /* is it hanging off the right place? */
  355. if (!sata_pmp_supported(ap)) {
  356. ata_dev_printk(dev, KERN_ERR,
  357. "host does not support Port Multiplier\n");
  358. return -EINVAL;
  359. }
  360. if (!ata_is_host_link(link)) {
  361. ata_dev_printk(dev, KERN_ERR,
  362. "Port Multipliers cannot be nested\n");
  363. return -EINVAL;
  364. }
  365. if (dev->devno) {
  366. ata_dev_printk(dev, KERN_ERR,
  367. "Port Multiplier must be the first device\n");
  368. return -EINVAL;
  369. }
  370. WARN_ON(link->pmp != 0);
  371. link->pmp = SATA_PMP_CTRL_PORT;
  372. /* read GSCR block */
  373. rc = sata_pmp_read_gscr(dev, dev->gscr);
  374. if (rc)
  375. goto fail;
  376. /* config PMP */
  377. rc = sata_pmp_configure(dev, 1);
  378. if (rc)
  379. goto fail;
  380. rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
  381. if (rc) {
  382. ata_dev_printk(dev, KERN_INFO,
  383. "failed to initialize PMP links\n");
  384. goto fail;
  385. }
  386. /* attach it */
  387. spin_lock_irqsave(ap->lock, flags);
  388. WARN_ON(ap->nr_pmp_links);
  389. ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
  390. spin_unlock_irqrestore(ap->lock, flags);
  391. sata_pmp_quirks(ap);
  392. if (ap->ops->pmp_attach)
  393. ap->ops->pmp_attach(ap);
  394. ata_port_for_each_link(tlink, ap)
  395. sata_link_init_spd(tlink);
  396. ata_acpi_associate_sata_port(ap);
  397. return 0;
  398. fail:
  399. link->pmp = 0;
  400. return rc;
  401. }
  402. /**
  403. * sata_pmp_detach - detach a SATA PMP device
  404. * @dev: SATA PMP device to detach
  405. *
  406. * Detach SATA PMP device @dev. This function is also
  407. * responsible for deconfiguring PMP links.
  408. *
  409. * LOCKING:
  410. * Kernel thread context (may sleep).
  411. */
  412. static void sata_pmp_detach(struct ata_device *dev)
  413. {
  414. struct ata_link *link = dev->link;
  415. struct ata_port *ap = link->ap;
  416. struct ata_link *tlink;
  417. unsigned long flags;
  418. ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
  419. WARN_ON(!ata_is_host_link(link) || dev->devno ||
  420. link->pmp != SATA_PMP_CTRL_PORT);
  421. if (ap->ops->pmp_detach)
  422. ap->ops->pmp_detach(ap);
  423. ata_port_for_each_link(tlink, ap)
  424. ata_eh_detach_dev(tlink->device);
  425. spin_lock_irqsave(ap->lock, flags);
  426. ap->nr_pmp_links = 0;
  427. link->pmp = 0;
  428. spin_unlock_irqrestore(ap->lock, flags);
  429. ata_acpi_associate_sata_port(ap);
  430. }
  431. /**
  432. * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
  433. * @dev: PMP device to compare against
  434. * @new_gscr: GSCR block of the new device
  435. *
  436. * Compare @new_gscr against @dev and determine whether @dev is
  437. * the PMP described by @new_gscr.
  438. *
  439. * LOCKING:
  440. * None.
  441. *
  442. * RETURNS:
  443. * 1 if @dev matches @new_gscr, 0 otherwise.
  444. */
  445. static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
  446. {
  447. const u32 *old_gscr = dev->gscr;
  448. u16 old_vendor, new_vendor, old_devid, new_devid;
  449. int old_nr_ports, new_nr_ports;
  450. old_vendor = sata_pmp_gscr_vendor(old_gscr);
  451. new_vendor = sata_pmp_gscr_vendor(new_gscr);
  452. old_devid = sata_pmp_gscr_devid(old_gscr);
  453. new_devid = sata_pmp_gscr_devid(new_gscr);
  454. old_nr_ports = sata_pmp_gscr_ports(old_gscr);
  455. new_nr_ports = sata_pmp_gscr_ports(new_gscr);
  456. if (old_vendor != new_vendor) {
  457. ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
  458. "vendor mismatch '0x%x' != '0x%x'\n",
  459. old_vendor, new_vendor);
  460. return 0;
  461. }
  462. if (old_devid != new_devid) {
  463. ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
  464. "device ID mismatch '0x%x' != '0x%x'\n",
  465. old_devid, new_devid);
  466. return 0;
  467. }
  468. if (old_nr_ports != new_nr_ports) {
  469. ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
  470. "nr_ports mismatch '0x%x' != '0x%x'\n",
  471. old_nr_ports, new_nr_ports);
  472. return 0;
  473. }
  474. return 1;
  475. }
  476. /**
  477. * sata_pmp_revalidate - revalidate SATA PMP
  478. * @dev: PMP device to revalidate
  479. * @new_class: new class code
  480. *
  481. * Re-read GSCR block and make sure @dev is still attached to the
  482. * port and properly configured.
  483. *
  484. * LOCKING:
  485. * Kernel thread context (may sleep).
  486. *
  487. * RETURNS:
  488. * 0 on success, -errno otherwise.
  489. */
  490. static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
  491. {
  492. struct ata_link *link = dev->link;
  493. struct ata_port *ap = link->ap;
  494. u32 *gscr = (void *)ap->sector_buf;
  495. int rc;
  496. DPRINTK("ENTER\n");
  497. ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
  498. if (!ata_dev_enabled(dev)) {
  499. rc = -ENODEV;
  500. goto fail;
  501. }
  502. /* wrong class? */
  503. if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
  504. rc = -ENODEV;
  505. goto fail;
  506. }
  507. /* read GSCR */
  508. rc = sata_pmp_read_gscr(dev, gscr);
  509. if (rc)
  510. goto fail;
  511. /* is the pmp still there? */
  512. if (!sata_pmp_same_pmp(dev, gscr)) {
  513. rc = -ENODEV;
  514. goto fail;
  515. }
  516. memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
  517. rc = sata_pmp_configure(dev, 0);
  518. if (rc)
  519. goto fail;
  520. ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
  521. DPRINTK("EXIT, rc=0\n");
  522. return 0;
  523. fail:
  524. ata_dev_printk(dev, KERN_ERR,
  525. "PMP revalidation failed (errno=%d)\n", rc);
  526. DPRINTK("EXIT, rc=%d\n", rc);
  527. return rc;
  528. }
  529. /**
  530. * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
  531. * @dev: PMP device to revalidate
  532. *
  533. * Make sure the attached PMP is accessible.
  534. *
  535. * LOCKING:
  536. * Kernel thread context (may sleep).
  537. *
  538. * RETURNS:
  539. * 0 on success, -errno otherwise.
  540. */
  541. static int sata_pmp_revalidate_quick(struct ata_device *dev)
  542. {
  543. unsigned int err_mask;
  544. u32 prod_id;
  545. err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
  546. if (err_mask) {
  547. ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
  548. "(Emask=0x%x)\n", err_mask);
  549. return -EIO;
  550. }
  551. if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
  552. ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
  553. /* something weird is going on, request full PMP recovery */
  554. return -EIO;
  555. }
  556. return 0;
  557. }
  558. /**
  559. * sata_pmp_eh_recover_pmp - recover PMP
  560. * @ap: ATA port PMP is attached to
  561. * @prereset: prereset method (can be NULL)
  562. * @softreset: softreset method
  563. * @hardreset: hardreset method
  564. * @postreset: postreset method (can be NULL)
  565. *
  566. * Recover PMP attached to @ap. Recovery procedure is somewhat
  567. * similar to that of ata_eh_recover() except that reset should
  568. * always be performed in hard->soft sequence and recovery
  569. * failure results in PMP detachment.
  570. *
  571. * LOCKING:
  572. * Kernel thread context (may sleep).
  573. *
  574. * RETURNS:
  575. * 0 on success, -errno on failure.
  576. */
  577. static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
  578. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  579. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  580. {
  581. struct ata_link *link = &ap->link;
  582. struct ata_eh_context *ehc = &link->eh_context;
  583. struct ata_device *dev = link->device;
  584. int tries = ATA_EH_PMP_TRIES;
  585. int detach = 0, rc = 0;
  586. int reval_failed = 0;
  587. DPRINTK("ENTER\n");
  588. if (dev->flags & ATA_DFLAG_DETACH) {
  589. detach = 1;
  590. goto fail;
  591. }
  592. retry:
  593. ehc->classes[0] = ATA_DEV_UNKNOWN;
  594. if (ehc->i.action & ATA_EH_RESET) {
  595. struct ata_link *tlink;
  596. /* reset */
  597. rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
  598. postreset);
  599. if (rc) {
  600. ata_link_printk(link, KERN_ERR,
  601. "failed to reset PMP, giving up\n");
  602. goto fail;
  603. }
  604. /* PMP is reset, SErrors cannot be trusted, scan all */
  605. ata_port_for_each_link(tlink, ap) {
  606. struct ata_eh_context *ehc = &tlink->eh_context;
  607. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  608. ehc->i.action |= ATA_EH_RESET;
  609. }
  610. }
  611. /* If revalidation is requested, revalidate and reconfigure;
  612. * otherwise, do quick revalidation.
  613. */
  614. if (ehc->i.action & ATA_EH_REVALIDATE)
  615. rc = sata_pmp_revalidate(dev, ehc->classes[0]);
  616. else
  617. rc = sata_pmp_revalidate_quick(dev);
  618. if (rc) {
  619. tries--;
  620. if (rc == -ENODEV) {
  621. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  622. detach = 1;
  623. /* give it just two more chances */
  624. tries = min(tries, 2);
  625. }
  626. if (tries) {
  627. int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
  628. /* consecutive revalidation failures? speed down */
  629. if (reval_failed)
  630. sata_down_spd_limit(link);
  631. else
  632. reval_failed = 1;
  633. ata_dev_printk(dev, KERN_WARNING,
  634. "retrying reset%s\n",
  635. sleep ? " in 5 secs" : "");
  636. if (sleep)
  637. ssleep(5);
  638. ehc->i.action |= ATA_EH_RESET;
  639. goto retry;
  640. } else {
  641. ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
  642. "after %d tries, giving up\n",
  643. ATA_EH_PMP_TRIES);
  644. goto fail;
  645. }
  646. }
  647. /* okay, PMP resurrected */
  648. ehc->i.flags = 0;
  649. DPRINTK("EXIT, rc=0\n");
  650. return 0;
  651. fail:
  652. sata_pmp_detach(dev);
  653. if (detach)
  654. ata_eh_detach_dev(dev);
  655. else
  656. ata_dev_disable(dev);
  657. DPRINTK("EXIT, rc=%d\n", rc);
  658. return rc;
  659. }
  660. static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
  661. {
  662. struct ata_link *link;
  663. unsigned long flags;
  664. int rc;
  665. spin_lock_irqsave(ap->lock, flags);
  666. ata_port_for_each_link(link, ap) {
  667. if (!(link->flags & ATA_LFLAG_DISABLED))
  668. continue;
  669. spin_unlock_irqrestore(ap->lock, flags);
  670. /* Some PMPs require hardreset sequence to get
  671. * SError.N working.
  672. */
  673. sata_link_hardreset(link, sata_deb_timing_normal,
  674. jiffies + ATA_TMOUT_INTERNAL_QUICK, NULL, NULL);
  675. /* unconditionally clear SError.N */
  676. rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
  677. if (rc) {
  678. ata_link_printk(link, KERN_ERR, "failed to clear "
  679. "SError.N (errno=%d)\n", rc);
  680. return rc;
  681. }
  682. spin_lock_irqsave(ap->lock, flags);
  683. }
  684. spin_unlock_irqrestore(ap->lock, flags);
  685. return 0;
  686. }
  687. static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
  688. {
  689. struct ata_port *ap = link->ap;
  690. unsigned long flags;
  691. if (link_tries[link->pmp] && --link_tries[link->pmp])
  692. return 1;
  693. /* disable this link */
  694. if (!(link->flags & ATA_LFLAG_DISABLED)) {
  695. ata_link_printk(link, KERN_WARNING,
  696. "failed to recover link after %d tries, disabling\n",
  697. ATA_EH_PMP_LINK_TRIES);
  698. spin_lock_irqsave(ap->lock, flags);
  699. link->flags |= ATA_LFLAG_DISABLED;
  700. spin_unlock_irqrestore(ap->lock, flags);
  701. }
  702. ata_dev_disable(link->device);
  703. link->eh_context.i.action = 0;
  704. return 0;
  705. }
  706. /**
  707. * sata_pmp_eh_recover - recover PMP-enabled port
  708. * @ap: ATA port to recover
  709. *
  710. * Drive EH recovery operation for PMP enabled port @ap. This
  711. * function recovers host and PMP ports with proper retrials and
  712. * fallbacks. Actual recovery operations are performed using
  713. * ata_eh_recover() and sata_pmp_eh_recover_pmp().
  714. *
  715. * LOCKING:
  716. * Kernel thread context (may sleep).
  717. *
  718. * RETURNS:
  719. * 0 on success, -errno on failure.
  720. */
  721. static int sata_pmp_eh_recover(struct ata_port *ap)
  722. {
  723. struct ata_port_operations *ops = ap->ops;
  724. int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
  725. struct ata_link *pmp_link = &ap->link;
  726. struct ata_device *pmp_dev = pmp_link->device;
  727. struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
  728. u32 *gscr = pmp_dev->gscr;
  729. struct ata_link *link;
  730. struct ata_device *dev;
  731. unsigned int err_mask;
  732. u32 gscr_error, sntf;
  733. int cnt, rc;
  734. pmp_tries = ATA_EH_PMP_TRIES;
  735. ata_port_for_each_link(link, ap)
  736. link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
  737. retry:
  738. /* PMP attached? */
  739. if (!sata_pmp_attached(ap)) {
  740. rc = ata_eh_recover(ap, ops->prereset, ops->softreset,
  741. ops->hardreset, ops->postreset, NULL);
  742. if (rc) {
  743. ata_link_for_each_dev(dev, &ap->link)
  744. ata_dev_disable(dev);
  745. return rc;
  746. }
  747. if (pmp_dev->class != ATA_DEV_PMP)
  748. return 0;
  749. /* new PMP online */
  750. ata_port_for_each_link(link, ap)
  751. link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
  752. /* fall through */
  753. }
  754. /* recover pmp */
  755. rc = sata_pmp_eh_recover_pmp(ap, ops->prereset, ops->softreset,
  756. ops->hardreset, ops->postreset);
  757. if (rc)
  758. goto pmp_fail;
  759. /* PHY event notification can disturb reset and other recovery
  760. * operations. Turn it off.
  761. */
  762. if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
  763. gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
  764. err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
  765. gscr[SATA_PMP_GSCR_FEAT_EN]);
  766. if (err_mask) {
  767. ata_link_printk(pmp_link, KERN_WARNING,
  768. "failed to disable NOTIFY (err_mask=0x%x)\n",
  769. err_mask);
  770. goto pmp_fail;
  771. }
  772. }
  773. /* handle disabled links */
  774. rc = sata_pmp_eh_handle_disabled_links(ap);
  775. if (rc)
  776. goto pmp_fail;
  777. /* recover links */
  778. rc = ata_eh_recover(ap, ops->pmp_prereset, ops->pmp_softreset,
  779. ops->pmp_hardreset, ops->pmp_postreset, &link);
  780. if (rc)
  781. goto link_fail;
  782. /* Connection status might have changed while resetting other
  783. * links, check SATA_PMP_GSCR_ERROR before returning.
  784. */
  785. /* clear SNotification */
  786. rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
  787. if (rc == 0)
  788. sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
  789. /* enable notification */
  790. if (pmp_dev->flags & ATA_DFLAG_AN) {
  791. gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
  792. err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
  793. gscr[SATA_PMP_GSCR_FEAT_EN]);
  794. if (err_mask) {
  795. ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
  796. "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
  797. rc = -EIO;
  798. goto pmp_fail;
  799. }
  800. }
  801. /* check GSCR_ERROR */
  802. err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
  803. if (err_mask) {
  804. ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
  805. "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
  806. rc = -EIO;
  807. goto pmp_fail;
  808. }
  809. cnt = 0;
  810. ata_port_for_each_link(link, ap) {
  811. if (!(gscr_error & (1 << link->pmp)))
  812. continue;
  813. if (sata_pmp_handle_link_fail(link, link_tries)) {
  814. ata_ehi_hotplugged(&link->eh_context.i);
  815. cnt++;
  816. } else {
  817. ata_link_printk(link, KERN_WARNING,
  818. "PHY status changed but maxed out on retries, "
  819. "giving up\n");
  820. ata_link_printk(link, KERN_WARNING,
  821. "Manully issue scan to resume this link\n");
  822. }
  823. }
  824. if (cnt) {
  825. ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
  826. "ports, repeating recovery\n");
  827. goto retry;
  828. }
  829. return 0;
  830. link_fail:
  831. if (sata_pmp_handle_link_fail(link, link_tries)) {
  832. pmp_ehc->i.action |= ATA_EH_RESET;
  833. goto retry;
  834. }
  835. /* fall through */
  836. pmp_fail:
  837. /* Control always ends up here after detaching PMP. Shut up
  838. * and return if we're unloading.
  839. */
  840. if (ap->pflags & ATA_PFLAG_UNLOADING)
  841. return rc;
  842. if (!sata_pmp_attached(ap))
  843. goto retry;
  844. if (--pmp_tries) {
  845. ata_port_printk(ap, KERN_WARNING,
  846. "failed to recover PMP, retrying in 5 secs\n");
  847. pmp_ehc->i.action |= ATA_EH_RESET;
  848. ssleep(5);
  849. goto retry;
  850. }
  851. ata_port_printk(ap, KERN_ERR,
  852. "failed to recover PMP after %d tries, giving up\n",
  853. ATA_EH_PMP_TRIES);
  854. sata_pmp_detach(pmp_dev);
  855. ata_dev_disable(pmp_dev);
  856. return rc;
  857. }
  858. /**
  859. * sata_pmp_error_handler - do standard error handling for PMP-enabled host
  860. * @ap: host port to handle error for
  861. *
  862. * Perform standard error handling sequence for PMP-enabled host
  863. * @ap.
  864. *
  865. * LOCKING:
  866. * Kernel thread context (may sleep).
  867. */
  868. void sata_pmp_error_handler(struct ata_port *ap)
  869. {
  870. ata_eh_autopsy(ap);
  871. ata_eh_report(ap);
  872. sata_pmp_eh_recover(ap);
  873. ata_eh_finish(ap);
  874. }
  875. EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
  876. EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
  877. EXPORT_SYMBOL_GPL(sata_pmp_error_handler);