sas_expander.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  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. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  428. struct domain_device *dev = sas_find_dev_by_rphy(rphy);
  429. u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
  430. u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
  431. if (!resp)
  432. return -ENOMEM;
  433. req[1] = SMP_REPORT_PHY_ERR_LOG;
  434. req[9] = phy->number;
  435. res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
  436. resp, RPEL_RESP_SIZE);
  437. if (!res)
  438. goto out;
  439. phy->invalid_dword_count = scsi_to_u32(&resp[12]);
  440. phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
  441. phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
  442. phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
  443. out:
  444. kfree(resp);
  445. return res;
  446. }
  447. #define RPS_REQ_SIZE 16
  448. #define RPS_RESP_SIZE 60
  449. static int sas_get_report_phy_sata(struct domain_device *dev,
  450. int phy_id,
  451. struct smp_resp *rps_resp)
  452. {
  453. int res;
  454. u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
  455. u8 *resp = (u8 *)rps_resp;
  456. if (!rps_req)
  457. return -ENOMEM;
  458. rps_req[1] = SMP_REPORT_PHY_SATA;
  459. rps_req[9] = phy_id;
  460. res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
  461. rps_resp, RPS_RESP_SIZE);
  462. /* 0x34 is the FIS type for the D2H fis. There's a potential
  463. * standards cockup here. sas-2 explicitly specifies the FIS
  464. * should be encoded so that FIS type is in resp[24].
  465. * However, some expanders endian reverse this. Undo the
  466. * reversal here */
  467. if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
  468. int i;
  469. for (i = 0; i < 5; i++) {
  470. int j = 24 + (i*4);
  471. u8 a, b;
  472. a = resp[j + 0];
  473. b = resp[j + 1];
  474. resp[j + 0] = resp[j + 3];
  475. resp[j + 1] = resp[j + 2];
  476. resp[j + 2] = b;
  477. resp[j + 3] = a;
  478. }
  479. }
  480. kfree(rps_req);
  481. return res;
  482. }
  483. static void sas_ex_get_linkrate(struct domain_device *parent,
  484. struct domain_device *child,
  485. struct ex_phy *parent_phy)
  486. {
  487. struct expander_device *parent_ex = &parent->ex_dev;
  488. struct sas_port *port;
  489. int i;
  490. child->pathways = 0;
  491. port = parent_phy->port;
  492. for (i = 0; i < parent_ex->num_phys; i++) {
  493. struct ex_phy *phy = &parent_ex->ex_phy[i];
  494. if (phy->phy_state == PHY_VACANT ||
  495. phy->phy_state == PHY_NOT_PRESENT)
  496. continue;
  497. if (SAS_ADDR(phy->attached_sas_addr) ==
  498. SAS_ADDR(child->sas_addr)) {
  499. child->min_linkrate = min(parent->min_linkrate,
  500. phy->linkrate);
  501. child->max_linkrate = max(parent->max_linkrate,
  502. phy->linkrate);
  503. child->pathways++;
  504. sas_port_add_phy(port, phy->phy);
  505. }
  506. }
  507. child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
  508. child->pathways = min(child->pathways, parent->pathways);
  509. }
  510. static struct domain_device *sas_ex_discover_end_dev(
  511. struct domain_device *parent, int phy_id)
  512. {
  513. struct expander_device *parent_ex = &parent->ex_dev;
  514. struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
  515. struct domain_device *child = NULL;
  516. struct sas_rphy *rphy;
  517. int res;
  518. if (phy->attached_sata_host || phy->attached_sata_ps)
  519. return NULL;
  520. child = kzalloc(sizeof(*child), GFP_KERNEL);
  521. if (!child)
  522. return NULL;
  523. child->parent = parent;
  524. child->port = parent->port;
  525. child->iproto = phy->attached_iproto;
  526. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  527. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  528. if (!phy->port) {
  529. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  530. if (unlikely(!phy->port))
  531. goto out_err;
  532. if (unlikely(sas_port_add(phy->port) != 0)) {
  533. sas_port_free(phy->port);
  534. goto out_err;
  535. }
  536. }
  537. sas_ex_get_linkrate(parent, child, phy);
  538. if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
  539. child->dev_type = SATA_DEV;
  540. if (phy->attached_tproto & SAS_PROTO_STP)
  541. child->tproto = phy->attached_tproto;
  542. if (phy->attached_sata_dev)
  543. child->tproto |= SATA_DEV;
  544. res = sas_get_report_phy_sata(parent, phy_id,
  545. &child->sata_dev.rps_resp);
  546. if (res) {
  547. SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
  548. "0x%x\n", SAS_ADDR(parent->sas_addr),
  549. phy_id, res);
  550. goto out_free;
  551. }
  552. memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
  553. sizeof(struct dev_to_host_fis));
  554. rphy = sas_end_device_alloc(phy->port);
  555. if (unlikely(!rphy))
  556. goto out_free;
  557. sas_init_dev(child);
  558. child->rphy = rphy;
  559. spin_lock_irq(&parent->port->dev_list_lock);
  560. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  561. spin_unlock_irq(&parent->port->dev_list_lock);
  562. res = sas_discover_sata(child);
  563. if (res) {
  564. SAS_DPRINTK("sas_discover_sata() for device %16llx at "
  565. "%016llx:0x%x returned 0x%x\n",
  566. SAS_ADDR(child->sas_addr),
  567. SAS_ADDR(parent->sas_addr), phy_id, res);
  568. goto out_list_del;
  569. }
  570. } else if (phy->attached_tproto & SAS_PROTO_SSP) {
  571. child->dev_type = SAS_END_DEV;
  572. rphy = sas_end_device_alloc(phy->port);
  573. /* FIXME: error handling */
  574. if (unlikely(!rphy))
  575. goto out_free;
  576. child->tproto = phy->attached_tproto;
  577. sas_init_dev(child);
  578. child->rphy = rphy;
  579. sas_fill_in_rphy(child, rphy);
  580. spin_lock_irq(&parent->port->dev_list_lock);
  581. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  582. spin_unlock_irq(&parent->port->dev_list_lock);
  583. res = sas_discover_end_dev(child);
  584. if (res) {
  585. SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
  586. "at %016llx:0x%x returned 0x%x\n",
  587. SAS_ADDR(child->sas_addr),
  588. SAS_ADDR(parent->sas_addr), phy_id, res);
  589. goto out_list_del;
  590. }
  591. } else {
  592. SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
  593. phy->attached_tproto, SAS_ADDR(parent->sas_addr),
  594. phy_id);
  595. }
  596. list_add_tail(&child->siblings, &parent_ex->children);
  597. return child;
  598. out_list_del:
  599. sas_rphy_free(child->rphy);
  600. child->rphy = NULL;
  601. list_del(&child->dev_list_node);
  602. out_free:
  603. sas_port_delete(phy->port);
  604. out_err:
  605. phy->port = NULL;
  606. kfree(child);
  607. return NULL;
  608. }
  609. /* See if this phy is part of a wide port */
  610. static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
  611. {
  612. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  613. int i;
  614. for (i = 0; i < parent->ex_dev.num_phys; i++) {
  615. struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
  616. if (ephy == phy)
  617. continue;
  618. if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
  619. SAS_ADDR_SIZE) && ephy->port) {
  620. sas_port_add_phy(ephy->port, phy->phy);
  621. phy->phy_state = PHY_DEVICE_DISCOVERED;
  622. return 0;
  623. }
  624. }
  625. return -ENODEV;
  626. }
  627. static struct domain_device *sas_ex_discover_expander(
  628. struct domain_device *parent, int phy_id)
  629. {
  630. struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
  631. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  632. struct domain_device *child = NULL;
  633. struct sas_rphy *rphy;
  634. struct sas_expander_device *edev;
  635. struct asd_sas_port *port;
  636. int res;
  637. if (phy->routing_attr == DIRECT_ROUTING) {
  638. SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
  639. "allowed\n",
  640. SAS_ADDR(parent->sas_addr), phy_id,
  641. SAS_ADDR(phy->attached_sas_addr),
  642. phy->attached_phy_id);
  643. return NULL;
  644. }
  645. child = kzalloc(sizeof(*child), GFP_KERNEL);
  646. if (!child)
  647. return NULL;
  648. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  649. /* FIXME: better error handling */
  650. BUG_ON(sas_port_add(phy->port) != 0);
  651. switch (phy->attached_dev_type) {
  652. case EDGE_DEV:
  653. rphy = sas_expander_alloc(phy->port,
  654. SAS_EDGE_EXPANDER_DEVICE);
  655. break;
  656. case FANOUT_DEV:
  657. rphy = sas_expander_alloc(phy->port,
  658. SAS_FANOUT_EXPANDER_DEVICE);
  659. break;
  660. default:
  661. rphy = NULL; /* shut gcc up */
  662. BUG();
  663. }
  664. port = parent->port;
  665. child->rphy = rphy;
  666. edev = rphy_to_expander_device(rphy);
  667. child->dev_type = phy->attached_dev_type;
  668. child->parent = parent;
  669. child->port = port;
  670. child->iproto = phy->attached_iproto;
  671. child->tproto = phy->attached_tproto;
  672. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  673. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  674. sas_ex_get_linkrate(parent, child, phy);
  675. edev->level = parent_ex->level + 1;
  676. parent->port->disc.max_level = max(parent->port->disc.max_level,
  677. edev->level);
  678. sas_init_dev(child);
  679. sas_fill_in_rphy(child, rphy);
  680. sas_rphy_add(rphy);
  681. spin_lock_irq(&parent->port->dev_list_lock);
  682. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  683. spin_unlock_irq(&parent->port->dev_list_lock);
  684. res = sas_discover_expander(child);
  685. if (res) {
  686. kfree(child);
  687. return NULL;
  688. }
  689. list_add_tail(&child->siblings, &parent->ex_dev.children);
  690. return child;
  691. }
  692. static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
  693. {
  694. struct expander_device *ex = &dev->ex_dev;
  695. struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
  696. struct domain_device *child = NULL;
  697. int res = 0;
  698. /* Phy state */
  699. if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
  700. if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
  701. res = sas_ex_phy_discover(dev, phy_id);
  702. if (res)
  703. return res;
  704. }
  705. /* Parent and domain coherency */
  706. if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  707. SAS_ADDR(dev->port->sas_addr))) {
  708. sas_add_parent_port(dev, phy_id);
  709. return 0;
  710. }
  711. if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  712. SAS_ADDR(dev->parent->sas_addr))) {
  713. sas_add_parent_port(dev, phy_id);
  714. if (ex_phy->routing_attr == TABLE_ROUTING)
  715. sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
  716. return 0;
  717. }
  718. if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
  719. sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
  720. if (ex_phy->attached_dev_type == NO_DEVICE) {
  721. if (ex_phy->routing_attr == DIRECT_ROUTING) {
  722. memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  723. sas_configure_routing(dev, ex_phy->attached_sas_addr);
  724. }
  725. return 0;
  726. } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
  727. return 0;
  728. if (ex_phy->attached_dev_type != SAS_END_DEV &&
  729. ex_phy->attached_dev_type != FANOUT_DEV &&
  730. ex_phy->attached_dev_type != EDGE_DEV) {
  731. SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
  732. "phy 0x%x\n", ex_phy->attached_dev_type,
  733. SAS_ADDR(dev->sas_addr),
  734. phy_id);
  735. return 0;
  736. }
  737. res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
  738. if (res) {
  739. SAS_DPRINTK("configure routing for dev %016llx "
  740. "reported 0x%x. Forgotten\n",
  741. SAS_ADDR(ex_phy->attached_sas_addr), res);
  742. sas_disable_routing(dev, ex_phy->attached_sas_addr);
  743. return res;
  744. }
  745. res = sas_ex_join_wide_port(dev, phy_id);
  746. if (!res) {
  747. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  748. phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
  749. return res;
  750. }
  751. switch (ex_phy->attached_dev_type) {
  752. case SAS_END_DEV:
  753. child = sas_ex_discover_end_dev(dev, phy_id);
  754. break;
  755. case FANOUT_DEV:
  756. if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
  757. SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
  758. "attached to ex %016llx phy 0x%x\n",
  759. SAS_ADDR(ex_phy->attached_sas_addr),
  760. ex_phy->attached_phy_id,
  761. SAS_ADDR(dev->sas_addr),
  762. phy_id);
  763. sas_ex_disable_phy(dev, phy_id);
  764. break;
  765. } else
  766. memcpy(dev->port->disc.fanout_sas_addr,
  767. ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
  768. /* fallthrough */
  769. case EDGE_DEV:
  770. child = sas_ex_discover_expander(dev, phy_id);
  771. break;
  772. default:
  773. break;
  774. }
  775. if (child) {
  776. int i;
  777. for (i = 0; i < ex->num_phys; i++) {
  778. if (ex->ex_phy[i].phy_state == PHY_VACANT ||
  779. ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
  780. continue;
  781. if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
  782. SAS_ADDR(child->sas_addr))
  783. ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
  784. }
  785. }
  786. return res;
  787. }
  788. static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
  789. {
  790. struct expander_device *ex = &dev->ex_dev;
  791. int i;
  792. for (i = 0; i < ex->num_phys; i++) {
  793. struct ex_phy *phy = &ex->ex_phy[i];
  794. if (phy->phy_state == PHY_VACANT ||
  795. phy->phy_state == PHY_NOT_PRESENT)
  796. continue;
  797. if ((phy->attached_dev_type == EDGE_DEV ||
  798. phy->attached_dev_type == FANOUT_DEV) &&
  799. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  800. memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
  801. return 1;
  802. }
  803. }
  804. return 0;
  805. }
  806. static int sas_check_level_subtractive_boundary(struct domain_device *dev)
  807. {
  808. struct expander_device *ex = &dev->ex_dev;
  809. struct domain_device *child;
  810. u8 sub_addr[8] = {0, };
  811. list_for_each_entry(child, &ex->children, siblings) {
  812. if (child->dev_type != EDGE_DEV &&
  813. child->dev_type != FANOUT_DEV)
  814. continue;
  815. if (sub_addr[0] == 0) {
  816. sas_find_sub_addr(child, sub_addr);
  817. continue;
  818. } else {
  819. u8 s2[8];
  820. if (sas_find_sub_addr(child, s2) &&
  821. (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
  822. SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
  823. "diverges from subtractive "
  824. "boundary %016llx\n",
  825. SAS_ADDR(dev->sas_addr),
  826. SAS_ADDR(child->sas_addr),
  827. SAS_ADDR(s2),
  828. SAS_ADDR(sub_addr));
  829. sas_ex_disable_port(child, s2);
  830. }
  831. }
  832. }
  833. return 0;
  834. }
  835. /**
  836. * sas_ex_discover_devices -- discover devices attached to this expander
  837. * dev: pointer to the expander domain device
  838. * single: if you want to do a single phy, else set to -1;
  839. *
  840. * Configure this expander for use with its devices and register the
  841. * devices of this expander.
  842. */
  843. static int sas_ex_discover_devices(struct domain_device *dev, int single)
  844. {
  845. struct expander_device *ex = &dev->ex_dev;
  846. int i = 0, end = ex->num_phys;
  847. int res = 0;
  848. if (0 <= single && single < end) {
  849. i = single;
  850. end = i+1;
  851. }
  852. for ( ; i < end; i++) {
  853. struct ex_phy *ex_phy = &ex->ex_phy[i];
  854. if (ex_phy->phy_state == PHY_VACANT ||
  855. ex_phy->phy_state == PHY_NOT_PRESENT ||
  856. ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
  857. continue;
  858. switch (ex_phy->linkrate) {
  859. case SAS_PHY_DISABLED:
  860. case SAS_PHY_RESET_PROBLEM:
  861. case SAS_SATA_PORT_SELECTOR:
  862. continue;
  863. default:
  864. res = sas_ex_discover_dev(dev, i);
  865. if (res)
  866. break;
  867. continue;
  868. }
  869. }
  870. if (!res)
  871. sas_check_level_subtractive_boundary(dev);
  872. return res;
  873. }
  874. static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
  875. {
  876. struct expander_device *ex = &dev->ex_dev;
  877. int i;
  878. u8 *sub_sas_addr = NULL;
  879. if (dev->dev_type != EDGE_DEV)
  880. return 0;
  881. for (i = 0; i < ex->num_phys; i++) {
  882. struct ex_phy *phy = &ex->ex_phy[i];
  883. if (phy->phy_state == PHY_VACANT ||
  884. phy->phy_state == PHY_NOT_PRESENT)
  885. continue;
  886. if ((phy->attached_dev_type == FANOUT_DEV ||
  887. phy->attached_dev_type == EDGE_DEV) &&
  888. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  889. if (!sub_sas_addr)
  890. sub_sas_addr = &phy->attached_sas_addr[0];
  891. else if (SAS_ADDR(sub_sas_addr) !=
  892. SAS_ADDR(phy->attached_sas_addr)) {
  893. SAS_DPRINTK("ex %016llx phy 0x%x "
  894. "diverges(%016llx) on subtractive "
  895. "boundary(%016llx). Disabled\n",
  896. SAS_ADDR(dev->sas_addr), i,
  897. SAS_ADDR(phy->attached_sas_addr),
  898. SAS_ADDR(sub_sas_addr));
  899. sas_ex_disable_phy(dev, i);
  900. }
  901. }
  902. }
  903. return 0;
  904. }
  905. static void sas_print_parent_topology_bug(struct domain_device *child,
  906. struct ex_phy *parent_phy,
  907. struct ex_phy *child_phy)
  908. {
  909. static const char ra_char[] = {
  910. [DIRECT_ROUTING] = 'D',
  911. [SUBTRACTIVE_ROUTING] = 'S',
  912. [TABLE_ROUTING] = 'T',
  913. };
  914. static const char *ex_type[] = {
  915. [EDGE_DEV] = "edge",
  916. [FANOUT_DEV] = "fanout",
  917. };
  918. struct domain_device *parent = child->parent;
  919. sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
  920. "has %c:%c routing link!\n",
  921. ex_type[parent->dev_type],
  922. SAS_ADDR(parent->sas_addr),
  923. parent_phy->phy_id,
  924. ex_type[child->dev_type],
  925. SAS_ADDR(child->sas_addr),
  926. child_phy->phy_id,
  927. ra_char[parent_phy->routing_attr],
  928. ra_char[child_phy->routing_attr]);
  929. }
  930. static int sas_check_eeds(struct domain_device *child,
  931. struct ex_phy *parent_phy,
  932. struct ex_phy *child_phy)
  933. {
  934. int res = 0;
  935. struct domain_device *parent = child->parent;
  936. if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
  937. res = -ENODEV;
  938. SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
  939. "phy S:0x%x, while there is a fanout ex %016llx\n",
  940. SAS_ADDR(parent->sas_addr),
  941. parent_phy->phy_id,
  942. SAS_ADDR(child->sas_addr),
  943. child_phy->phy_id,
  944. SAS_ADDR(parent->port->disc.fanout_sas_addr));
  945. } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
  946. memcpy(parent->port->disc.eeds_a, parent->sas_addr,
  947. SAS_ADDR_SIZE);
  948. memcpy(parent->port->disc.eeds_b, child->sas_addr,
  949. SAS_ADDR_SIZE);
  950. } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
  951. SAS_ADDR(parent->sas_addr)) ||
  952. (SAS_ADDR(parent->port->disc.eeds_a) ==
  953. SAS_ADDR(child->sas_addr)))
  954. &&
  955. ((SAS_ADDR(parent->port->disc.eeds_b) ==
  956. SAS_ADDR(parent->sas_addr)) ||
  957. (SAS_ADDR(parent->port->disc.eeds_b) ==
  958. SAS_ADDR(child->sas_addr))))
  959. ;
  960. else {
  961. res = -ENODEV;
  962. SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
  963. "phy 0x%x link forms a third EEDS!\n",
  964. SAS_ADDR(parent->sas_addr),
  965. parent_phy->phy_id,
  966. SAS_ADDR(child->sas_addr),
  967. child_phy->phy_id);
  968. }
  969. return res;
  970. }
  971. /* Here we spill over 80 columns. It is intentional.
  972. */
  973. static int sas_check_parent_topology(struct domain_device *child)
  974. {
  975. struct expander_device *child_ex = &child->ex_dev;
  976. struct expander_device *parent_ex;
  977. int i;
  978. int res = 0;
  979. if (!child->parent)
  980. return 0;
  981. if (child->parent->dev_type != EDGE_DEV &&
  982. child->parent->dev_type != FANOUT_DEV)
  983. return 0;
  984. parent_ex = &child->parent->ex_dev;
  985. for (i = 0; i < parent_ex->num_phys; i++) {
  986. struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
  987. struct ex_phy *child_phy;
  988. if (parent_phy->phy_state == PHY_VACANT ||
  989. parent_phy->phy_state == PHY_NOT_PRESENT)
  990. continue;
  991. if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
  992. continue;
  993. child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
  994. switch (child->parent->dev_type) {
  995. case EDGE_DEV:
  996. if (child->dev_type == FANOUT_DEV) {
  997. if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
  998. child_phy->routing_attr != TABLE_ROUTING) {
  999. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1000. res = -ENODEV;
  1001. }
  1002. } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1003. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1004. res = sas_check_eeds(child, parent_phy, child_phy);
  1005. } else if (child_phy->routing_attr != TABLE_ROUTING) {
  1006. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1007. res = -ENODEV;
  1008. }
  1009. } else if (parent_phy->routing_attr == TABLE_ROUTING &&
  1010. child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
  1011. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1012. res = -ENODEV;
  1013. }
  1014. break;
  1015. case FANOUT_DEV:
  1016. if (parent_phy->routing_attr != TABLE_ROUTING ||
  1017. child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
  1018. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1019. res = -ENODEV;
  1020. }
  1021. break;
  1022. default:
  1023. break;
  1024. }
  1025. }
  1026. return res;
  1027. }
  1028. #define RRI_REQ_SIZE 16
  1029. #define RRI_RESP_SIZE 44
  1030. static int sas_configure_present(struct domain_device *dev, int phy_id,
  1031. u8 *sas_addr, int *index, int *present)
  1032. {
  1033. int i, res = 0;
  1034. struct expander_device *ex = &dev->ex_dev;
  1035. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1036. u8 *rri_req;
  1037. u8 *rri_resp;
  1038. *present = 0;
  1039. *index = 0;
  1040. rri_req = alloc_smp_req(RRI_REQ_SIZE);
  1041. if (!rri_req)
  1042. return -ENOMEM;
  1043. rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
  1044. if (!rri_resp) {
  1045. kfree(rri_req);
  1046. return -ENOMEM;
  1047. }
  1048. rri_req[1] = SMP_REPORT_ROUTE_INFO;
  1049. rri_req[9] = phy_id;
  1050. for (i = 0; i < ex->max_route_indexes ; i++) {
  1051. *(__be16 *)(rri_req+6) = cpu_to_be16(i);
  1052. res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
  1053. RRI_RESP_SIZE);
  1054. if (res)
  1055. goto out;
  1056. res = rri_resp[2];
  1057. if (res == SMP_RESP_NO_INDEX) {
  1058. SAS_DPRINTK("overflow of indexes: dev %016llx "
  1059. "phy 0x%x index 0x%x\n",
  1060. SAS_ADDR(dev->sas_addr), phy_id, i);
  1061. goto out;
  1062. } else if (res != SMP_RESP_FUNC_ACC) {
  1063. SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
  1064. "result 0x%x\n", __FUNCTION__,
  1065. SAS_ADDR(dev->sas_addr), phy_id, i, res);
  1066. goto out;
  1067. }
  1068. if (SAS_ADDR(sas_addr) != 0) {
  1069. if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
  1070. *index = i;
  1071. if ((rri_resp[12] & 0x80) == 0x80)
  1072. *present = 0;
  1073. else
  1074. *present = 1;
  1075. goto out;
  1076. } else if (SAS_ADDR(rri_resp+16) == 0) {
  1077. *index = i;
  1078. *present = 0;
  1079. goto out;
  1080. }
  1081. } else if (SAS_ADDR(rri_resp+16) == 0 &&
  1082. phy->last_da_index < i) {
  1083. phy->last_da_index = i;
  1084. *index = i;
  1085. *present = 0;
  1086. goto out;
  1087. }
  1088. }
  1089. res = -1;
  1090. out:
  1091. kfree(rri_req);
  1092. kfree(rri_resp);
  1093. return res;
  1094. }
  1095. #define CRI_REQ_SIZE 44
  1096. #define CRI_RESP_SIZE 8
  1097. static int sas_configure_set(struct domain_device *dev, int phy_id,
  1098. u8 *sas_addr, int index, int include)
  1099. {
  1100. int res;
  1101. u8 *cri_req;
  1102. u8 *cri_resp;
  1103. cri_req = alloc_smp_req(CRI_REQ_SIZE);
  1104. if (!cri_req)
  1105. return -ENOMEM;
  1106. cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
  1107. if (!cri_resp) {
  1108. kfree(cri_req);
  1109. return -ENOMEM;
  1110. }
  1111. cri_req[1] = SMP_CONF_ROUTE_INFO;
  1112. *(__be16 *)(cri_req+6) = cpu_to_be16(index);
  1113. cri_req[9] = phy_id;
  1114. if (SAS_ADDR(sas_addr) == 0 || !include)
  1115. cri_req[12] |= 0x80;
  1116. memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
  1117. res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
  1118. CRI_RESP_SIZE);
  1119. if (res)
  1120. goto out;
  1121. res = cri_resp[2];
  1122. if (res == SMP_RESP_NO_INDEX) {
  1123. SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
  1124. "index 0x%x\n",
  1125. SAS_ADDR(dev->sas_addr), phy_id, index);
  1126. }
  1127. out:
  1128. kfree(cri_req);
  1129. kfree(cri_resp);
  1130. return res;
  1131. }
  1132. static int sas_configure_phy(struct domain_device *dev, int phy_id,
  1133. u8 *sas_addr, int include)
  1134. {
  1135. int index;
  1136. int present;
  1137. int res;
  1138. res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
  1139. if (res)
  1140. return res;
  1141. if (include ^ present)
  1142. return sas_configure_set(dev, phy_id, sas_addr, index,include);
  1143. return res;
  1144. }
  1145. /**
  1146. * sas_configure_parent -- configure routing table of parent
  1147. * parent: parent expander
  1148. * child: child expander
  1149. * sas_addr: SAS port identifier of device directly attached to child
  1150. */
  1151. static int sas_configure_parent(struct domain_device *parent,
  1152. struct domain_device *child,
  1153. u8 *sas_addr, int include)
  1154. {
  1155. struct expander_device *ex_parent = &parent->ex_dev;
  1156. int res = 0;
  1157. int i;
  1158. if (parent->parent) {
  1159. res = sas_configure_parent(parent->parent, parent, sas_addr,
  1160. include);
  1161. if (res)
  1162. return res;
  1163. }
  1164. if (ex_parent->conf_route_table == 0) {
  1165. SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
  1166. SAS_ADDR(parent->sas_addr));
  1167. return 0;
  1168. }
  1169. for (i = 0; i < ex_parent->num_phys; i++) {
  1170. struct ex_phy *phy = &ex_parent->ex_phy[i];
  1171. if ((phy->routing_attr == TABLE_ROUTING) &&
  1172. (SAS_ADDR(phy->attached_sas_addr) ==
  1173. SAS_ADDR(child->sas_addr))) {
  1174. res = sas_configure_phy(parent, i, sas_addr, include);
  1175. if (res)
  1176. return res;
  1177. }
  1178. }
  1179. return res;
  1180. }
  1181. /**
  1182. * sas_configure_routing -- configure routing
  1183. * dev: expander device
  1184. * sas_addr: port identifier of device directly attached to the expander device
  1185. */
  1186. static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
  1187. {
  1188. if (dev->parent)
  1189. return sas_configure_parent(dev->parent, dev, sas_addr, 1);
  1190. return 0;
  1191. }
  1192. static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
  1193. {
  1194. if (dev->parent)
  1195. return sas_configure_parent(dev->parent, dev, sas_addr, 0);
  1196. return 0;
  1197. }
  1198. /**
  1199. * sas_discover_expander -- expander discovery
  1200. * @ex: pointer to expander domain device
  1201. *
  1202. * See comment in sas_discover_sata().
  1203. */
  1204. static int sas_discover_expander(struct domain_device *dev)
  1205. {
  1206. int res;
  1207. res = sas_notify_lldd_dev_found(dev);
  1208. if (res)
  1209. return res;
  1210. res = sas_ex_general(dev);
  1211. if (res)
  1212. goto out_err;
  1213. res = sas_ex_manuf_info(dev);
  1214. if (res)
  1215. goto out_err;
  1216. res = sas_expander_discover(dev);
  1217. if (res) {
  1218. SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
  1219. SAS_ADDR(dev->sas_addr), res);
  1220. goto out_err;
  1221. }
  1222. sas_check_ex_subtractive_boundary(dev);
  1223. res = sas_check_parent_topology(dev);
  1224. if (res)
  1225. goto out_err;
  1226. return 0;
  1227. out_err:
  1228. sas_notify_lldd_dev_gone(dev);
  1229. return res;
  1230. }
  1231. static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
  1232. {
  1233. int res = 0;
  1234. struct domain_device *dev;
  1235. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  1236. if (dev->dev_type == EDGE_DEV ||
  1237. dev->dev_type == FANOUT_DEV) {
  1238. struct sas_expander_device *ex =
  1239. rphy_to_expander_device(dev->rphy);
  1240. if (level == ex->level)
  1241. res = sas_ex_discover_devices(dev, -1);
  1242. else if (level > 0)
  1243. res = sas_ex_discover_devices(port->port_dev, -1);
  1244. }
  1245. }
  1246. return res;
  1247. }
  1248. static int sas_ex_bfs_disc(struct asd_sas_port *port)
  1249. {
  1250. int res;
  1251. int level;
  1252. do {
  1253. level = port->disc.max_level;
  1254. res = sas_ex_level_discovery(port, level);
  1255. mb();
  1256. } while (level < port->disc.max_level);
  1257. return res;
  1258. }
  1259. int sas_discover_root_expander(struct domain_device *dev)
  1260. {
  1261. int res;
  1262. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1263. res = sas_rphy_add(dev->rphy);
  1264. if (res)
  1265. goto out_err;
  1266. ex->level = dev->port->disc.max_level; /* 0 */
  1267. res = sas_discover_expander(dev);
  1268. if (res)
  1269. goto out_err2;
  1270. sas_ex_bfs_disc(dev->port);
  1271. return res;
  1272. out_err2:
  1273. sas_rphy_remove(dev->rphy);
  1274. out_err:
  1275. return res;
  1276. }
  1277. /* ---------- Domain revalidation ---------- */
  1278. static int sas_get_phy_discover(struct domain_device *dev,
  1279. int phy_id, struct smp_resp *disc_resp)
  1280. {
  1281. int res;
  1282. u8 *disc_req;
  1283. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  1284. if (!disc_req)
  1285. return -ENOMEM;
  1286. disc_req[1] = SMP_DISCOVER;
  1287. disc_req[9] = phy_id;
  1288. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  1289. disc_resp, DISCOVER_RESP_SIZE);
  1290. if (res)
  1291. goto out;
  1292. else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
  1293. res = disc_resp->result;
  1294. goto out;
  1295. }
  1296. out:
  1297. kfree(disc_req);
  1298. return res;
  1299. }
  1300. static int sas_get_phy_change_count(struct domain_device *dev,
  1301. int phy_id, int *pcc)
  1302. {
  1303. int res;
  1304. struct smp_resp *disc_resp;
  1305. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1306. if (!disc_resp)
  1307. return -ENOMEM;
  1308. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1309. if (!res)
  1310. *pcc = disc_resp->disc.change_count;
  1311. kfree(disc_resp);
  1312. return res;
  1313. }
  1314. static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
  1315. int phy_id, u8 *attached_sas_addr)
  1316. {
  1317. int res;
  1318. struct smp_resp *disc_resp;
  1319. struct discover_resp *dr;
  1320. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1321. if (!disc_resp)
  1322. return -ENOMEM;
  1323. dr = &disc_resp->disc;
  1324. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1325. if (!res) {
  1326. memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
  1327. if (dr->attached_dev_type == 0)
  1328. memset(attached_sas_addr, 0, 8);
  1329. }
  1330. kfree(disc_resp);
  1331. return res;
  1332. }
  1333. static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
  1334. int from_phy)
  1335. {
  1336. struct expander_device *ex = &dev->ex_dev;
  1337. int res = 0;
  1338. int i;
  1339. for (i = from_phy; i < ex->num_phys; i++) {
  1340. int phy_change_count = 0;
  1341. res = sas_get_phy_change_count(dev, i, &phy_change_count);
  1342. if (res)
  1343. goto out;
  1344. else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
  1345. ex->ex_phy[i].phy_change_count = phy_change_count;
  1346. *phy_id = i;
  1347. return 0;
  1348. }
  1349. }
  1350. out:
  1351. return res;
  1352. }
  1353. static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
  1354. {
  1355. int res;
  1356. u8 *rg_req;
  1357. struct smp_resp *rg_resp;
  1358. rg_req = alloc_smp_req(RG_REQ_SIZE);
  1359. if (!rg_req)
  1360. return -ENOMEM;
  1361. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  1362. if (!rg_resp) {
  1363. kfree(rg_req);
  1364. return -ENOMEM;
  1365. }
  1366. rg_req[1] = SMP_REPORT_GENERAL;
  1367. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  1368. RG_RESP_SIZE);
  1369. if (res)
  1370. goto out;
  1371. if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  1372. res = rg_resp->result;
  1373. goto out;
  1374. }
  1375. *ecc = be16_to_cpu(rg_resp->rg.change_count);
  1376. out:
  1377. kfree(rg_resp);
  1378. kfree(rg_req);
  1379. return res;
  1380. }
  1381. static int sas_find_bcast_dev(struct domain_device *dev,
  1382. struct domain_device **src_dev)
  1383. {
  1384. struct expander_device *ex = &dev->ex_dev;
  1385. int ex_change_count = -1;
  1386. int res;
  1387. res = sas_get_ex_change_count(dev, &ex_change_count);
  1388. if (res)
  1389. goto out;
  1390. if (ex_change_count != -1 &&
  1391. ex_change_count != ex->ex_change_count) {
  1392. *src_dev = dev;
  1393. ex->ex_change_count = ex_change_count;
  1394. } else {
  1395. struct domain_device *ch;
  1396. list_for_each_entry(ch, &ex->children, siblings) {
  1397. if (ch->dev_type == EDGE_DEV ||
  1398. ch->dev_type == FANOUT_DEV) {
  1399. res = sas_find_bcast_dev(ch, src_dev);
  1400. if (src_dev)
  1401. return res;
  1402. }
  1403. }
  1404. }
  1405. out:
  1406. return res;
  1407. }
  1408. static void sas_unregister_ex_tree(struct domain_device *dev)
  1409. {
  1410. struct expander_device *ex = &dev->ex_dev;
  1411. struct domain_device *child, *n;
  1412. list_for_each_entry_safe(child, n, &ex->children, siblings) {
  1413. if (child->dev_type == EDGE_DEV ||
  1414. child->dev_type == FANOUT_DEV)
  1415. sas_unregister_ex_tree(child);
  1416. else
  1417. sas_unregister_dev(child);
  1418. }
  1419. sas_unregister_dev(dev);
  1420. }
  1421. static void sas_unregister_devs_sas_addr(struct domain_device *parent,
  1422. int phy_id)
  1423. {
  1424. struct expander_device *ex_dev = &parent->ex_dev;
  1425. struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
  1426. struct domain_device *child, *n;
  1427. list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
  1428. if (SAS_ADDR(child->sas_addr) ==
  1429. SAS_ADDR(phy->attached_sas_addr)) {
  1430. if (child->dev_type == EDGE_DEV ||
  1431. child->dev_type == FANOUT_DEV)
  1432. sas_unregister_ex_tree(child);
  1433. else
  1434. sas_unregister_dev(child);
  1435. break;
  1436. }
  1437. }
  1438. sas_disable_routing(parent, phy->attached_sas_addr);
  1439. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  1440. sas_port_delete_phy(phy->port, phy->phy);
  1441. if (phy->port->num_phys == 0)
  1442. sas_port_delete(phy->port);
  1443. phy->port = NULL;
  1444. }
  1445. static int sas_discover_bfs_by_root_level(struct domain_device *root,
  1446. const int level)
  1447. {
  1448. struct expander_device *ex_root = &root->ex_dev;
  1449. struct domain_device *child;
  1450. int res = 0;
  1451. list_for_each_entry(child, &ex_root->children, siblings) {
  1452. if (child->dev_type == EDGE_DEV ||
  1453. child->dev_type == FANOUT_DEV) {
  1454. struct sas_expander_device *ex =
  1455. rphy_to_expander_device(child->rphy);
  1456. if (level > ex->level)
  1457. res = sas_discover_bfs_by_root_level(child,
  1458. level);
  1459. else if (level == ex->level)
  1460. res = sas_ex_discover_devices(child, -1);
  1461. }
  1462. }
  1463. return res;
  1464. }
  1465. static int sas_discover_bfs_by_root(struct domain_device *dev)
  1466. {
  1467. int res;
  1468. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1469. int level = ex->level+1;
  1470. res = sas_ex_discover_devices(dev, -1);
  1471. if (res)
  1472. goto out;
  1473. do {
  1474. res = sas_discover_bfs_by_root_level(dev, level);
  1475. mb();
  1476. level += 1;
  1477. } while (level <= dev->port->disc.max_level);
  1478. out:
  1479. return res;
  1480. }
  1481. static int sas_discover_new(struct domain_device *dev, int phy_id)
  1482. {
  1483. struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
  1484. struct domain_device *child;
  1485. int res;
  1486. SAS_DPRINTK("ex %016llx phy%d new device attached\n",
  1487. SAS_ADDR(dev->sas_addr), phy_id);
  1488. res = sas_ex_phy_discover(dev, phy_id);
  1489. if (res)
  1490. goto out;
  1491. res = sas_ex_discover_devices(dev, phy_id);
  1492. if (res)
  1493. goto out;
  1494. list_for_each_entry(child, &dev->ex_dev.children, siblings) {
  1495. if (SAS_ADDR(child->sas_addr) ==
  1496. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1497. if (child->dev_type == EDGE_DEV ||
  1498. child->dev_type == FANOUT_DEV)
  1499. res = sas_discover_bfs_by_root(child);
  1500. break;
  1501. }
  1502. }
  1503. out:
  1504. return res;
  1505. }
  1506. static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
  1507. {
  1508. struct expander_device *ex = &dev->ex_dev;
  1509. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1510. u8 attached_sas_addr[8];
  1511. int res;
  1512. res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
  1513. switch (res) {
  1514. case SMP_RESP_NO_PHY:
  1515. phy->phy_state = PHY_NOT_PRESENT;
  1516. sas_unregister_devs_sas_addr(dev, phy_id);
  1517. goto out; break;
  1518. case SMP_RESP_PHY_VACANT:
  1519. phy->phy_state = PHY_VACANT;
  1520. sas_unregister_devs_sas_addr(dev, phy_id);
  1521. goto out; break;
  1522. case SMP_RESP_FUNC_ACC:
  1523. break;
  1524. }
  1525. if (SAS_ADDR(attached_sas_addr) == 0) {
  1526. phy->phy_state = PHY_EMPTY;
  1527. sas_unregister_devs_sas_addr(dev, phy_id);
  1528. } else if (SAS_ADDR(attached_sas_addr) ==
  1529. SAS_ADDR(phy->attached_sas_addr)) {
  1530. SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
  1531. SAS_ADDR(dev->sas_addr), phy_id);
  1532. sas_ex_phy_discover(dev, phy_id);
  1533. } else
  1534. res = sas_discover_new(dev, phy_id);
  1535. out:
  1536. return res;
  1537. }
  1538. static int sas_rediscover(struct domain_device *dev, const int phy_id)
  1539. {
  1540. struct expander_device *ex = &dev->ex_dev;
  1541. struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
  1542. int res = 0;
  1543. int i;
  1544. SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
  1545. SAS_ADDR(dev->sas_addr), phy_id);
  1546. if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
  1547. for (i = 0; i < ex->num_phys; i++) {
  1548. struct ex_phy *phy = &ex->ex_phy[i];
  1549. if (i == phy_id)
  1550. continue;
  1551. if (SAS_ADDR(phy->attached_sas_addr) ==
  1552. SAS_ADDR(changed_phy->attached_sas_addr)) {
  1553. SAS_DPRINTK("phy%d part of wide port with "
  1554. "phy%d\n", phy_id, i);
  1555. goto out;
  1556. }
  1557. }
  1558. res = sas_rediscover_dev(dev, phy_id);
  1559. } else
  1560. res = sas_discover_new(dev, phy_id);
  1561. out:
  1562. return res;
  1563. }
  1564. /**
  1565. * sas_revalidate_domain -- revalidate the domain
  1566. * @port: port to the domain of interest
  1567. *
  1568. * NOTE: this process _must_ quit (return) as soon as any connection
  1569. * errors are encountered. Connection recovery is done elsewhere.
  1570. * Discover process only interrogates devices in order to discover the
  1571. * domain.
  1572. */
  1573. int sas_ex_revalidate_domain(struct domain_device *port_dev)
  1574. {
  1575. int res;
  1576. struct domain_device *dev = NULL;
  1577. res = sas_find_bcast_dev(port_dev, &dev);
  1578. if (res)
  1579. goto out;
  1580. if (dev) {
  1581. struct expander_device *ex = &dev->ex_dev;
  1582. int i = 0, phy_id;
  1583. do {
  1584. phy_id = -1;
  1585. res = sas_find_bcast_phy(dev, &phy_id, i);
  1586. if (phy_id == -1)
  1587. break;
  1588. res = sas_rediscover(dev, phy_id);
  1589. i = phy_id + 1;
  1590. } while (i < ex->num_phys);
  1591. }
  1592. out:
  1593. return res;
  1594. }
  1595. int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1596. struct request *req)
  1597. {
  1598. struct domain_device *dev;
  1599. int ret, type = rphy->identify.device_type;
  1600. struct request *rsp = req->next_rq;
  1601. if (!rsp) {
  1602. printk("%s: space for a smp response is missing\n",
  1603. __FUNCTION__);
  1604. return -EINVAL;
  1605. }
  1606. /* seems aic94xx doesn't support */
  1607. if (!rphy) {
  1608. printk("%s: can we send a smp request to a host?\n",
  1609. __FUNCTION__);
  1610. return -EINVAL;
  1611. }
  1612. if (type != SAS_EDGE_EXPANDER_DEVICE &&
  1613. type != SAS_FANOUT_EXPANDER_DEVICE) {
  1614. printk("%s: can we send a smp request to a device?\n",
  1615. __FUNCTION__);
  1616. return -EINVAL;
  1617. }
  1618. dev = sas_find_dev_by_rphy(rphy);
  1619. if (!dev) {
  1620. printk("%s: fail to find a domain_device?\n", __FUNCTION__);
  1621. return -EINVAL;
  1622. }
  1623. /* do we need to support multiple segments? */
  1624. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1625. printk("%s: multiple segments req %u %u, rsp %u %u\n",
  1626. __FUNCTION__, req->bio->bi_vcnt, req->data_len,
  1627. rsp->bio->bi_vcnt, rsp->data_len);
  1628. return -EINVAL;
  1629. }
  1630. ret = smp_execute_task(dev, bio_data(req->bio), req->data_len,
  1631. bio_data(rsp->bio), rsp->data_len);
  1632. return ret;
  1633. }