libata-pmp.c 24 KB

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