sas_expander.c 47 KB

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