libata-pmp.c 28 KB

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