sas_expander.c 45 KB

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