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