sas_expander.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Serial Attached SCSI (SAS) Expander discovery and configuration
  4. *
  5. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  6. * Copyright (C) 2005 Luben Tuikov <[email protected]>
  7. *
  8. * This file is licensed under GPLv2.
  9. */
  10. #include <linux/scatterlist.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/slab.h>
  13. #include <asm/unaligned.h>
  14. #include "sas_internal.h"
  15. #include <scsi/sas_ata.h>
  16. #include <scsi/scsi_transport.h>
  17. #include <scsi/scsi_transport_sas.h>
  18. #include "scsi_sas_internal.h"
  19. static int sas_discover_expander(struct domain_device *dev);
  20. static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
  21. static int sas_configure_phy(struct domain_device *dev, int phy_id,
  22. u8 *sas_addr, int include);
  23. static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
  24. /* ---------- SMP task management ---------- */
  25. /* Give it some long enough timeout. In seconds. */
  26. #define SMP_TIMEOUT 10
  27. static int smp_execute_task_sg(struct domain_device *dev,
  28. struct scatterlist *req, struct scatterlist *resp)
  29. {
  30. int res, retry;
  31. struct sas_task *task = NULL;
  32. struct sas_internal *i =
  33. to_sas_internal(dev->port->ha->core.shost->transportt);
  34. struct sas_ha_struct *ha = dev->port->ha;
  35. pm_runtime_get_sync(ha->dev);
  36. mutex_lock(&dev->ex_dev.cmd_mutex);
  37. for (retry = 0; retry < 3; retry++) {
  38. if (test_bit(SAS_DEV_GONE, &dev->state)) {
  39. res = -ECOMM;
  40. break;
  41. }
  42. task = sas_alloc_slow_task(GFP_KERNEL);
  43. if (!task) {
  44. res = -ENOMEM;
  45. break;
  46. }
  47. task->dev = dev;
  48. task->task_proto = dev->tproto;
  49. task->smp_task.smp_req = *req;
  50. task->smp_task.smp_resp = *resp;
  51. task->task_done = sas_task_internal_done;
  52. task->slow_task->timer.function = sas_task_internal_timedout;
  53. task->slow_task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
  54. add_timer(&task->slow_task->timer);
  55. res = i->dft->lldd_execute_task(task, GFP_KERNEL);
  56. if (res) {
  57. del_timer_sync(&task->slow_task->timer);
  58. pr_notice("executing SMP task failed:%d\n", res);
  59. break;
  60. }
  61. wait_for_completion(&task->slow_task->completion);
  62. res = -ECOMM;
  63. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  64. pr_notice("smp task timed out or aborted\n");
  65. i->dft->lldd_abort_task(task);
  66. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  67. pr_notice("SMP task aborted and not done\n");
  68. break;
  69. }
  70. }
  71. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  72. task->task_status.stat == SAS_SAM_STAT_GOOD) {
  73. res = 0;
  74. break;
  75. }
  76. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  77. task->task_status.stat == SAS_DATA_UNDERRUN) {
  78. /* no error, but return the number of bytes of
  79. * underrun */
  80. res = task->task_status.residual;
  81. break;
  82. }
  83. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  84. task->task_status.stat == SAS_DATA_OVERRUN) {
  85. res = -EMSGSIZE;
  86. break;
  87. }
  88. if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
  89. task->task_status.stat == SAS_DEVICE_UNKNOWN)
  90. break;
  91. else {
  92. pr_notice("%s: task to dev %016llx response: 0x%x status 0x%x\n",
  93. __func__,
  94. SAS_ADDR(dev->sas_addr),
  95. task->task_status.resp,
  96. task->task_status.stat);
  97. sas_free_task(task);
  98. task = NULL;
  99. }
  100. }
  101. mutex_unlock(&dev->ex_dev.cmd_mutex);
  102. pm_runtime_put_sync(ha->dev);
  103. BUG_ON(retry == 3 && task != NULL);
  104. sas_free_task(task);
  105. return res;
  106. }
  107. static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
  108. void *resp, int resp_size)
  109. {
  110. struct scatterlist req_sg;
  111. struct scatterlist resp_sg;
  112. sg_init_one(&req_sg, req, req_size);
  113. sg_init_one(&resp_sg, resp, resp_size);
  114. return smp_execute_task_sg(dev, &req_sg, &resp_sg);
  115. }
  116. /* ---------- Allocations ---------- */
  117. static inline void *alloc_smp_req(int size)
  118. {
  119. u8 *p = kzalloc(size, GFP_KERNEL);
  120. if (p)
  121. p[0] = SMP_REQUEST;
  122. return p;
  123. }
  124. static inline void *alloc_smp_resp(int size)
  125. {
  126. return kzalloc(size, GFP_KERNEL);
  127. }
  128. static char sas_route_char(struct domain_device *dev, struct ex_phy *phy)
  129. {
  130. switch (phy->routing_attr) {
  131. case TABLE_ROUTING:
  132. if (dev->ex_dev.t2t_supp)
  133. return 'U';
  134. else
  135. return 'T';
  136. case DIRECT_ROUTING:
  137. return 'D';
  138. case SUBTRACTIVE_ROUTING:
  139. return 'S';
  140. default:
  141. return '?';
  142. }
  143. }
  144. static enum sas_device_type to_dev_type(struct discover_resp *dr)
  145. {
  146. /* This is detecting a failure to transmit initial dev to host
  147. * FIS as described in section J.5 of sas-2 r16
  148. */
  149. if (dr->attached_dev_type == SAS_PHY_UNUSED && dr->attached_sata_dev &&
  150. dr->linkrate >= SAS_LINK_RATE_1_5_GBPS)
  151. return SAS_SATA_PENDING;
  152. else
  153. return dr->attached_dev_type;
  154. }
  155. static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
  156. struct smp_disc_resp *disc_resp)
  157. {
  158. enum sas_device_type dev_type;
  159. enum sas_linkrate linkrate;
  160. u8 sas_addr[SAS_ADDR_SIZE];
  161. struct discover_resp *dr = &disc_resp->disc;
  162. struct sas_ha_struct *ha = dev->port->ha;
  163. struct expander_device *ex = &dev->ex_dev;
  164. struct ex_phy *phy = &ex->ex_phy[phy_id];
  165. struct sas_rphy *rphy = dev->rphy;
  166. bool new_phy = !phy->phy;
  167. char *type;
  168. if (new_phy) {
  169. if (WARN_ON_ONCE(test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)))
  170. return;
  171. phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
  172. /* FIXME: error_handling */
  173. BUG_ON(!phy->phy);
  174. }
  175. switch (disc_resp->result) {
  176. case SMP_RESP_PHY_VACANT:
  177. phy->phy_state = PHY_VACANT;
  178. break;
  179. default:
  180. phy->phy_state = PHY_NOT_PRESENT;
  181. break;
  182. case SMP_RESP_FUNC_ACC:
  183. phy->phy_state = PHY_EMPTY; /* do not know yet */
  184. break;
  185. }
  186. /* check if anything important changed to squelch debug */
  187. dev_type = phy->attached_dev_type;
  188. linkrate = phy->linkrate;
  189. memcpy(sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  190. /* Handle vacant phy - rest of dr data is not valid so skip it */
  191. if (phy->phy_state == PHY_VACANT) {
  192. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  193. phy->attached_dev_type = SAS_PHY_UNUSED;
  194. if (!test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
  195. phy->phy_id = phy_id;
  196. goto skip;
  197. } else
  198. goto out;
  199. }
  200. phy->attached_dev_type = to_dev_type(dr);
  201. if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
  202. goto out;
  203. phy->phy_id = phy_id;
  204. phy->linkrate = dr->linkrate;
  205. phy->attached_sata_host = dr->attached_sata_host;
  206. phy->attached_sata_dev = dr->attached_sata_dev;
  207. phy->attached_sata_ps = dr->attached_sata_ps;
  208. phy->attached_iproto = dr->iproto << 1;
  209. phy->attached_tproto = dr->tproto << 1;
  210. /* help some expanders that fail to zero sas_address in the 'no
  211. * device' case
  212. */
  213. if (phy->attached_dev_type == SAS_PHY_UNUSED ||
  214. phy->linkrate < SAS_LINK_RATE_1_5_GBPS)
  215. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  216. else
  217. memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
  218. phy->attached_phy_id = dr->attached_phy_id;
  219. phy->phy_change_count = dr->change_count;
  220. phy->routing_attr = dr->routing_attr;
  221. phy->virtual = dr->virtual;
  222. phy->last_da_index = -1;
  223. phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
  224. phy->phy->identify.device_type = dr->attached_dev_type;
  225. phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
  226. phy->phy->identify.target_port_protocols = phy->attached_tproto;
  227. if (!phy->attached_tproto && dr->attached_sata_dev)
  228. phy->phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
  229. phy->phy->identify.phy_identifier = phy_id;
  230. phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
  231. phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
  232. phy->phy->minimum_linkrate = dr->pmin_linkrate;
  233. phy->phy->maximum_linkrate = dr->pmax_linkrate;
  234. phy->phy->negotiated_linkrate = phy->linkrate;
  235. phy->phy->enabled = (phy->linkrate != SAS_PHY_DISABLED);
  236. skip:
  237. if (new_phy)
  238. if (sas_phy_add(phy->phy)) {
  239. sas_phy_free(phy->phy);
  240. return;
  241. }
  242. out:
  243. switch (phy->attached_dev_type) {
  244. case SAS_SATA_PENDING:
  245. type = "stp pending";
  246. break;
  247. case SAS_PHY_UNUSED:
  248. type = "no device";
  249. break;
  250. case SAS_END_DEVICE:
  251. if (phy->attached_iproto) {
  252. if (phy->attached_tproto)
  253. type = "host+target";
  254. else
  255. type = "host";
  256. } else {
  257. if (dr->attached_sata_dev)
  258. type = "stp";
  259. else
  260. type = "ssp";
  261. }
  262. break;
  263. case SAS_EDGE_EXPANDER_DEVICE:
  264. case SAS_FANOUT_EXPANDER_DEVICE:
  265. type = "smp";
  266. break;
  267. default:
  268. type = "unknown";
  269. }
  270. /* this routine is polled by libata error recovery so filter
  271. * unimportant messages
  272. */
  273. if (new_phy || phy->attached_dev_type != dev_type ||
  274. phy->linkrate != linkrate ||
  275. SAS_ADDR(phy->attached_sas_addr) != SAS_ADDR(sas_addr))
  276. /* pass */;
  277. else
  278. return;
  279. /* if the attached device type changed and ata_eh is active,
  280. * make sure we run revalidation when eh completes (see:
  281. * sas_enable_revalidation)
  282. */
  283. if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
  284. set_bit(DISCE_REVALIDATE_DOMAIN, &dev->port->disc.pending);
  285. pr_debug("%sex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
  286. test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state) ? "ata: " : "",
  287. SAS_ADDR(dev->sas_addr), phy->phy_id,
  288. sas_route_char(dev, phy), phy->linkrate,
  289. SAS_ADDR(phy->attached_sas_addr), type);
  290. }
  291. /* check if we have an existing attached ata device on this expander phy */
  292. struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
  293. {
  294. struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
  295. struct domain_device *dev;
  296. struct sas_rphy *rphy;
  297. if (!ex_phy->port)
  298. return NULL;
  299. rphy = ex_phy->port->rphy;
  300. if (!rphy)
  301. return NULL;
  302. dev = sas_find_dev_by_rphy(rphy);
  303. if (dev && dev_is_sata(dev))
  304. return dev;
  305. return NULL;
  306. }
  307. #define DISCOVER_REQ_SIZE 16
  308. #define DISCOVER_RESP_SIZE sizeof(struct smp_disc_resp)
  309. static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
  310. struct smp_disc_resp *disc_resp,
  311. int single)
  312. {
  313. struct discover_resp *dr = &disc_resp->disc;
  314. int res;
  315. disc_req[9] = single;
  316. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  317. disc_resp, DISCOVER_RESP_SIZE);
  318. if (res)
  319. return res;
  320. if (memcmp(dev->sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE) == 0) {
  321. pr_notice("Found loopback topology, just ignore it!\n");
  322. return 0;
  323. }
  324. sas_set_ex_phy(dev, single, disc_resp);
  325. return 0;
  326. }
  327. int sas_ex_phy_discover(struct domain_device *dev, int single)
  328. {
  329. struct expander_device *ex = &dev->ex_dev;
  330. int res = 0;
  331. u8 *disc_req;
  332. struct smp_disc_resp *disc_resp;
  333. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  334. if (!disc_req)
  335. return -ENOMEM;
  336. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  337. if (!disc_resp) {
  338. kfree(disc_req);
  339. return -ENOMEM;
  340. }
  341. disc_req[1] = SMP_DISCOVER;
  342. if (0 <= single && single < ex->num_phys) {
  343. res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
  344. } else {
  345. int i;
  346. for (i = 0; i < ex->num_phys; i++) {
  347. res = sas_ex_phy_discover_helper(dev, disc_req,
  348. disc_resp, i);
  349. if (res)
  350. goto out_err;
  351. }
  352. }
  353. out_err:
  354. kfree(disc_resp);
  355. kfree(disc_req);
  356. return res;
  357. }
  358. static int sas_expander_discover(struct domain_device *dev)
  359. {
  360. struct expander_device *ex = &dev->ex_dev;
  361. int res;
  362. ex->ex_phy = kcalloc(ex->num_phys, sizeof(*ex->ex_phy), GFP_KERNEL);
  363. if (!ex->ex_phy)
  364. return -ENOMEM;
  365. res = sas_ex_phy_discover(dev, -1);
  366. if (res)
  367. goto out_err;
  368. return 0;
  369. out_err:
  370. kfree(ex->ex_phy);
  371. ex->ex_phy = NULL;
  372. return res;
  373. }
  374. #define MAX_EXPANDER_PHYS 128
  375. #define RG_REQ_SIZE 8
  376. #define RG_RESP_SIZE sizeof(struct smp_rg_resp)
  377. static int sas_ex_general(struct domain_device *dev)
  378. {
  379. u8 *rg_req;
  380. struct smp_rg_resp *rg_resp;
  381. struct report_general_resp *rg;
  382. int res;
  383. int i;
  384. rg_req = alloc_smp_req(RG_REQ_SIZE);
  385. if (!rg_req)
  386. return -ENOMEM;
  387. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  388. if (!rg_resp) {
  389. kfree(rg_req);
  390. return -ENOMEM;
  391. }
  392. rg_req[1] = SMP_REPORT_GENERAL;
  393. for (i = 0; i < 5; i++) {
  394. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  395. RG_RESP_SIZE);
  396. if (res) {
  397. pr_notice("RG to ex %016llx failed:0x%x\n",
  398. SAS_ADDR(dev->sas_addr), res);
  399. goto out;
  400. } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  401. pr_debug("RG:ex %016llx returned SMP result:0x%x\n",
  402. SAS_ADDR(dev->sas_addr), rg_resp->result);
  403. res = rg_resp->result;
  404. goto out;
  405. }
  406. rg = &rg_resp->rg;
  407. dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
  408. dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
  409. dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
  410. dev->ex_dev.t2t_supp = rg->t2t_supp;
  411. dev->ex_dev.conf_route_table = rg->conf_route_table;
  412. dev->ex_dev.configuring = rg->configuring;
  413. memcpy(dev->ex_dev.enclosure_logical_id,
  414. rg->enclosure_logical_id, 8);
  415. if (dev->ex_dev.configuring) {
  416. pr_debug("RG: ex %016llx self-configuring...\n",
  417. SAS_ADDR(dev->sas_addr));
  418. schedule_timeout_interruptible(5*HZ);
  419. } else
  420. break;
  421. }
  422. out:
  423. kfree(rg_req);
  424. kfree(rg_resp);
  425. return res;
  426. }
  427. static void ex_assign_manuf_info(struct domain_device *dev, void
  428. *_mi_resp)
  429. {
  430. u8 *mi_resp = _mi_resp;
  431. struct sas_rphy *rphy = dev->rphy;
  432. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  433. memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
  434. memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
  435. memcpy(edev->product_rev, mi_resp + 36,
  436. SAS_EXPANDER_PRODUCT_REV_LEN);
  437. if (mi_resp[8] & 1) {
  438. memcpy(edev->component_vendor_id, mi_resp + 40,
  439. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  440. edev->component_id = mi_resp[48] << 8 | mi_resp[49];
  441. edev->component_revision_id = mi_resp[50];
  442. }
  443. }
  444. #define MI_REQ_SIZE 8
  445. #define MI_RESP_SIZE 64
  446. static int sas_ex_manuf_info(struct domain_device *dev)
  447. {
  448. u8 *mi_req;
  449. u8 *mi_resp;
  450. int res;
  451. mi_req = alloc_smp_req(MI_REQ_SIZE);
  452. if (!mi_req)
  453. return -ENOMEM;
  454. mi_resp = alloc_smp_resp(MI_RESP_SIZE);
  455. if (!mi_resp) {
  456. kfree(mi_req);
  457. return -ENOMEM;
  458. }
  459. mi_req[1] = SMP_REPORT_MANUF_INFO;
  460. res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp, MI_RESP_SIZE);
  461. if (res) {
  462. pr_notice("MI: ex %016llx failed:0x%x\n",
  463. SAS_ADDR(dev->sas_addr), res);
  464. goto out;
  465. } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
  466. pr_debug("MI ex %016llx returned SMP result:0x%x\n",
  467. SAS_ADDR(dev->sas_addr), mi_resp[2]);
  468. goto out;
  469. }
  470. ex_assign_manuf_info(dev, mi_resp);
  471. out:
  472. kfree(mi_req);
  473. kfree(mi_resp);
  474. return res;
  475. }
  476. #define PC_REQ_SIZE 44
  477. #define PC_RESP_SIZE 8
  478. int sas_smp_phy_control(struct domain_device *dev, int phy_id,
  479. enum phy_func phy_func,
  480. struct sas_phy_linkrates *rates)
  481. {
  482. u8 *pc_req;
  483. u8 *pc_resp;
  484. int res;
  485. pc_req = alloc_smp_req(PC_REQ_SIZE);
  486. if (!pc_req)
  487. return -ENOMEM;
  488. pc_resp = alloc_smp_resp(PC_RESP_SIZE);
  489. if (!pc_resp) {
  490. kfree(pc_req);
  491. return -ENOMEM;
  492. }
  493. pc_req[1] = SMP_PHY_CONTROL;
  494. pc_req[9] = phy_id;
  495. pc_req[10] = phy_func;
  496. if (rates) {
  497. pc_req[32] = rates->minimum_linkrate << 4;
  498. pc_req[33] = rates->maximum_linkrate << 4;
  499. }
  500. res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp, PC_RESP_SIZE);
  501. if (res) {
  502. pr_err("ex %016llx phy%02d PHY control failed: %d\n",
  503. SAS_ADDR(dev->sas_addr), phy_id, res);
  504. } else if (pc_resp[2] != SMP_RESP_FUNC_ACC) {
  505. pr_err("ex %016llx phy%02d PHY control failed: function result 0x%x\n",
  506. SAS_ADDR(dev->sas_addr), phy_id, pc_resp[2]);
  507. res = pc_resp[2];
  508. }
  509. kfree(pc_resp);
  510. kfree(pc_req);
  511. return res;
  512. }
  513. static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
  514. {
  515. struct expander_device *ex = &dev->ex_dev;
  516. struct ex_phy *phy = &ex->ex_phy[phy_id];
  517. sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
  518. phy->linkrate = SAS_PHY_DISABLED;
  519. }
  520. static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
  521. {
  522. struct expander_device *ex = &dev->ex_dev;
  523. int i;
  524. for (i = 0; i < ex->num_phys; i++) {
  525. struct ex_phy *phy = &ex->ex_phy[i];
  526. if (phy->phy_state == PHY_VACANT ||
  527. phy->phy_state == PHY_NOT_PRESENT)
  528. continue;
  529. if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
  530. sas_ex_disable_phy(dev, i);
  531. }
  532. }
  533. static int sas_dev_present_in_domain(struct asd_sas_port *port,
  534. u8 *sas_addr)
  535. {
  536. struct domain_device *dev;
  537. if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
  538. return 1;
  539. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  540. if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
  541. return 1;
  542. }
  543. return 0;
  544. }
  545. #define RPEL_REQ_SIZE 16
  546. #define RPEL_RESP_SIZE 32
  547. int sas_smp_get_phy_events(struct sas_phy *phy)
  548. {
  549. int res;
  550. u8 *req;
  551. u8 *resp;
  552. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  553. struct domain_device *dev = sas_find_dev_by_rphy(rphy);
  554. req = alloc_smp_req(RPEL_REQ_SIZE);
  555. if (!req)
  556. return -ENOMEM;
  557. resp = alloc_smp_resp(RPEL_RESP_SIZE);
  558. if (!resp) {
  559. kfree(req);
  560. return -ENOMEM;
  561. }
  562. req[1] = SMP_REPORT_PHY_ERR_LOG;
  563. req[9] = phy->number;
  564. res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
  565. resp, RPEL_RESP_SIZE);
  566. if (res)
  567. goto out;
  568. phy->invalid_dword_count = get_unaligned_be32(&resp[12]);
  569. phy->running_disparity_error_count = get_unaligned_be32(&resp[16]);
  570. phy->loss_of_dword_sync_count = get_unaligned_be32(&resp[20]);
  571. phy->phy_reset_problem_count = get_unaligned_be32(&resp[24]);
  572. out:
  573. kfree(req);
  574. kfree(resp);
  575. return res;
  576. }
  577. #ifdef CONFIG_SCSI_SAS_ATA
  578. #define RPS_REQ_SIZE 16
  579. #define RPS_RESP_SIZE sizeof(struct smp_rps_resp)
  580. int sas_get_report_phy_sata(struct domain_device *dev, int phy_id,
  581. struct smp_rps_resp *rps_resp)
  582. {
  583. int res;
  584. u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
  585. u8 *resp = (u8 *)rps_resp;
  586. if (!rps_req)
  587. return -ENOMEM;
  588. rps_req[1] = SMP_REPORT_PHY_SATA;
  589. rps_req[9] = phy_id;
  590. res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
  591. rps_resp, RPS_RESP_SIZE);
  592. /* 0x34 is the FIS type for the D2H fis. There's a potential
  593. * standards cockup here. sas-2 explicitly specifies the FIS
  594. * should be encoded so that FIS type is in resp[24].
  595. * However, some expanders endian reverse this. Undo the
  596. * reversal here */
  597. if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
  598. int i;
  599. for (i = 0; i < 5; i++) {
  600. int j = 24 + (i*4);
  601. u8 a, b;
  602. a = resp[j + 0];
  603. b = resp[j + 1];
  604. resp[j + 0] = resp[j + 3];
  605. resp[j + 1] = resp[j + 2];
  606. resp[j + 2] = b;
  607. resp[j + 3] = a;
  608. }
  609. }
  610. kfree(rps_req);
  611. return res;
  612. }
  613. #endif
  614. static void sas_ex_get_linkrate(struct domain_device *parent,
  615. struct domain_device *child,
  616. struct ex_phy *parent_phy)
  617. {
  618. struct expander_device *parent_ex = &parent->ex_dev;
  619. struct sas_port *port;
  620. int i;
  621. child->pathways = 0;
  622. port = parent_phy->port;
  623. for (i = 0; i < parent_ex->num_phys; i++) {
  624. struct ex_phy *phy = &parent_ex->ex_phy[i];
  625. if (phy->phy_state == PHY_VACANT ||
  626. phy->phy_state == PHY_NOT_PRESENT)
  627. continue;
  628. if (SAS_ADDR(phy->attached_sas_addr) ==
  629. SAS_ADDR(child->sas_addr)) {
  630. child->min_linkrate = min(parent->min_linkrate,
  631. phy->linkrate);
  632. child->max_linkrate = max(parent->max_linkrate,
  633. phy->linkrate);
  634. child->pathways++;
  635. sas_port_add_phy(port, phy->phy);
  636. }
  637. }
  638. child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
  639. child->pathways = min(child->pathways, parent->pathways);
  640. }
  641. static struct domain_device *sas_ex_discover_end_dev(
  642. struct domain_device *parent, int phy_id)
  643. {
  644. struct expander_device *parent_ex = &parent->ex_dev;
  645. struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
  646. struct domain_device *child = NULL;
  647. struct sas_rphy *rphy;
  648. int res;
  649. if (phy->attached_sata_host || phy->attached_sata_ps)
  650. return NULL;
  651. child = sas_alloc_device();
  652. if (!child)
  653. return NULL;
  654. kref_get(&parent->kref);
  655. child->parent = parent;
  656. child->port = parent->port;
  657. child->iproto = phy->attached_iproto;
  658. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  659. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  660. if (!phy->port) {
  661. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  662. if (unlikely(!phy->port))
  663. goto out_err;
  664. if (unlikely(sas_port_add(phy->port) != 0)) {
  665. sas_port_free(phy->port);
  666. goto out_err;
  667. }
  668. }
  669. sas_ex_get_linkrate(parent, child, phy);
  670. sas_device_set_phy(child, phy->port);
  671. #ifdef CONFIG_SCSI_SAS_ATA
  672. if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
  673. if (child->linkrate > parent->min_linkrate) {
  674. struct sas_phy *cphy = child->phy;
  675. enum sas_linkrate min_prate = cphy->minimum_linkrate,
  676. parent_min_lrate = parent->min_linkrate,
  677. min_linkrate = (min_prate > parent_min_lrate) ?
  678. parent_min_lrate : 0;
  679. struct sas_phy_linkrates rates = {
  680. .maximum_linkrate = parent->min_linkrate,
  681. .minimum_linkrate = min_linkrate,
  682. };
  683. int ret;
  684. pr_notice("ex %016llx phy%02d SATA device linkrate > min pathway connection rate, attempting to lower device linkrate\n",
  685. SAS_ADDR(child->sas_addr), phy_id);
  686. ret = sas_smp_phy_control(parent, phy_id,
  687. PHY_FUNC_LINK_RESET, &rates);
  688. if (ret) {
  689. pr_err("ex %016llx phy%02d SATA device could not set linkrate (%d)\n",
  690. SAS_ADDR(child->sas_addr), phy_id, ret);
  691. goto out_free;
  692. }
  693. pr_notice("ex %016llx phy%02d SATA device set linkrate successfully\n",
  694. SAS_ADDR(child->sas_addr), phy_id);
  695. child->linkrate = child->min_linkrate;
  696. }
  697. res = sas_get_ata_info(child, phy);
  698. if (res)
  699. goto out_free;
  700. sas_init_dev(child);
  701. res = sas_ata_init(child);
  702. if (res)
  703. goto out_free;
  704. rphy = sas_end_device_alloc(phy->port);
  705. if (!rphy)
  706. goto out_free;
  707. rphy->identify.phy_identifier = phy_id;
  708. child->rphy = rphy;
  709. get_device(&rphy->dev);
  710. list_add_tail(&child->disco_list_node, &parent->port->disco_list);
  711. res = sas_discover_sata(child);
  712. if (res) {
  713. pr_notice("sas_discover_sata() for device %16llx at %016llx:%02d returned 0x%x\n",
  714. SAS_ADDR(child->sas_addr),
  715. SAS_ADDR(parent->sas_addr), phy_id, res);
  716. goto out_list_del;
  717. }
  718. } else
  719. #endif
  720. if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
  721. child->dev_type = SAS_END_DEVICE;
  722. rphy = sas_end_device_alloc(phy->port);
  723. /* FIXME: error handling */
  724. if (unlikely(!rphy))
  725. goto out_free;
  726. child->tproto = phy->attached_tproto;
  727. sas_init_dev(child);
  728. child->rphy = rphy;
  729. get_device(&rphy->dev);
  730. rphy->identify.phy_identifier = phy_id;
  731. sas_fill_in_rphy(child, rphy);
  732. list_add_tail(&child->disco_list_node, &parent->port->disco_list);
  733. res = sas_discover_end_dev(child);
  734. if (res) {
  735. pr_notice("sas_discover_end_dev() for device %016llx at %016llx:%02d returned 0x%x\n",
  736. SAS_ADDR(child->sas_addr),
  737. SAS_ADDR(parent->sas_addr), phy_id, res);
  738. goto out_list_del;
  739. }
  740. } else {
  741. pr_notice("target proto 0x%x at %016llx:0x%x not handled\n",
  742. phy->attached_tproto, SAS_ADDR(parent->sas_addr),
  743. phy_id);
  744. goto out_free;
  745. }
  746. list_add_tail(&child->siblings, &parent_ex->children);
  747. return child;
  748. out_list_del:
  749. sas_rphy_free(child->rphy);
  750. list_del(&child->disco_list_node);
  751. spin_lock_irq(&parent->port->dev_list_lock);
  752. list_del(&child->dev_list_node);
  753. spin_unlock_irq(&parent->port->dev_list_lock);
  754. out_free:
  755. sas_port_delete(phy->port);
  756. out_err:
  757. phy->port = NULL;
  758. sas_put_device(child);
  759. return NULL;
  760. }
  761. /* See if this phy is part of a wide port */
  762. static bool sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
  763. {
  764. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  765. int i;
  766. for (i = 0; i < parent->ex_dev.num_phys; i++) {
  767. struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
  768. if (ephy == phy)
  769. continue;
  770. if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
  771. SAS_ADDR_SIZE) && ephy->port) {
  772. sas_port_add_phy(ephy->port, phy->phy);
  773. phy->port = ephy->port;
  774. phy->phy_state = PHY_DEVICE_DISCOVERED;
  775. return true;
  776. }
  777. }
  778. return false;
  779. }
  780. static struct domain_device *sas_ex_discover_expander(
  781. struct domain_device *parent, int phy_id)
  782. {
  783. struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
  784. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  785. struct domain_device *child = NULL;
  786. struct sas_rphy *rphy;
  787. struct sas_expander_device *edev;
  788. struct asd_sas_port *port;
  789. int res;
  790. if (phy->routing_attr == DIRECT_ROUTING) {
  791. pr_warn("ex %016llx:%02d:D <--> ex %016llx:0x%x is not allowed\n",
  792. SAS_ADDR(parent->sas_addr), phy_id,
  793. SAS_ADDR(phy->attached_sas_addr),
  794. phy->attached_phy_id);
  795. return NULL;
  796. }
  797. child = sas_alloc_device();
  798. if (!child)
  799. return NULL;
  800. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  801. /* FIXME: better error handling */
  802. BUG_ON(sas_port_add(phy->port) != 0);
  803. switch (phy->attached_dev_type) {
  804. case SAS_EDGE_EXPANDER_DEVICE:
  805. rphy = sas_expander_alloc(phy->port,
  806. SAS_EDGE_EXPANDER_DEVICE);
  807. break;
  808. case SAS_FANOUT_EXPANDER_DEVICE:
  809. rphy = sas_expander_alloc(phy->port,
  810. SAS_FANOUT_EXPANDER_DEVICE);
  811. break;
  812. default:
  813. rphy = NULL; /* shut gcc up */
  814. BUG();
  815. }
  816. port = parent->port;
  817. child->rphy = rphy;
  818. get_device(&rphy->dev);
  819. edev = rphy_to_expander_device(rphy);
  820. child->dev_type = phy->attached_dev_type;
  821. kref_get(&parent->kref);
  822. child->parent = parent;
  823. child->port = port;
  824. child->iproto = phy->attached_iproto;
  825. child->tproto = phy->attached_tproto;
  826. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  827. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  828. sas_ex_get_linkrate(parent, child, phy);
  829. edev->level = parent_ex->level + 1;
  830. parent->port->disc.max_level = max(parent->port->disc.max_level,
  831. edev->level);
  832. sas_init_dev(child);
  833. sas_fill_in_rphy(child, rphy);
  834. sas_rphy_add(rphy);
  835. spin_lock_irq(&parent->port->dev_list_lock);
  836. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  837. spin_unlock_irq(&parent->port->dev_list_lock);
  838. res = sas_discover_expander(child);
  839. if (res) {
  840. sas_rphy_delete(rphy);
  841. spin_lock_irq(&parent->port->dev_list_lock);
  842. list_del(&child->dev_list_node);
  843. spin_unlock_irq(&parent->port->dev_list_lock);
  844. sas_put_device(child);
  845. sas_port_delete(phy->port);
  846. phy->port = NULL;
  847. return NULL;
  848. }
  849. list_add_tail(&child->siblings, &parent->ex_dev.children);
  850. return child;
  851. }
  852. static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
  853. {
  854. struct expander_device *ex = &dev->ex_dev;
  855. struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
  856. struct domain_device *child = NULL;
  857. int res = 0;
  858. /* Phy state */
  859. if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
  860. if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
  861. res = sas_ex_phy_discover(dev, phy_id);
  862. if (res)
  863. return res;
  864. }
  865. /* Parent and domain coherency */
  866. if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  867. SAS_ADDR(dev->port->sas_addr))) {
  868. sas_add_parent_port(dev, phy_id);
  869. return 0;
  870. }
  871. if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  872. SAS_ADDR(dev->parent->sas_addr))) {
  873. sas_add_parent_port(dev, phy_id);
  874. if (ex_phy->routing_attr == TABLE_ROUTING)
  875. sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
  876. return 0;
  877. }
  878. if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
  879. sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
  880. if (ex_phy->attached_dev_type == SAS_PHY_UNUSED) {
  881. if (ex_phy->routing_attr == DIRECT_ROUTING) {
  882. memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  883. sas_configure_routing(dev, ex_phy->attached_sas_addr);
  884. }
  885. return 0;
  886. } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
  887. return 0;
  888. if (ex_phy->attached_dev_type != SAS_END_DEVICE &&
  889. ex_phy->attached_dev_type != SAS_FANOUT_EXPANDER_DEVICE &&
  890. ex_phy->attached_dev_type != SAS_EDGE_EXPANDER_DEVICE &&
  891. ex_phy->attached_dev_type != SAS_SATA_PENDING) {
  892. pr_warn("unknown device type(0x%x) attached to ex %016llx phy%02d\n",
  893. ex_phy->attached_dev_type,
  894. SAS_ADDR(dev->sas_addr),
  895. phy_id);
  896. return 0;
  897. }
  898. res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
  899. if (res) {
  900. pr_notice("configure routing for dev %016llx reported 0x%x. Forgotten\n",
  901. SAS_ADDR(ex_phy->attached_sas_addr), res);
  902. sas_disable_routing(dev, ex_phy->attached_sas_addr);
  903. return res;
  904. }
  905. if (sas_ex_join_wide_port(dev, phy_id)) {
  906. pr_debug("Attaching ex phy%02d to wide port %016llx\n",
  907. phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
  908. return res;
  909. }
  910. switch (ex_phy->attached_dev_type) {
  911. case SAS_END_DEVICE:
  912. case SAS_SATA_PENDING:
  913. child = sas_ex_discover_end_dev(dev, phy_id);
  914. break;
  915. case SAS_FANOUT_EXPANDER_DEVICE:
  916. if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
  917. pr_debug("second fanout expander %016llx phy%02d attached to ex %016llx phy%02d\n",
  918. SAS_ADDR(ex_phy->attached_sas_addr),
  919. ex_phy->attached_phy_id,
  920. SAS_ADDR(dev->sas_addr),
  921. phy_id);
  922. sas_ex_disable_phy(dev, phy_id);
  923. return res;
  924. } else
  925. memcpy(dev->port->disc.fanout_sas_addr,
  926. ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
  927. fallthrough;
  928. case SAS_EDGE_EXPANDER_DEVICE:
  929. child = sas_ex_discover_expander(dev, phy_id);
  930. break;
  931. default:
  932. break;
  933. }
  934. if (!child)
  935. pr_notice("ex %016llx phy%02d failed to discover\n",
  936. SAS_ADDR(dev->sas_addr), phy_id);
  937. return res;
  938. }
  939. static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
  940. {
  941. struct expander_device *ex = &dev->ex_dev;
  942. int i;
  943. for (i = 0; i < ex->num_phys; i++) {
  944. struct ex_phy *phy = &ex->ex_phy[i];
  945. if (phy->phy_state == PHY_VACANT ||
  946. phy->phy_state == PHY_NOT_PRESENT)
  947. continue;
  948. if (dev_is_expander(phy->attached_dev_type) &&
  949. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  950. memcpy(sub_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  951. return 1;
  952. }
  953. }
  954. return 0;
  955. }
  956. static int sas_check_level_subtractive_boundary(struct domain_device *dev)
  957. {
  958. struct expander_device *ex = &dev->ex_dev;
  959. struct domain_device *child;
  960. u8 sub_addr[SAS_ADDR_SIZE] = {0, };
  961. list_for_each_entry(child, &ex->children, siblings) {
  962. if (!dev_is_expander(child->dev_type))
  963. continue;
  964. if (sub_addr[0] == 0) {
  965. sas_find_sub_addr(child, sub_addr);
  966. continue;
  967. } else {
  968. u8 s2[SAS_ADDR_SIZE];
  969. if (sas_find_sub_addr(child, s2) &&
  970. (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
  971. pr_notice("ex %016llx->%016llx-?->%016llx diverges from subtractive boundary %016llx\n",
  972. SAS_ADDR(dev->sas_addr),
  973. SAS_ADDR(child->sas_addr),
  974. SAS_ADDR(s2),
  975. SAS_ADDR(sub_addr));
  976. sas_ex_disable_port(child, s2);
  977. }
  978. }
  979. }
  980. return 0;
  981. }
  982. /**
  983. * sas_ex_discover_devices - discover devices attached to this expander
  984. * @dev: pointer to the expander domain device
  985. * @single: if you want to do a single phy, else set to -1;
  986. *
  987. * Configure this expander for use with its devices and register the
  988. * devices of this expander.
  989. */
  990. static int sas_ex_discover_devices(struct domain_device *dev, int single)
  991. {
  992. struct expander_device *ex = &dev->ex_dev;
  993. int i = 0, end = ex->num_phys;
  994. int res = 0;
  995. if (0 <= single && single < end) {
  996. i = single;
  997. end = i+1;
  998. }
  999. for ( ; i < end; i++) {
  1000. struct ex_phy *ex_phy = &ex->ex_phy[i];
  1001. if (ex_phy->phy_state == PHY_VACANT ||
  1002. ex_phy->phy_state == PHY_NOT_PRESENT ||
  1003. ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
  1004. continue;
  1005. switch (ex_phy->linkrate) {
  1006. case SAS_PHY_DISABLED:
  1007. case SAS_PHY_RESET_PROBLEM:
  1008. case SAS_SATA_PORT_SELECTOR:
  1009. continue;
  1010. default:
  1011. res = sas_ex_discover_dev(dev, i);
  1012. if (res)
  1013. break;
  1014. continue;
  1015. }
  1016. }
  1017. if (!res)
  1018. sas_check_level_subtractive_boundary(dev);
  1019. return res;
  1020. }
  1021. static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
  1022. {
  1023. struct expander_device *ex = &dev->ex_dev;
  1024. int i;
  1025. u8 *sub_sas_addr = NULL;
  1026. if (dev->dev_type != SAS_EDGE_EXPANDER_DEVICE)
  1027. return 0;
  1028. for (i = 0; i < ex->num_phys; i++) {
  1029. struct ex_phy *phy = &ex->ex_phy[i];
  1030. if (phy->phy_state == PHY_VACANT ||
  1031. phy->phy_state == PHY_NOT_PRESENT)
  1032. continue;
  1033. if (dev_is_expander(phy->attached_dev_type) &&
  1034. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1035. if (!sub_sas_addr)
  1036. sub_sas_addr = &phy->attached_sas_addr[0];
  1037. else if (SAS_ADDR(sub_sas_addr) !=
  1038. SAS_ADDR(phy->attached_sas_addr)) {
  1039. pr_notice("ex %016llx phy%02d diverges(%016llx) on subtractive boundary(%016llx). Disabled\n",
  1040. SAS_ADDR(dev->sas_addr), i,
  1041. SAS_ADDR(phy->attached_sas_addr),
  1042. SAS_ADDR(sub_sas_addr));
  1043. sas_ex_disable_phy(dev, i);
  1044. }
  1045. }
  1046. }
  1047. return 0;
  1048. }
  1049. static void sas_print_parent_topology_bug(struct domain_device *child,
  1050. struct ex_phy *parent_phy,
  1051. struct ex_phy *child_phy)
  1052. {
  1053. static const char *ex_type[] = {
  1054. [SAS_EDGE_EXPANDER_DEVICE] = "edge",
  1055. [SAS_FANOUT_EXPANDER_DEVICE] = "fanout",
  1056. };
  1057. struct domain_device *parent = child->parent;
  1058. pr_notice("%s ex %016llx phy%02d <--> %s ex %016llx phy%02d has %c:%c routing link!\n",
  1059. ex_type[parent->dev_type],
  1060. SAS_ADDR(parent->sas_addr),
  1061. parent_phy->phy_id,
  1062. ex_type[child->dev_type],
  1063. SAS_ADDR(child->sas_addr),
  1064. child_phy->phy_id,
  1065. sas_route_char(parent, parent_phy),
  1066. sas_route_char(child, child_phy));
  1067. }
  1068. static int sas_check_eeds(struct domain_device *child,
  1069. struct ex_phy *parent_phy,
  1070. struct ex_phy *child_phy)
  1071. {
  1072. int res = 0;
  1073. struct domain_device *parent = child->parent;
  1074. if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
  1075. res = -ENODEV;
  1076. pr_warn("edge ex %016llx phy S:%02d <--> edge ex %016llx phy S:%02d, while there is a fanout ex %016llx\n",
  1077. SAS_ADDR(parent->sas_addr),
  1078. parent_phy->phy_id,
  1079. SAS_ADDR(child->sas_addr),
  1080. child_phy->phy_id,
  1081. SAS_ADDR(parent->port->disc.fanout_sas_addr));
  1082. } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
  1083. memcpy(parent->port->disc.eeds_a, parent->sas_addr,
  1084. SAS_ADDR_SIZE);
  1085. memcpy(parent->port->disc.eeds_b, child->sas_addr,
  1086. SAS_ADDR_SIZE);
  1087. } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
  1088. SAS_ADDR(parent->sas_addr)) ||
  1089. (SAS_ADDR(parent->port->disc.eeds_a) ==
  1090. SAS_ADDR(child->sas_addr)))
  1091. &&
  1092. ((SAS_ADDR(parent->port->disc.eeds_b) ==
  1093. SAS_ADDR(parent->sas_addr)) ||
  1094. (SAS_ADDR(parent->port->disc.eeds_b) ==
  1095. SAS_ADDR(child->sas_addr))))
  1096. ;
  1097. else {
  1098. res = -ENODEV;
  1099. pr_warn("edge ex %016llx phy%02d <--> edge ex %016llx phy%02d link forms a third EEDS!\n",
  1100. SAS_ADDR(parent->sas_addr),
  1101. parent_phy->phy_id,
  1102. SAS_ADDR(child->sas_addr),
  1103. child_phy->phy_id);
  1104. }
  1105. return res;
  1106. }
  1107. /* Here we spill over 80 columns. It is intentional.
  1108. */
  1109. static int sas_check_parent_topology(struct domain_device *child)
  1110. {
  1111. struct expander_device *child_ex = &child->ex_dev;
  1112. struct expander_device *parent_ex;
  1113. int i;
  1114. int res = 0;
  1115. if (!child->parent)
  1116. return 0;
  1117. if (!dev_is_expander(child->parent->dev_type))
  1118. return 0;
  1119. parent_ex = &child->parent->ex_dev;
  1120. for (i = 0; i < parent_ex->num_phys; i++) {
  1121. struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
  1122. struct ex_phy *child_phy;
  1123. if (parent_phy->phy_state == PHY_VACANT ||
  1124. parent_phy->phy_state == PHY_NOT_PRESENT)
  1125. continue;
  1126. if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
  1127. continue;
  1128. child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
  1129. switch (child->parent->dev_type) {
  1130. case SAS_EDGE_EXPANDER_DEVICE:
  1131. if (child->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
  1132. if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
  1133. child_phy->routing_attr != TABLE_ROUTING) {
  1134. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1135. res = -ENODEV;
  1136. }
  1137. } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1138. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1139. res = sas_check_eeds(child, parent_phy, child_phy);
  1140. } else if (child_phy->routing_attr != TABLE_ROUTING) {
  1141. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1142. res = -ENODEV;
  1143. }
  1144. } else if (parent_phy->routing_attr == TABLE_ROUTING) {
  1145. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
  1146. (child_phy->routing_attr == TABLE_ROUTING &&
  1147. child_ex->t2t_supp && parent_ex->t2t_supp)) {
  1148. /* All good */;
  1149. } else {
  1150. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1151. res = -ENODEV;
  1152. }
  1153. }
  1154. break;
  1155. case SAS_FANOUT_EXPANDER_DEVICE:
  1156. if (parent_phy->routing_attr != TABLE_ROUTING ||
  1157. child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
  1158. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1159. res = -ENODEV;
  1160. }
  1161. break;
  1162. default:
  1163. break;
  1164. }
  1165. }
  1166. return res;
  1167. }
  1168. #define RRI_REQ_SIZE 16
  1169. #define RRI_RESP_SIZE 44
  1170. static int sas_configure_present(struct domain_device *dev, int phy_id,
  1171. u8 *sas_addr, int *index, int *present)
  1172. {
  1173. int i, res = 0;
  1174. struct expander_device *ex = &dev->ex_dev;
  1175. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1176. u8 *rri_req;
  1177. u8 *rri_resp;
  1178. *present = 0;
  1179. *index = 0;
  1180. rri_req = alloc_smp_req(RRI_REQ_SIZE);
  1181. if (!rri_req)
  1182. return -ENOMEM;
  1183. rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
  1184. if (!rri_resp) {
  1185. kfree(rri_req);
  1186. return -ENOMEM;
  1187. }
  1188. rri_req[1] = SMP_REPORT_ROUTE_INFO;
  1189. rri_req[9] = phy_id;
  1190. for (i = 0; i < ex->max_route_indexes ; i++) {
  1191. *(__be16 *)(rri_req+6) = cpu_to_be16(i);
  1192. res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
  1193. RRI_RESP_SIZE);
  1194. if (res)
  1195. goto out;
  1196. res = rri_resp[2];
  1197. if (res == SMP_RESP_NO_INDEX) {
  1198. pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
  1199. SAS_ADDR(dev->sas_addr), phy_id, i);
  1200. goto out;
  1201. } else if (res != SMP_RESP_FUNC_ACC) {
  1202. pr_notice("%s: dev %016llx phy%02d index 0x%x result 0x%x\n",
  1203. __func__, SAS_ADDR(dev->sas_addr), phy_id,
  1204. i, res);
  1205. goto out;
  1206. }
  1207. if (SAS_ADDR(sas_addr) != 0) {
  1208. if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
  1209. *index = i;
  1210. if ((rri_resp[12] & 0x80) == 0x80)
  1211. *present = 0;
  1212. else
  1213. *present = 1;
  1214. goto out;
  1215. } else if (SAS_ADDR(rri_resp+16) == 0) {
  1216. *index = i;
  1217. *present = 0;
  1218. goto out;
  1219. }
  1220. } else if (SAS_ADDR(rri_resp+16) == 0 &&
  1221. phy->last_da_index < i) {
  1222. phy->last_da_index = i;
  1223. *index = i;
  1224. *present = 0;
  1225. goto out;
  1226. }
  1227. }
  1228. res = -1;
  1229. out:
  1230. kfree(rri_req);
  1231. kfree(rri_resp);
  1232. return res;
  1233. }
  1234. #define CRI_REQ_SIZE 44
  1235. #define CRI_RESP_SIZE 8
  1236. static int sas_configure_set(struct domain_device *dev, int phy_id,
  1237. u8 *sas_addr, int index, int include)
  1238. {
  1239. int res;
  1240. u8 *cri_req;
  1241. u8 *cri_resp;
  1242. cri_req = alloc_smp_req(CRI_REQ_SIZE);
  1243. if (!cri_req)
  1244. return -ENOMEM;
  1245. cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
  1246. if (!cri_resp) {
  1247. kfree(cri_req);
  1248. return -ENOMEM;
  1249. }
  1250. cri_req[1] = SMP_CONF_ROUTE_INFO;
  1251. *(__be16 *)(cri_req+6) = cpu_to_be16(index);
  1252. cri_req[9] = phy_id;
  1253. if (SAS_ADDR(sas_addr) == 0 || !include)
  1254. cri_req[12] |= 0x80;
  1255. memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
  1256. res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
  1257. CRI_RESP_SIZE);
  1258. if (res)
  1259. goto out;
  1260. res = cri_resp[2];
  1261. if (res == SMP_RESP_NO_INDEX) {
  1262. pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
  1263. SAS_ADDR(dev->sas_addr), phy_id, index);
  1264. }
  1265. out:
  1266. kfree(cri_req);
  1267. kfree(cri_resp);
  1268. return res;
  1269. }
  1270. static int sas_configure_phy(struct domain_device *dev, int phy_id,
  1271. u8 *sas_addr, int include)
  1272. {
  1273. int index;
  1274. int present;
  1275. int res;
  1276. res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
  1277. if (res)
  1278. return res;
  1279. if (include ^ present)
  1280. return sas_configure_set(dev, phy_id, sas_addr, index,
  1281. include);
  1282. return res;
  1283. }
  1284. /**
  1285. * sas_configure_parent - configure routing table of parent
  1286. * @parent: parent expander
  1287. * @child: child expander
  1288. * @sas_addr: SAS port identifier of device directly attached to child
  1289. * @include: whether or not to include @child in the expander routing table
  1290. */
  1291. static int sas_configure_parent(struct domain_device *parent,
  1292. struct domain_device *child,
  1293. u8 *sas_addr, int include)
  1294. {
  1295. struct expander_device *ex_parent = &parent->ex_dev;
  1296. int res = 0;
  1297. int i;
  1298. if (parent->parent) {
  1299. res = sas_configure_parent(parent->parent, parent, sas_addr,
  1300. include);
  1301. if (res)
  1302. return res;
  1303. }
  1304. if (ex_parent->conf_route_table == 0) {
  1305. pr_debug("ex %016llx has self-configuring routing table\n",
  1306. SAS_ADDR(parent->sas_addr));
  1307. return 0;
  1308. }
  1309. for (i = 0; i < ex_parent->num_phys; i++) {
  1310. struct ex_phy *phy = &ex_parent->ex_phy[i];
  1311. if ((phy->routing_attr == TABLE_ROUTING) &&
  1312. (SAS_ADDR(phy->attached_sas_addr) ==
  1313. SAS_ADDR(child->sas_addr))) {
  1314. res = sas_configure_phy(parent, i, sas_addr, include);
  1315. if (res)
  1316. return res;
  1317. }
  1318. }
  1319. return res;
  1320. }
  1321. /**
  1322. * sas_configure_routing - configure routing
  1323. * @dev: expander device
  1324. * @sas_addr: port identifier of device directly attached to the expander device
  1325. */
  1326. static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
  1327. {
  1328. if (dev->parent)
  1329. return sas_configure_parent(dev->parent, dev, sas_addr, 1);
  1330. return 0;
  1331. }
  1332. static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
  1333. {
  1334. if (dev->parent)
  1335. return sas_configure_parent(dev->parent, dev, sas_addr, 0);
  1336. return 0;
  1337. }
  1338. /**
  1339. * sas_discover_expander - expander discovery
  1340. * @dev: pointer to expander domain device
  1341. *
  1342. * See comment in sas_discover_sata().
  1343. */
  1344. static int sas_discover_expander(struct domain_device *dev)
  1345. {
  1346. int res;
  1347. res = sas_notify_lldd_dev_found(dev);
  1348. if (res)
  1349. return res;
  1350. res = sas_ex_general(dev);
  1351. if (res)
  1352. goto out_err;
  1353. res = sas_ex_manuf_info(dev);
  1354. if (res)
  1355. goto out_err;
  1356. res = sas_expander_discover(dev);
  1357. if (res) {
  1358. pr_warn("expander %016llx discovery failed(0x%x)\n",
  1359. SAS_ADDR(dev->sas_addr), res);
  1360. goto out_err;
  1361. }
  1362. sas_check_ex_subtractive_boundary(dev);
  1363. res = sas_check_parent_topology(dev);
  1364. if (res)
  1365. goto out_err;
  1366. return 0;
  1367. out_err:
  1368. sas_notify_lldd_dev_gone(dev);
  1369. return res;
  1370. }
  1371. static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
  1372. {
  1373. int res = 0;
  1374. struct domain_device *dev;
  1375. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  1376. if (dev_is_expander(dev->dev_type)) {
  1377. struct sas_expander_device *ex =
  1378. rphy_to_expander_device(dev->rphy);
  1379. if (level == ex->level)
  1380. res = sas_ex_discover_devices(dev, -1);
  1381. else if (level > 0)
  1382. res = sas_ex_discover_devices(port->port_dev, -1);
  1383. }
  1384. }
  1385. return res;
  1386. }
  1387. static int sas_ex_bfs_disc(struct asd_sas_port *port)
  1388. {
  1389. int res;
  1390. int level;
  1391. do {
  1392. level = port->disc.max_level;
  1393. res = sas_ex_level_discovery(port, level);
  1394. mb();
  1395. } while (level < port->disc.max_level);
  1396. return res;
  1397. }
  1398. int sas_discover_root_expander(struct domain_device *dev)
  1399. {
  1400. int res;
  1401. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1402. res = sas_rphy_add(dev->rphy);
  1403. if (res)
  1404. goto out_err;
  1405. ex->level = dev->port->disc.max_level; /* 0 */
  1406. res = sas_discover_expander(dev);
  1407. if (res)
  1408. goto out_err2;
  1409. sas_ex_bfs_disc(dev->port);
  1410. return res;
  1411. out_err2:
  1412. sas_rphy_remove(dev->rphy);
  1413. out_err:
  1414. return res;
  1415. }
  1416. /* ---------- Domain revalidation ---------- */
  1417. static int sas_get_phy_discover(struct domain_device *dev,
  1418. int phy_id, struct smp_disc_resp *disc_resp)
  1419. {
  1420. int res;
  1421. u8 *disc_req;
  1422. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  1423. if (!disc_req)
  1424. return -ENOMEM;
  1425. disc_req[1] = SMP_DISCOVER;
  1426. disc_req[9] = phy_id;
  1427. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  1428. disc_resp, DISCOVER_RESP_SIZE);
  1429. if (res)
  1430. goto out;
  1431. if (disc_resp->result != SMP_RESP_FUNC_ACC)
  1432. res = disc_resp->result;
  1433. out:
  1434. kfree(disc_req);
  1435. return res;
  1436. }
  1437. static int sas_get_phy_change_count(struct domain_device *dev,
  1438. int phy_id, int *pcc)
  1439. {
  1440. int res;
  1441. struct smp_disc_resp *disc_resp;
  1442. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1443. if (!disc_resp)
  1444. return -ENOMEM;
  1445. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1446. if (!res)
  1447. *pcc = disc_resp->disc.change_count;
  1448. kfree(disc_resp);
  1449. return res;
  1450. }
  1451. int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
  1452. u8 *sas_addr, enum sas_device_type *type)
  1453. {
  1454. int res;
  1455. struct smp_disc_resp *disc_resp;
  1456. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1457. if (!disc_resp)
  1458. return -ENOMEM;
  1459. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1460. if (res == 0) {
  1461. memcpy(sas_addr, disc_resp->disc.attached_sas_addr,
  1462. SAS_ADDR_SIZE);
  1463. *type = to_dev_type(&disc_resp->disc);
  1464. if (*type == 0)
  1465. memset(sas_addr, 0, SAS_ADDR_SIZE);
  1466. }
  1467. kfree(disc_resp);
  1468. return res;
  1469. }
  1470. static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
  1471. int from_phy, bool update)
  1472. {
  1473. struct expander_device *ex = &dev->ex_dev;
  1474. int res = 0;
  1475. int i;
  1476. for (i = from_phy; i < ex->num_phys; i++) {
  1477. int phy_change_count = 0;
  1478. res = sas_get_phy_change_count(dev, i, &phy_change_count);
  1479. switch (res) {
  1480. case SMP_RESP_PHY_VACANT:
  1481. case SMP_RESP_NO_PHY:
  1482. continue;
  1483. case SMP_RESP_FUNC_ACC:
  1484. break;
  1485. default:
  1486. return res;
  1487. }
  1488. if (phy_change_count != ex->ex_phy[i].phy_change_count) {
  1489. if (update)
  1490. ex->ex_phy[i].phy_change_count =
  1491. phy_change_count;
  1492. *phy_id = i;
  1493. return 0;
  1494. }
  1495. }
  1496. return 0;
  1497. }
  1498. static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
  1499. {
  1500. int res;
  1501. u8 *rg_req;
  1502. struct smp_rg_resp *rg_resp;
  1503. rg_req = alloc_smp_req(RG_REQ_SIZE);
  1504. if (!rg_req)
  1505. return -ENOMEM;
  1506. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  1507. if (!rg_resp) {
  1508. kfree(rg_req);
  1509. return -ENOMEM;
  1510. }
  1511. rg_req[1] = SMP_REPORT_GENERAL;
  1512. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  1513. RG_RESP_SIZE);
  1514. if (res)
  1515. goto out;
  1516. if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  1517. res = rg_resp->result;
  1518. goto out;
  1519. }
  1520. *ecc = be16_to_cpu(rg_resp->rg.change_count);
  1521. out:
  1522. kfree(rg_resp);
  1523. kfree(rg_req);
  1524. return res;
  1525. }
  1526. /**
  1527. * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
  1528. * @dev:domain device to be detect.
  1529. * @src_dev: the device which originated BROADCAST(CHANGE).
  1530. *
  1531. * Add self-configuration expander support. Suppose two expander cascading,
  1532. * when the first level expander is self-configuring, hotplug the disks in
  1533. * second level expander, BROADCAST(CHANGE) will not only be originated
  1534. * in the second level expander, but also be originated in the first level
  1535. * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
  1536. * expander changed count in two level expanders will all increment at least
  1537. * once, but the phy which chang count has changed is the source device which
  1538. * we concerned.
  1539. */
  1540. static int sas_find_bcast_dev(struct domain_device *dev,
  1541. struct domain_device **src_dev)
  1542. {
  1543. struct expander_device *ex = &dev->ex_dev;
  1544. int ex_change_count = -1;
  1545. int phy_id = -1;
  1546. int res;
  1547. struct domain_device *ch;
  1548. res = sas_get_ex_change_count(dev, &ex_change_count);
  1549. if (res)
  1550. goto out;
  1551. if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
  1552. /* Just detect if this expander phys phy change count changed,
  1553. * in order to determine if this expander originate BROADCAST,
  1554. * and do not update phy change count field in our structure.
  1555. */
  1556. res = sas_find_bcast_phy(dev, &phy_id, 0, false);
  1557. if (phy_id != -1) {
  1558. *src_dev = dev;
  1559. ex->ex_change_count = ex_change_count;
  1560. pr_info("ex %016llx phy%02d change count has changed\n",
  1561. SAS_ADDR(dev->sas_addr), phy_id);
  1562. return res;
  1563. } else
  1564. pr_info("ex %016llx phys DID NOT change\n",
  1565. SAS_ADDR(dev->sas_addr));
  1566. }
  1567. list_for_each_entry(ch, &ex->children, siblings) {
  1568. if (dev_is_expander(ch->dev_type)) {
  1569. res = sas_find_bcast_dev(ch, src_dev);
  1570. if (*src_dev)
  1571. return res;
  1572. }
  1573. }
  1574. out:
  1575. return res;
  1576. }
  1577. static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
  1578. {
  1579. struct expander_device *ex = &dev->ex_dev;
  1580. struct domain_device *child, *n;
  1581. list_for_each_entry_safe(child, n, &ex->children, siblings) {
  1582. set_bit(SAS_DEV_GONE, &child->state);
  1583. if (dev_is_expander(child->dev_type))
  1584. sas_unregister_ex_tree(port, child);
  1585. else
  1586. sas_unregister_dev(port, child);
  1587. }
  1588. sas_unregister_dev(port, dev);
  1589. }
  1590. static void sas_unregister_devs_sas_addr(struct domain_device *parent,
  1591. int phy_id, bool last)
  1592. {
  1593. struct expander_device *ex_dev = &parent->ex_dev;
  1594. struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
  1595. struct domain_device *child, *n, *found = NULL;
  1596. if (last) {
  1597. list_for_each_entry_safe(child, n,
  1598. &ex_dev->children, siblings) {
  1599. if (SAS_ADDR(child->sas_addr) ==
  1600. SAS_ADDR(phy->attached_sas_addr)) {
  1601. set_bit(SAS_DEV_GONE, &child->state);
  1602. if (dev_is_expander(child->dev_type))
  1603. sas_unregister_ex_tree(parent->port, child);
  1604. else
  1605. sas_unregister_dev(parent->port, child);
  1606. found = child;
  1607. break;
  1608. }
  1609. }
  1610. sas_disable_routing(parent, phy->attached_sas_addr);
  1611. }
  1612. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  1613. if (phy->port) {
  1614. sas_port_delete_phy(phy->port, phy->phy);
  1615. sas_device_set_phy(found, phy->port);
  1616. if (phy->port->num_phys == 0)
  1617. list_add_tail(&phy->port->del_list,
  1618. &parent->port->sas_port_del_list);
  1619. phy->port = NULL;
  1620. }
  1621. }
  1622. static int sas_discover_bfs_by_root_level(struct domain_device *root,
  1623. const int level)
  1624. {
  1625. struct expander_device *ex_root = &root->ex_dev;
  1626. struct domain_device *child;
  1627. int res = 0;
  1628. list_for_each_entry(child, &ex_root->children, siblings) {
  1629. if (dev_is_expander(child->dev_type)) {
  1630. struct sas_expander_device *ex =
  1631. rphy_to_expander_device(child->rphy);
  1632. if (level > ex->level)
  1633. res = sas_discover_bfs_by_root_level(child,
  1634. level);
  1635. else if (level == ex->level)
  1636. res = sas_ex_discover_devices(child, -1);
  1637. }
  1638. }
  1639. return res;
  1640. }
  1641. static int sas_discover_bfs_by_root(struct domain_device *dev)
  1642. {
  1643. int res;
  1644. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1645. int level = ex->level+1;
  1646. res = sas_ex_discover_devices(dev, -1);
  1647. if (res)
  1648. goto out;
  1649. do {
  1650. res = sas_discover_bfs_by_root_level(dev, level);
  1651. mb();
  1652. level += 1;
  1653. } while (level <= dev->port->disc.max_level);
  1654. out:
  1655. return res;
  1656. }
  1657. static int sas_discover_new(struct domain_device *dev, int phy_id)
  1658. {
  1659. struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
  1660. struct domain_device *child;
  1661. int res;
  1662. pr_debug("ex %016llx phy%02d new device attached\n",
  1663. SAS_ADDR(dev->sas_addr), phy_id);
  1664. res = sas_ex_phy_discover(dev, phy_id);
  1665. if (res)
  1666. return res;
  1667. if (sas_ex_join_wide_port(dev, phy_id))
  1668. return 0;
  1669. res = sas_ex_discover_devices(dev, phy_id);
  1670. if (res)
  1671. return res;
  1672. list_for_each_entry(child, &dev->ex_dev.children, siblings) {
  1673. if (SAS_ADDR(child->sas_addr) ==
  1674. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1675. if (dev_is_expander(child->dev_type))
  1676. res = sas_discover_bfs_by_root(child);
  1677. break;
  1678. }
  1679. }
  1680. return res;
  1681. }
  1682. static bool dev_type_flutter(enum sas_device_type new, enum sas_device_type old)
  1683. {
  1684. if (old == new)
  1685. return true;
  1686. /* treat device directed resets as flutter, if we went
  1687. * SAS_END_DEVICE to SAS_SATA_PENDING the link needs recovery
  1688. */
  1689. if ((old == SAS_SATA_PENDING && new == SAS_END_DEVICE) ||
  1690. (old == SAS_END_DEVICE && new == SAS_SATA_PENDING))
  1691. return true;
  1692. return false;
  1693. }
  1694. static int sas_rediscover_dev(struct domain_device *dev, int phy_id,
  1695. bool last, int sibling)
  1696. {
  1697. struct expander_device *ex = &dev->ex_dev;
  1698. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1699. enum sas_device_type type = SAS_PHY_UNUSED;
  1700. u8 sas_addr[SAS_ADDR_SIZE];
  1701. char msg[80] = "";
  1702. int res;
  1703. if (!last)
  1704. sprintf(msg, ", part of a wide port with phy%02d", sibling);
  1705. pr_debug("ex %016llx rediscovering phy%02d%s\n",
  1706. SAS_ADDR(dev->sas_addr), phy_id, msg);
  1707. memset(sas_addr, 0, SAS_ADDR_SIZE);
  1708. res = sas_get_phy_attached_dev(dev, phy_id, sas_addr, &type);
  1709. switch (res) {
  1710. case SMP_RESP_NO_PHY:
  1711. phy->phy_state = PHY_NOT_PRESENT;
  1712. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1713. return res;
  1714. case SMP_RESP_PHY_VACANT:
  1715. phy->phy_state = PHY_VACANT;
  1716. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1717. return res;
  1718. case SMP_RESP_FUNC_ACC:
  1719. break;
  1720. case -ECOMM:
  1721. break;
  1722. default:
  1723. return res;
  1724. }
  1725. if ((SAS_ADDR(sas_addr) == 0) || (res == -ECOMM)) {
  1726. phy->phy_state = PHY_EMPTY;
  1727. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1728. /*
  1729. * Even though the PHY is empty, for convenience we discover
  1730. * the PHY to update the PHY info, like negotiated linkrate.
  1731. */
  1732. sas_ex_phy_discover(dev, phy_id);
  1733. return res;
  1734. } else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
  1735. dev_type_flutter(type, phy->attached_dev_type)) {
  1736. struct domain_device *ata_dev = sas_ex_to_ata(dev, phy_id);
  1737. char *action = "";
  1738. sas_ex_phy_discover(dev, phy_id);
  1739. if (ata_dev && phy->attached_dev_type == SAS_SATA_PENDING)
  1740. action = ", needs recovery";
  1741. pr_debug("ex %016llx phy%02d broadcast flutter%s\n",
  1742. SAS_ADDR(dev->sas_addr), phy_id, action);
  1743. return res;
  1744. }
  1745. /* we always have to delete the old device when we went here */
  1746. pr_info("ex %016llx phy%02d replace %016llx\n",
  1747. SAS_ADDR(dev->sas_addr), phy_id,
  1748. SAS_ADDR(phy->attached_sas_addr));
  1749. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1750. return sas_discover_new(dev, phy_id);
  1751. }
  1752. /**
  1753. * sas_rediscover - revalidate the domain.
  1754. * @dev:domain device to be detect.
  1755. * @phy_id: the phy id will be detected.
  1756. *
  1757. * NOTE: this process _must_ quit (return) as soon as any connection
  1758. * errors are encountered. Connection recovery is done elsewhere.
  1759. * Discover process only interrogates devices in order to discover the
  1760. * domain.For plugging out, we un-register the device only when it is
  1761. * the last phy in the port, for other phys in this port, we just delete it
  1762. * from the port.For inserting, we do discovery when it is the
  1763. * first phy,for other phys in this port, we add it to the port to
  1764. * forming the wide-port.
  1765. */
  1766. static int sas_rediscover(struct domain_device *dev, const int phy_id)
  1767. {
  1768. struct expander_device *ex = &dev->ex_dev;
  1769. struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
  1770. int res = 0;
  1771. int i;
  1772. bool last = true; /* is this the last phy of the port */
  1773. pr_debug("ex %016llx phy%02d originated BROADCAST(CHANGE)\n",
  1774. SAS_ADDR(dev->sas_addr), phy_id);
  1775. if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
  1776. for (i = 0; i < ex->num_phys; i++) {
  1777. struct ex_phy *phy = &ex->ex_phy[i];
  1778. if (i == phy_id)
  1779. continue;
  1780. if (SAS_ADDR(phy->attached_sas_addr) ==
  1781. SAS_ADDR(changed_phy->attached_sas_addr)) {
  1782. last = false;
  1783. break;
  1784. }
  1785. }
  1786. res = sas_rediscover_dev(dev, phy_id, last, i);
  1787. } else
  1788. res = sas_discover_new(dev, phy_id);
  1789. return res;
  1790. }
  1791. /**
  1792. * sas_ex_revalidate_domain - revalidate the domain
  1793. * @port_dev: port domain device.
  1794. *
  1795. * NOTE: this process _must_ quit (return) as soon as any connection
  1796. * errors are encountered. Connection recovery is done elsewhere.
  1797. * Discover process only interrogates devices in order to discover the
  1798. * domain.
  1799. */
  1800. int sas_ex_revalidate_domain(struct domain_device *port_dev)
  1801. {
  1802. int res;
  1803. struct domain_device *dev = NULL;
  1804. res = sas_find_bcast_dev(port_dev, &dev);
  1805. if (res == 0 && dev) {
  1806. struct expander_device *ex = &dev->ex_dev;
  1807. int i = 0, phy_id;
  1808. do {
  1809. phy_id = -1;
  1810. res = sas_find_bcast_phy(dev, &phy_id, i, true);
  1811. if (phy_id == -1)
  1812. break;
  1813. res = sas_rediscover(dev, phy_id);
  1814. i = phy_id + 1;
  1815. } while (i < ex->num_phys);
  1816. }
  1817. return res;
  1818. }
  1819. void sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
  1820. struct sas_rphy *rphy)
  1821. {
  1822. struct domain_device *dev;
  1823. unsigned int rcvlen = 0;
  1824. int ret = -EINVAL;
  1825. /* no rphy means no smp target support (ie aic94xx host) */
  1826. if (!rphy)
  1827. return sas_smp_host_handler(job, shost);
  1828. switch (rphy->identify.device_type) {
  1829. case SAS_EDGE_EXPANDER_DEVICE:
  1830. case SAS_FANOUT_EXPANDER_DEVICE:
  1831. break;
  1832. default:
  1833. pr_err("%s: can we send a smp request to a device?\n",
  1834. __func__);
  1835. goto out;
  1836. }
  1837. dev = sas_find_dev_by_rphy(rphy);
  1838. if (!dev) {
  1839. pr_err("%s: fail to find a domain_device?\n", __func__);
  1840. goto out;
  1841. }
  1842. /* do we need to support multiple segments? */
  1843. if (job->request_payload.sg_cnt > 1 ||
  1844. job->reply_payload.sg_cnt > 1) {
  1845. pr_info("%s: multiple segments req %u, rsp %u\n",
  1846. __func__, job->request_payload.payload_len,
  1847. job->reply_payload.payload_len);
  1848. goto out;
  1849. }
  1850. ret = smp_execute_task_sg(dev, job->request_payload.sg_list,
  1851. job->reply_payload.sg_list);
  1852. if (ret >= 0) {
  1853. /* bsg_job_done() requires the length received */
  1854. rcvlen = job->reply_payload.payload_len - ret;
  1855. ret = 0;
  1856. }
  1857. out:
  1858. bsg_job_done(job, ret, rcvlen);
  1859. }