libata-pmp.c 28 KB

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