sas_expander.c 46 KB

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