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