sas_expander.c 46 KB

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