libata-pmp.c 28 KB

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