sas_expander.c 46 KB

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