sas_expander.c 47 KB

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