sas_expander.c 45 KB

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