libata-pmp.c 26 KB

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