libata-pmp.c 26 KB

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