sas_expander.c 47 KB

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