sas_expander.c 46 KB

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