sas_expander.c 48 KB

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