sas_expander.c 45 KB

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