libata-pmp.c 26 KB

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