libata-pmp.c 24 KB

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