libata-pmp.c 25 KB

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