libata-pmp.c 24 KB

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