sas_expander.c 48 KB

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