libata-pmp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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_SCR_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_SCR_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. /* turn off notification till fan-out ports are reset and configured */
  230. if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
  231. gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
  232. err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_FEAT_EN,
  233. gscr[SATA_PMP_GSCR_FEAT_EN]);
  234. if (err_mask) {
  235. rc = -EIO;
  236. reason = "failed to write GSCR_FEAT_EN";
  237. goto fail;
  238. }
  239. }
  240. if (print_info) {
  241. ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
  242. "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
  243. sata_pmp_spec_rev_str(gscr),
  244. sata_pmp_gscr_vendor(gscr),
  245. sata_pmp_gscr_devid(gscr),
  246. sata_pmp_gscr_rev(gscr),
  247. nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
  248. gscr[SATA_PMP_GSCR_FEAT]);
  249. if (!(dev->flags & ATA_DFLAG_AN))
  250. ata_dev_printk(dev, KERN_INFO,
  251. "Asynchronous notification not supported, "
  252. "hotplug won't\n work on fan-out "
  253. "ports. Use warm-plug instead.\n");
  254. }
  255. return 0;
  256. fail:
  257. ata_dev_printk(dev, KERN_ERR,
  258. "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
  259. reason, err_mask);
  260. return rc;
  261. }
  262. static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
  263. {
  264. struct ata_link *pmp_link = ap->pmp_link;
  265. int i;
  266. if (!pmp_link) {
  267. pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
  268. GFP_NOIO);
  269. if (!pmp_link)
  270. return -ENOMEM;
  271. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  272. ata_link_init(ap, &pmp_link[i], i);
  273. ap->pmp_link = pmp_link;
  274. }
  275. for (i = 0; i < nr_ports; i++) {
  276. struct ata_link *link = &pmp_link[i];
  277. struct ata_eh_context *ehc = &link->eh_context;
  278. link->flags = 0;
  279. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  280. ehc->i.action |= ATA_EH_RESET;
  281. }
  282. return 0;
  283. }
  284. static void sata_pmp_quirks(struct ata_port *ap)
  285. {
  286. u32 *gscr = ap->link.device->gscr;
  287. u16 vendor = sata_pmp_gscr_vendor(gscr);
  288. u16 devid = sata_pmp_gscr_devid(gscr);
  289. struct ata_link *link;
  290. if (vendor == 0x1095 && devid == 0x3726) {
  291. /* sil3726 quirks */
  292. ata_port_for_each_link(link, ap) {
  293. /* class code report is unreliable */
  294. if (link->pmp < 5)
  295. link->flags |= ATA_LFLAG_ASSUME_ATA;
  296. /* port 5 is for SEMB device and it doesn't like SRST */
  297. if (link->pmp == 5)
  298. link->flags |= ATA_LFLAG_NO_SRST |
  299. ATA_LFLAG_ASSUME_SEMB;
  300. }
  301. } else if (vendor == 0x1095 && devid == 0x4723) {
  302. /* sil4723 quirks */
  303. ata_port_for_each_link(link, ap) {
  304. /* class code report is unreliable */
  305. if (link->pmp < 2)
  306. link->flags |= ATA_LFLAG_ASSUME_ATA;
  307. /* the config device at port 2 locks up on SRST */
  308. if (link->pmp == 2)
  309. link->flags |= ATA_LFLAG_NO_SRST |
  310. ATA_LFLAG_ASSUME_ATA;
  311. }
  312. } else if (vendor == 0x1095 && devid == 0x4726) {
  313. /* sil4726 quirks */
  314. ata_port_for_each_link(link, ap) {
  315. /* Class code report is unreliable and SRST
  316. * times out under certain configurations.
  317. * Config device can be at port 0 or 5 and
  318. * locks up on SRST.
  319. */
  320. if (link->pmp <= 5)
  321. link->flags |= ATA_LFLAG_NO_SRST |
  322. ATA_LFLAG_ASSUME_ATA;
  323. /* Port 6 is for SEMB device which doesn't
  324. * like SRST either.
  325. */
  326. if (link->pmp == 6)
  327. link->flags |= ATA_LFLAG_NO_SRST |
  328. ATA_LFLAG_ASSUME_SEMB;
  329. }
  330. } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
  331. devid == 0x5734 || devid == 0x5744)) {
  332. /* sil5723/5744 quirks */
  333. /* sil5723/5744 has either two or three downstream
  334. * ports depending on operation mode. The last port
  335. * is empty if any actual IO device is available or
  336. * occupied by a pseudo configuration device
  337. * otherwise. Don't try hard to recover it.
  338. */
  339. ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
  340. }
  341. }
  342. /**
  343. * sata_pmp_attach - attach a SATA PMP device
  344. * @dev: SATA PMP device to attach
  345. *
  346. * Configure and attach SATA PMP device @dev. This function is
  347. * also responsible for allocating and initializing PMP links.
  348. *
  349. * LOCKING:
  350. * Kernel thread context (may sleep).
  351. *
  352. * RETURNS:
  353. * 0 on success, -errno on failure.
  354. */
  355. int sata_pmp_attach(struct ata_device *dev)
  356. {
  357. struct ata_link *link = dev->link;
  358. struct ata_port *ap = link->ap;
  359. unsigned long flags;
  360. struct ata_link *tlink;
  361. int rc;
  362. /* is it hanging off the right place? */
  363. if (!sata_pmp_supported(ap)) {
  364. ata_dev_printk(dev, KERN_ERR,
  365. "host does not support Port Multiplier\n");
  366. return -EINVAL;
  367. }
  368. if (!ata_is_host_link(link)) {
  369. ata_dev_printk(dev, KERN_ERR,
  370. "Port Multipliers cannot be nested\n");
  371. return -EINVAL;
  372. }
  373. if (dev->devno) {
  374. ata_dev_printk(dev, KERN_ERR,
  375. "Port Multiplier must be the first device\n");
  376. return -EINVAL;
  377. }
  378. WARN_ON(link->pmp != 0);
  379. link->pmp = SATA_PMP_CTRL_PORT;
  380. /* read GSCR block */
  381. rc = sata_pmp_read_gscr(dev, dev->gscr);
  382. if (rc)
  383. goto fail;
  384. /* config PMP */
  385. rc = sata_pmp_configure(dev, 1);
  386. if (rc)
  387. goto fail;
  388. rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
  389. if (rc) {
  390. ata_dev_printk(dev, KERN_INFO,
  391. "failed to initialize PMP links\n");
  392. goto fail;
  393. }
  394. /* attach it */
  395. spin_lock_irqsave(ap->lock, flags);
  396. WARN_ON(ap->nr_pmp_links);
  397. ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
  398. spin_unlock_irqrestore(ap->lock, flags);
  399. sata_pmp_quirks(ap);
  400. if (ap->ops->pmp_attach)
  401. ap->ops->pmp_attach(ap);
  402. ata_port_for_each_link(tlink, ap)
  403. sata_link_init_spd(tlink);
  404. ata_acpi_associate_sata_port(ap);
  405. return 0;
  406. fail:
  407. link->pmp = 0;
  408. return rc;
  409. }
  410. /**
  411. * sata_pmp_detach - detach a SATA PMP device
  412. * @dev: SATA PMP device to detach
  413. *
  414. * Detach SATA PMP device @dev. This function is also
  415. * responsible for deconfiguring PMP links.
  416. *
  417. * LOCKING:
  418. * Kernel thread context (may sleep).
  419. */
  420. static void sata_pmp_detach(struct ata_device *dev)
  421. {
  422. struct ata_link *link = dev->link;
  423. struct ata_port *ap = link->ap;
  424. struct ata_link *tlink;
  425. unsigned long flags;
  426. ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
  427. WARN_ON(!ata_is_host_link(link) || dev->devno ||
  428. link->pmp != SATA_PMP_CTRL_PORT);
  429. if (ap->ops->pmp_detach)
  430. ap->ops->pmp_detach(ap);
  431. ata_port_for_each_link(tlink, ap)
  432. ata_eh_detach_dev(tlink->device);
  433. spin_lock_irqsave(ap->lock, flags);
  434. ap->nr_pmp_links = 0;
  435. link->pmp = 0;
  436. spin_unlock_irqrestore(ap->lock, flags);
  437. ata_acpi_associate_sata_port(ap);
  438. }
  439. /**
  440. * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
  441. * @dev: PMP device to compare against
  442. * @new_gscr: GSCR block of the new device
  443. *
  444. * Compare @new_gscr against @dev and determine whether @dev is
  445. * the PMP described by @new_gscr.
  446. *
  447. * LOCKING:
  448. * None.
  449. *
  450. * RETURNS:
  451. * 1 if @dev matches @new_gscr, 0 otherwise.
  452. */
  453. static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
  454. {
  455. const u32 *old_gscr = dev->gscr;
  456. u16 old_vendor, new_vendor, old_devid, new_devid;
  457. int old_nr_ports, new_nr_ports;
  458. old_vendor = sata_pmp_gscr_vendor(old_gscr);
  459. new_vendor = sata_pmp_gscr_vendor(new_gscr);
  460. old_devid = sata_pmp_gscr_devid(old_gscr);
  461. new_devid = sata_pmp_gscr_devid(new_gscr);
  462. old_nr_ports = sata_pmp_gscr_ports(old_gscr);
  463. new_nr_ports = sata_pmp_gscr_ports(new_gscr);
  464. if (old_vendor != new_vendor) {
  465. ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
  466. "vendor mismatch '0x%x' != '0x%x'\n",
  467. old_vendor, new_vendor);
  468. return 0;
  469. }
  470. if (old_devid != new_devid) {
  471. ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
  472. "device ID mismatch '0x%x' != '0x%x'\n",
  473. old_devid, new_devid);
  474. return 0;
  475. }
  476. if (old_nr_ports != new_nr_ports) {
  477. ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
  478. "nr_ports mismatch '0x%x' != '0x%x'\n",
  479. old_nr_ports, new_nr_ports);
  480. return 0;
  481. }
  482. return 1;
  483. }
  484. /**
  485. * sata_pmp_revalidate - revalidate SATA PMP
  486. * @dev: PMP device to revalidate
  487. * @new_class: new class code
  488. *
  489. * Re-read GSCR block and make sure @dev is still attached to the
  490. * port and properly configured.
  491. *
  492. * LOCKING:
  493. * Kernel thread context (may sleep).
  494. *
  495. * RETURNS:
  496. * 0 on success, -errno otherwise.
  497. */
  498. static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
  499. {
  500. struct ata_link *link = dev->link;
  501. struct ata_port *ap = link->ap;
  502. u32 *gscr = (void *)ap->sector_buf;
  503. int rc;
  504. DPRINTK("ENTER\n");
  505. ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
  506. if (!ata_dev_enabled(dev)) {
  507. rc = -ENODEV;
  508. goto fail;
  509. }
  510. /* wrong class? */
  511. if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
  512. rc = -ENODEV;
  513. goto fail;
  514. }
  515. /* read GSCR */
  516. rc = sata_pmp_read_gscr(dev, gscr);
  517. if (rc)
  518. goto fail;
  519. /* is the pmp still there? */
  520. if (!sata_pmp_same_pmp(dev, gscr)) {
  521. rc = -ENODEV;
  522. goto fail;
  523. }
  524. memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
  525. rc = sata_pmp_configure(dev, 0);
  526. if (rc)
  527. goto fail;
  528. ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
  529. DPRINTK("EXIT, rc=0\n");
  530. return 0;
  531. fail:
  532. ata_dev_printk(dev, KERN_ERR,
  533. "PMP revalidation failed (errno=%d)\n", rc);
  534. DPRINTK("EXIT, rc=%d\n", rc);
  535. return rc;
  536. }
  537. /**
  538. * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
  539. * @dev: PMP device to revalidate
  540. *
  541. * Make sure the attached PMP is accessible.
  542. *
  543. * LOCKING:
  544. * Kernel thread context (may sleep).
  545. *
  546. * RETURNS:
  547. * 0 on success, -errno otherwise.
  548. */
  549. static int sata_pmp_revalidate_quick(struct ata_device *dev)
  550. {
  551. unsigned int err_mask;
  552. u32 prod_id;
  553. err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
  554. if (err_mask) {
  555. ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
  556. "(Emask=0x%x)\n", err_mask);
  557. return -EIO;
  558. }
  559. if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
  560. ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
  561. /* something weird is going on, request full PMP recovery */
  562. return -EIO;
  563. }
  564. return 0;
  565. }
  566. /**
  567. * sata_pmp_eh_recover_pmp - recover PMP
  568. * @ap: ATA port PMP is attached to
  569. * @prereset: prereset method (can be NULL)
  570. * @softreset: softreset method
  571. * @hardreset: hardreset method
  572. * @postreset: postreset method (can be NULL)
  573. *
  574. * Recover PMP attached to @ap. Recovery procedure is somewhat
  575. * similar to that of ata_eh_recover() except that reset should
  576. * always be performed in hard->soft sequence and recovery
  577. * failure results in PMP detachment.
  578. *
  579. * LOCKING:
  580. * Kernel thread context (may sleep).
  581. *
  582. * RETURNS:
  583. * 0 on success, -errno on failure.
  584. */
  585. static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
  586. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  587. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  588. {
  589. struct ata_link *link = &ap->link;
  590. struct ata_eh_context *ehc = &link->eh_context;
  591. struct ata_device *dev = link->device;
  592. int tries = ATA_EH_PMP_TRIES;
  593. int detach = 0, rc = 0;
  594. int reval_failed = 0;
  595. DPRINTK("ENTER\n");
  596. if (dev->flags & ATA_DFLAG_DETACH) {
  597. detach = 1;
  598. goto fail;
  599. }
  600. retry:
  601. ehc->classes[0] = ATA_DEV_UNKNOWN;
  602. if (ehc->i.action & ATA_EH_RESET) {
  603. struct ata_link *tlink;
  604. ata_eh_freeze_port(ap);
  605. /* reset */
  606. rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
  607. postreset);
  608. if (rc) {
  609. ata_link_printk(link, KERN_ERR,
  610. "failed to reset PMP, giving up\n");
  611. goto fail;
  612. }
  613. ata_eh_thaw_port(ap);
  614. /* PMP is reset, SErrors cannot be trusted, scan all */
  615. ata_port_for_each_link(tlink, ap) {
  616. struct ata_eh_context *ehc = &tlink->eh_context;
  617. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  618. ehc->i.action |= ATA_EH_RESET;
  619. }
  620. }
  621. /* If revalidation is requested, revalidate and reconfigure;
  622. * otherwise, do quick revalidation.
  623. */
  624. if (ehc->i.action & ATA_EH_REVALIDATE)
  625. rc = sata_pmp_revalidate(dev, ehc->classes[0]);
  626. else
  627. rc = sata_pmp_revalidate_quick(dev);
  628. if (rc) {
  629. tries--;
  630. if (rc == -ENODEV) {
  631. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  632. detach = 1;
  633. /* give it just two more chances */
  634. tries = min(tries, 2);
  635. }
  636. if (tries) {
  637. int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
  638. /* consecutive revalidation failures? speed down */
  639. if (reval_failed)
  640. sata_down_spd_limit(link);
  641. else
  642. reval_failed = 1;
  643. ata_dev_printk(dev, KERN_WARNING,
  644. "retrying reset%s\n",
  645. sleep ? " in 5 secs" : "");
  646. if (sleep)
  647. ssleep(5);
  648. ehc->i.action |= ATA_EH_RESET;
  649. goto retry;
  650. } else {
  651. ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
  652. "after %d tries, giving up\n",
  653. ATA_EH_PMP_TRIES);
  654. goto fail;
  655. }
  656. }
  657. /* okay, PMP resurrected */
  658. ehc->i.flags = 0;
  659. DPRINTK("EXIT, rc=0\n");
  660. return 0;
  661. fail:
  662. sata_pmp_detach(dev);
  663. if (detach)
  664. ata_eh_detach_dev(dev);
  665. else
  666. ata_dev_disable(dev);
  667. DPRINTK("EXIT, rc=%d\n", rc);
  668. return rc;
  669. }
  670. static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
  671. {
  672. struct ata_link *link;
  673. unsigned long flags;
  674. int rc;
  675. spin_lock_irqsave(ap->lock, flags);
  676. ata_port_for_each_link(link, ap) {
  677. if (!(link->flags & ATA_LFLAG_DISABLED))
  678. continue;
  679. spin_unlock_irqrestore(ap->lock, flags);
  680. /* Some PMPs require hardreset sequence to get
  681. * SError.N working.
  682. */
  683. sata_link_hardreset(link, sata_deb_timing_normal,
  684. jiffies + ATA_TMOUT_INTERNAL_QUICK, NULL, NULL);
  685. /* unconditionally clear SError.N */
  686. rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
  687. if (rc) {
  688. ata_link_printk(link, KERN_ERR, "failed to clear "
  689. "SError.N (errno=%d)\n", rc);
  690. return rc;
  691. }
  692. spin_lock_irqsave(ap->lock, flags);
  693. }
  694. spin_unlock_irqrestore(ap->lock, flags);
  695. return 0;
  696. }
  697. static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
  698. {
  699. struct ata_port *ap = link->ap;
  700. unsigned long flags;
  701. if (link_tries[link->pmp] && --link_tries[link->pmp])
  702. return 1;
  703. /* disable this link */
  704. if (!(link->flags & ATA_LFLAG_DISABLED)) {
  705. ata_link_printk(link, KERN_WARNING,
  706. "failed to recover link after %d tries, disabling\n",
  707. ATA_EH_PMP_LINK_TRIES);
  708. spin_lock_irqsave(ap->lock, flags);
  709. link->flags |= ATA_LFLAG_DISABLED;
  710. spin_unlock_irqrestore(ap->lock, flags);
  711. }
  712. ata_dev_disable(link->device);
  713. link->eh_context.i.action = 0;
  714. return 0;
  715. }
  716. /**
  717. * sata_pmp_eh_recover - recover PMP-enabled port
  718. * @ap: ATA port to recover
  719. *
  720. * Drive EH recovery operation for PMP enabled port @ap. This
  721. * function recovers host and PMP ports with proper retrials and
  722. * fallbacks. Actual recovery operations are performed using
  723. * ata_eh_recover() and sata_pmp_eh_recover_pmp().
  724. *
  725. * LOCKING:
  726. * Kernel thread context (may sleep).
  727. *
  728. * RETURNS:
  729. * 0 on success, -errno on failure.
  730. */
  731. static int sata_pmp_eh_recover(struct ata_port *ap)
  732. {
  733. struct ata_port_operations *ops = ap->ops;
  734. int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
  735. struct ata_link *pmp_link = &ap->link;
  736. struct ata_device *pmp_dev = pmp_link->device;
  737. struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
  738. struct ata_link *link;
  739. struct ata_device *dev;
  740. unsigned int err_mask;
  741. u32 gscr_error, sntf;
  742. int cnt, rc;
  743. pmp_tries = ATA_EH_PMP_TRIES;
  744. ata_port_for_each_link(link, ap)
  745. link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
  746. retry:
  747. /* PMP attached? */
  748. if (!sata_pmp_attached(ap)) {
  749. rc = ata_eh_recover(ap, ops->prereset, ops->softreset,
  750. ops->hardreset, ops->postreset, NULL);
  751. if (rc) {
  752. ata_link_for_each_dev(dev, &ap->link)
  753. ata_dev_disable(dev);
  754. return rc;
  755. }
  756. if (pmp_dev->class != ATA_DEV_PMP)
  757. return 0;
  758. /* new PMP online */
  759. ata_port_for_each_link(link, ap)
  760. link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
  761. /* fall through */
  762. }
  763. /* recover pmp */
  764. rc = sata_pmp_eh_recover_pmp(ap, ops->prereset, ops->softreset,
  765. ops->hardreset, ops->postreset);
  766. if (rc)
  767. goto pmp_fail;
  768. /* handle disabled links */
  769. rc = sata_pmp_eh_handle_disabled_links(ap);
  770. if (rc)
  771. goto pmp_fail;
  772. /* recover links */
  773. rc = ata_eh_recover(ap, ops->pmp_prereset, ops->pmp_softreset,
  774. ops->pmp_hardreset, ops->pmp_postreset, &link);
  775. if (rc)
  776. goto link_fail;
  777. /* Connection status might have changed while resetting other
  778. * links, check SATA_PMP_GSCR_ERROR before returning.
  779. */
  780. /* clear SNotification */
  781. rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
  782. if (rc == 0)
  783. sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
  784. /* enable notification */
  785. if (pmp_dev->flags & ATA_DFLAG_AN) {
  786. pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
  787. err_mask = sata_pmp_write(pmp_dev->link, SATA_PMP_GSCR_FEAT_EN,
  788. pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN]);
  789. if (err_mask) {
  790. ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
  791. "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
  792. rc = -EIO;
  793. goto pmp_fail;
  794. }
  795. }
  796. /* check GSCR_ERROR */
  797. err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
  798. if (err_mask) {
  799. ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
  800. "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
  801. rc = -EIO;
  802. goto pmp_fail;
  803. }
  804. cnt = 0;
  805. ata_port_for_each_link(link, ap) {
  806. if (!(gscr_error & (1 << link->pmp)))
  807. continue;
  808. if (sata_pmp_handle_link_fail(link, link_tries)) {
  809. ata_ehi_hotplugged(&link->eh_context.i);
  810. cnt++;
  811. } else {
  812. ata_link_printk(link, KERN_WARNING,
  813. "PHY status changed but maxed out on retries, "
  814. "giving up\n");
  815. ata_link_printk(link, KERN_WARNING,
  816. "Manully issue scan to resume this link\n");
  817. }
  818. }
  819. if (cnt) {
  820. ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
  821. "ports, repeating recovery\n");
  822. goto retry;
  823. }
  824. return 0;
  825. link_fail:
  826. if (sata_pmp_handle_link_fail(link, link_tries)) {
  827. pmp_ehc->i.action |= ATA_EH_RESET;
  828. goto retry;
  829. }
  830. /* fall through */
  831. pmp_fail:
  832. /* Control always ends up here after detaching PMP. Shut up
  833. * and return if we're unloading.
  834. */
  835. if (ap->pflags & ATA_PFLAG_UNLOADING)
  836. return rc;
  837. if (!sata_pmp_attached(ap))
  838. goto retry;
  839. if (--pmp_tries) {
  840. ata_port_printk(ap, KERN_WARNING,
  841. "failed to recover PMP, retrying in 5 secs\n");
  842. pmp_ehc->i.action |= ATA_EH_RESET;
  843. ssleep(5);
  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);