sas_expander.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  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 = -ETASK;
  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. } else {
  95. SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
  96. "status 0x%x\n", __FUNCTION__,
  97. SAS_ADDR(dev->sas_addr),
  98. task->task_status.resp,
  99. task->task_status.stat);
  100. sas_free_task(task);
  101. task = NULL;
  102. }
  103. }
  104. ex_err:
  105. BUG_ON(retry == 3 && task != NULL);
  106. if (task != NULL) {
  107. sas_free_task(task);
  108. }
  109. return res;
  110. }
  111. /* ---------- Allocations ---------- */
  112. static inline void *alloc_smp_req(int size)
  113. {
  114. u8 *p = kzalloc(size, GFP_KERNEL);
  115. if (p)
  116. p[0] = SMP_REQUEST;
  117. return p;
  118. }
  119. static inline void *alloc_smp_resp(int size)
  120. {
  121. return kzalloc(size, GFP_KERNEL);
  122. }
  123. /* ---------- Expander configuration ---------- */
  124. static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
  125. void *disc_resp)
  126. {
  127. struct expander_device *ex = &dev->ex_dev;
  128. struct ex_phy *phy = &ex->ex_phy[phy_id];
  129. struct smp_resp *resp = disc_resp;
  130. struct discover_resp *dr = &resp->disc;
  131. struct sas_rphy *rphy = dev->rphy;
  132. int rediscover = (phy->phy != NULL);
  133. if (!rediscover) {
  134. phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
  135. /* FIXME: error_handling */
  136. BUG_ON(!phy->phy);
  137. }
  138. switch (resp->result) {
  139. case SMP_RESP_PHY_VACANT:
  140. phy->phy_state = PHY_VACANT;
  141. return;
  142. default:
  143. phy->phy_state = PHY_NOT_PRESENT;
  144. return;
  145. case SMP_RESP_FUNC_ACC:
  146. phy->phy_state = PHY_EMPTY; /* do not know yet */
  147. break;
  148. }
  149. phy->phy_id = phy_id;
  150. phy->attached_dev_type = dr->attached_dev_type;
  151. phy->linkrate = dr->linkrate;
  152. phy->attached_sata_host = dr->attached_sata_host;
  153. phy->attached_sata_dev = dr->attached_sata_dev;
  154. phy->attached_sata_ps = dr->attached_sata_ps;
  155. phy->attached_iproto = dr->iproto << 1;
  156. phy->attached_tproto = dr->tproto << 1;
  157. memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
  158. phy->attached_phy_id = dr->attached_phy_id;
  159. phy->phy_change_count = dr->change_count;
  160. phy->routing_attr = dr->routing_attr;
  161. phy->virtual = dr->virtual;
  162. phy->last_da_index = -1;
  163. phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
  164. phy->phy->identify.target_port_protocols = phy->attached_tproto;
  165. phy->phy->identify.phy_identifier = phy_id;
  166. phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
  167. phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
  168. phy->phy->minimum_linkrate = dr->pmin_linkrate;
  169. phy->phy->maximum_linkrate = dr->pmax_linkrate;
  170. phy->phy->negotiated_linkrate = phy->linkrate;
  171. if (!rediscover)
  172. sas_phy_add(phy->phy);
  173. SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
  174. SAS_ADDR(dev->sas_addr), phy->phy_id,
  175. phy->routing_attr == TABLE_ROUTING ? 'T' :
  176. phy->routing_attr == DIRECT_ROUTING ? 'D' :
  177. phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
  178. SAS_ADDR(phy->attached_sas_addr));
  179. return;
  180. }
  181. #define DISCOVER_REQ_SIZE 16
  182. #define DISCOVER_RESP_SIZE 56
  183. static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
  184. u8 *disc_resp, int single)
  185. {
  186. int i, res;
  187. disc_req[9] = single;
  188. for (i = 1 ; i < 3; i++) {
  189. struct discover_resp *dr;
  190. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  191. disc_resp, DISCOVER_RESP_SIZE);
  192. if (res)
  193. return res;
  194. /* This is detecting a failure to transmit inital
  195. * dev to host FIS as described in section G.5 of
  196. * sas-2 r 04b */
  197. dr = &((struct smp_resp *)disc_resp)->disc;
  198. if (!(dr->attached_dev_type == 0 &&
  199. dr->attached_sata_dev))
  200. break;
  201. /* In order to generate the dev to host FIS, we
  202. * send a link reset to the expander port */
  203. sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
  204. /* Wait for the reset to trigger the negotiation */
  205. msleep(500);
  206. }
  207. sas_set_ex_phy(dev, single, disc_resp);
  208. return 0;
  209. }
  210. static int sas_ex_phy_discover(struct domain_device *dev, int single)
  211. {
  212. struct expander_device *ex = &dev->ex_dev;
  213. int res = 0;
  214. u8 *disc_req;
  215. u8 *disc_resp;
  216. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  217. if (!disc_req)
  218. return -ENOMEM;
  219. disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
  220. if (!disc_resp) {
  221. kfree(disc_req);
  222. return -ENOMEM;
  223. }
  224. disc_req[1] = SMP_DISCOVER;
  225. if (0 <= single && single < ex->num_phys) {
  226. res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
  227. } else {
  228. int i;
  229. for (i = 0; i < ex->num_phys; i++) {
  230. res = sas_ex_phy_discover_helper(dev, disc_req,
  231. disc_resp, i);
  232. if (res)
  233. goto out_err;
  234. }
  235. }
  236. out_err:
  237. kfree(disc_resp);
  238. kfree(disc_req);
  239. return res;
  240. }
  241. static int sas_expander_discover(struct domain_device *dev)
  242. {
  243. struct expander_device *ex = &dev->ex_dev;
  244. int res = -ENOMEM;
  245. ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
  246. if (!ex->ex_phy)
  247. return -ENOMEM;
  248. res = sas_ex_phy_discover(dev, -1);
  249. if (res)
  250. goto out_err;
  251. return 0;
  252. out_err:
  253. kfree(ex->ex_phy);
  254. ex->ex_phy = NULL;
  255. return res;
  256. }
  257. #define MAX_EXPANDER_PHYS 128
  258. static void ex_assign_report_general(struct domain_device *dev,
  259. struct smp_resp *resp)
  260. {
  261. struct report_general_resp *rg = &resp->rg;
  262. dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
  263. dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
  264. dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
  265. dev->ex_dev.conf_route_table = rg->conf_route_table;
  266. dev->ex_dev.configuring = rg->configuring;
  267. memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
  268. }
  269. #define RG_REQ_SIZE 8
  270. #define RG_RESP_SIZE 32
  271. static int sas_ex_general(struct domain_device *dev)
  272. {
  273. u8 *rg_req;
  274. struct smp_resp *rg_resp;
  275. int res;
  276. int i;
  277. rg_req = alloc_smp_req(RG_REQ_SIZE);
  278. if (!rg_req)
  279. return -ENOMEM;
  280. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  281. if (!rg_resp) {
  282. kfree(rg_req);
  283. return -ENOMEM;
  284. }
  285. rg_req[1] = SMP_REPORT_GENERAL;
  286. for (i = 0; i < 5; i++) {
  287. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  288. RG_RESP_SIZE);
  289. if (res) {
  290. SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
  291. SAS_ADDR(dev->sas_addr), res);
  292. goto out;
  293. } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  294. SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
  295. SAS_ADDR(dev->sas_addr), rg_resp->result);
  296. res = rg_resp->result;
  297. goto out;
  298. }
  299. ex_assign_report_general(dev, rg_resp);
  300. if (dev->ex_dev.configuring) {
  301. SAS_DPRINTK("RG: ex %llx self-configuring...\n",
  302. SAS_ADDR(dev->sas_addr));
  303. schedule_timeout_interruptible(5*HZ);
  304. } else
  305. break;
  306. }
  307. out:
  308. kfree(rg_req);
  309. kfree(rg_resp);
  310. return res;
  311. }
  312. static void ex_assign_manuf_info(struct domain_device *dev, void
  313. *_mi_resp)
  314. {
  315. u8 *mi_resp = _mi_resp;
  316. struct sas_rphy *rphy = dev->rphy;
  317. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  318. memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
  319. memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
  320. memcpy(edev->product_rev, mi_resp + 36,
  321. SAS_EXPANDER_PRODUCT_REV_LEN);
  322. if (mi_resp[8] & 1) {
  323. memcpy(edev->component_vendor_id, mi_resp + 40,
  324. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  325. edev->component_id = mi_resp[48] << 8 | mi_resp[49];
  326. edev->component_revision_id = mi_resp[50];
  327. }
  328. }
  329. #define MI_REQ_SIZE 8
  330. #define MI_RESP_SIZE 64
  331. static int sas_ex_manuf_info(struct domain_device *dev)
  332. {
  333. u8 *mi_req;
  334. u8 *mi_resp;
  335. int res;
  336. mi_req = alloc_smp_req(MI_REQ_SIZE);
  337. if (!mi_req)
  338. return -ENOMEM;
  339. mi_resp = alloc_smp_resp(MI_RESP_SIZE);
  340. if (!mi_resp) {
  341. kfree(mi_req);
  342. return -ENOMEM;
  343. }
  344. mi_req[1] = SMP_REPORT_MANUF_INFO;
  345. res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
  346. if (res) {
  347. SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
  348. SAS_ADDR(dev->sas_addr), res);
  349. goto out;
  350. } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
  351. SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
  352. SAS_ADDR(dev->sas_addr), mi_resp[2]);
  353. goto out;
  354. }
  355. ex_assign_manuf_info(dev, mi_resp);
  356. out:
  357. kfree(mi_req);
  358. kfree(mi_resp);
  359. return res;
  360. }
  361. #define PC_REQ_SIZE 44
  362. #define PC_RESP_SIZE 8
  363. int sas_smp_phy_control(struct domain_device *dev, int phy_id,
  364. enum phy_func phy_func,
  365. struct sas_phy_linkrates *rates)
  366. {
  367. u8 *pc_req;
  368. u8 *pc_resp;
  369. int res;
  370. pc_req = alloc_smp_req(PC_REQ_SIZE);
  371. if (!pc_req)
  372. return -ENOMEM;
  373. pc_resp = alloc_smp_resp(PC_RESP_SIZE);
  374. if (!pc_resp) {
  375. kfree(pc_req);
  376. return -ENOMEM;
  377. }
  378. pc_req[1] = SMP_PHY_CONTROL;
  379. pc_req[9] = phy_id;
  380. pc_req[10]= phy_func;
  381. if (rates) {
  382. pc_req[32] = rates->minimum_linkrate << 4;
  383. pc_req[33] = rates->maximum_linkrate << 4;
  384. }
  385. res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
  386. kfree(pc_resp);
  387. kfree(pc_req);
  388. return res;
  389. }
  390. static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
  391. {
  392. struct expander_device *ex = &dev->ex_dev;
  393. struct ex_phy *phy = &ex->ex_phy[phy_id];
  394. sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
  395. phy->linkrate = SAS_PHY_DISABLED;
  396. }
  397. static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
  398. {
  399. struct expander_device *ex = &dev->ex_dev;
  400. int i;
  401. for (i = 0; i < ex->num_phys; i++) {
  402. struct ex_phy *phy = &ex->ex_phy[i];
  403. if (phy->phy_state == PHY_VACANT ||
  404. phy->phy_state == PHY_NOT_PRESENT)
  405. continue;
  406. if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
  407. sas_ex_disable_phy(dev, i);
  408. }
  409. }
  410. static int sas_dev_present_in_domain(struct asd_sas_port *port,
  411. u8 *sas_addr)
  412. {
  413. struct domain_device *dev;
  414. if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
  415. return 1;
  416. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  417. if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
  418. return 1;
  419. }
  420. return 0;
  421. }
  422. #define RPEL_REQ_SIZE 16
  423. #define RPEL_RESP_SIZE 32
  424. int sas_smp_get_phy_events(struct sas_phy *phy)
  425. {
  426. int res;
  427. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  428. struct domain_device *dev = sas_find_dev_by_rphy(rphy);
  429. u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
  430. u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
  431. if (!resp)
  432. return -ENOMEM;
  433. req[1] = SMP_REPORT_PHY_ERR_LOG;
  434. req[9] = phy->number;
  435. res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
  436. resp, RPEL_RESP_SIZE);
  437. if (!res)
  438. goto out;
  439. phy->invalid_dword_count = scsi_to_u32(&resp[12]);
  440. phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
  441. phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
  442. phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
  443. out:
  444. kfree(resp);
  445. return res;
  446. }
  447. #ifdef CONFIG_SCSI_SAS_ATA
  448. #define RPS_REQ_SIZE 16
  449. #define RPS_RESP_SIZE 60
  450. static int sas_get_report_phy_sata(struct domain_device *dev,
  451. int phy_id,
  452. struct smp_resp *rps_resp)
  453. {
  454. int res;
  455. u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
  456. u8 *resp = (u8 *)rps_resp;
  457. if (!rps_req)
  458. return -ENOMEM;
  459. rps_req[1] = SMP_REPORT_PHY_SATA;
  460. rps_req[9] = phy_id;
  461. res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
  462. rps_resp, RPS_RESP_SIZE);
  463. /* 0x34 is the FIS type for the D2H fis. There's a potential
  464. * standards cockup here. sas-2 explicitly specifies the FIS
  465. * should be encoded so that FIS type is in resp[24].
  466. * However, some expanders endian reverse this. Undo the
  467. * reversal here */
  468. if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
  469. int i;
  470. for (i = 0; i < 5; i++) {
  471. int j = 24 + (i*4);
  472. u8 a, b;
  473. a = resp[j + 0];
  474. b = resp[j + 1];
  475. resp[j + 0] = resp[j + 3];
  476. resp[j + 1] = resp[j + 2];
  477. resp[j + 2] = b;
  478. resp[j + 3] = a;
  479. }
  480. }
  481. kfree(rps_req);
  482. return res;
  483. }
  484. #endif
  485. static void sas_ex_get_linkrate(struct domain_device *parent,
  486. struct domain_device *child,
  487. struct ex_phy *parent_phy)
  488. {
  489. struct expander_device *parent_ex = &parent->ex_dev;
  490. struct sas_port *port;
  491. int i;
  492. child->pathways = 0;
  493. port = parent_phy->port;
  494. for (i = 0; i < parent_ex->num_phys; i++) {
  495. struct ex_phy *phy = &parent_ex->ex_phy[i];
  496. if (phy->phy_state == PHY_VACANT ||
  497. phy->phy_state == PHY_NOT_PRESENT)
  498. continue;
  499. if (SAS_ADDR(phy->attached_sas_addr) ==
  500. SAS_ADDR(child->sas_addr)) {
  501. child->min_linkrate = min(parent->min_linkrate,
  502. phy->linkrate);
  503. child->max_linkrate = max(parent->max_linkrate,
  504. phy->linkrate);
  505. child->pathways++;
  506. sas_port_add_phy(port, phy->phy);
  507. }
  508. }
  509. child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
  510. child->pathways = min(child->pathways, parent->pathways);
  511. }
  512. static struct domain_device *sas_ex_discover_end_dev(
  513. struct domain_device *parent, int phy_id)
  514. {
  515. struct expander_device *parent_ex = &parent->ex_dev;
  516. struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
  517. struct domain_device *child = NULL;
  518. struct sas_rphy *rphy;
  519. int res;
  520. if (phy->attached_sata_host || phy->attached_sata_ps)
  521. return NULL;
  522. child = kzalloc(sizeof(*child), GFP_KERNEL);
  523. if (!child)
  524. return NULL;
  525. child->parent = parent;
  526. child->port = parent->port;
  527. child->iproto = phy->attached_iproto;
  528. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  529. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  530. if (!phy->port) {
  531. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  532. if (unlikely(!phy->port))
  533. goto out_err;
  534. if (unlikely(sas_port_add(phy->port) != 0)) {
  535. sas_port_free(phy->port);
  536. goto out_err;
  537. }
  538. }
  539. sas_ex_get_linkrate(parent, child, phy);
  540. #ifdef CONFIG_SCSI_SAS_ATA
  541. if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
  542. child->dev_type = SATA_DEV;
  543. if (phy->attached_tproto & SAS_PROTO_STP)
  544. child->tproto = phy->attached_tproto;
  545. if (phy->attached_sata_dev)
  546. child->tproto |= SATA_DEV;
  547. res = sas_get_report_phy_sata(parent, phy_id,
  548. &child->sata_dev.rps_resp);
  549. if (res) {
  550. SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
  551. "0x%x\n", SAS_ADDR(parent->sas_addr),
  552. phy_id, res);
  553. goto out_free;
  554. }
  555. memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
  556. sizeof(struct dev_to_host_fis));
  557. rphy = sas_end_device_alloc(phy->port);
  558. if (unlikely(!rphy))
  559. goto out_free;
  560. sas_init_dev(child);
  561. child->rphy = rphy;
  562. spin_lock_irq(&parent->port->dev_list_lock);
  563. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  564. spin_unlock_irq(&parent->port->dev_list_lock);
  565. res = sas_discover_sata(child);
  566. if (res) {
  567. SAS_DPRINTK("sas_discover_sata() for device %16llx at "
  568. "%016llx:0x%x returned 0x%x\n",
  569. SAS_ADDR(child->sas_addr),
  570. SAS_ADDR(parent->sas_addr), phy_id, res);
  571. goto out_list_del;
  572. }
  573. } else
  574. #endif
  575. if (phy->attached_tproto & SAS_PROTO_SSP) {
  576. child->dev_type = SAS_END_DEV;
  577. rphy = sas_end_device_alloc(phy->port);
  578. /* FIXME: error handling */
  579. if (unlikely(!rphy))
  580. goto out_free;
  581. child->tproto = phy->attached_tproto;
  582. sas_init_dev(child);
  583. child->rphy = rphy;
  584. sas_fill_in_rphy(child, rphy);
  585. spin_lock_irq(&parent->port->dev_list_lock);
  586. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  587. spin_unlock_irq(&parent->port->dev_list_lock);
  588. res = sas_discover_end_dev(child);
  589. if (res) {
  590. SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
  591. "at %016llx:0x%x returned 0x%x\n",
  592. SAS_ADDR(child->sas_addr),
  593. SAS_ADDR(parent->sas_addr), phy_id, res);
  594. goto out_list_del;
  595. }
  596. } else {
  597. SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
  598. phy->attached_tproto, SAS_ADDR(parent->sas_addr),
  599. phy_id);
  600. goto out_free;
  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_irq(&parent->port->dev_list_lock);
  688. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  689. spin_unlock_irq(&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. /**
  1205. * sas_discover_expander -- expander discovery
  1206. * @ex: pointer to expander domain device
  1207. *
  1208. * See comment in sas_discover_sata().
  1209. */
  1210. static int sas_discover_expander(struct domain_device *dev)
  1211. {
  1212. int res;
  1213. res = sas_notify_lldd_dev_found(dev);
  1214. if (res)
  1215. return res;
  1216. res = sas_ex_general(dev);
  1217. if (res)
  1218. goto out_err;
  1219. res = sas_ex_manuf_info(dev);
  1220. if (res)
  1221. goto out_err;
  1222. res = sas_expander_discover(dev);
  1223. if (res) {
  1224. SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
  1225. SAS_ADDR(dev->sas_addr), res);
  1226. goto out_err;
  1227. }
  1228. sas_check_ex_subtractive_boundary(dev);
  1229. res = sas_check_parent_topology(dev);
  1230. if (res)
  1231. goto out_err;
  1232. return 0;
  1233. out_err:
  1234. sas_notify_lldd_dev_gone(dev);
  1235. return res;
  1236. }
  1237. static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
  1238. {
  1239. int res = 0;
  1240. struct domain_device *dev;
  1241. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  1242. if (dev->dev_type == EDGE_DEV ||
  1243. dev->dev_type == FANOUT_DEV) {
  1244. struct sas_expander_device *ex =
  1245. rphy_to_expander_device(dev->rphy);
  1246. if (level == ex->level)
  1247. res = sas_ex_discover_devices(dev, -1);
  1248. else if (level > 0)
  1249. res = sas_ex_discover_devices(port->port_dev, -1);
  1250. }
  1251. }
  1252. return res;
  1253. }
  1254. static int sas_ex_bfs_disc(struct asd_sas_port *port)
  1255. {
  1256. int res;
  1257. int level;
  1258. do {
  1259. level = port->disc.max_level;
  1260. res = sas_ex_level_discovery(port, level);
  1261. mb();
  1262. } while (level < port->disc.max_level);
  1263. return res;
  1264. }
  1265. int sas_discover_root_expander(struct domain_device *dev)
  1266. {
  1267. int res;
  1268. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1269. res = sas_rphy_add(dev->rphy);
  1270. if (res)
  1271. goto out_err;
  1272. ex->level = dev->port->disc.max_level; /* 0 */
  1273. res = sas_discover_expander(dev);
  1274. if (res)
  1275. goto out_err2;
  1276. sas_ex_bfs_disc(dev->port);
  1277. return res;
  1278. out_err2:
  1279. sas_rphy_remove(dev->rphy);
  1280. out_err:
  1281. return res;
  1282. }
  1283. /* ---------- Domain revalidation ---------- */
  1284. static int sas_get_phy_discover(struct domain_device *dev,
  1285. int phy_id, struct smp_resp *disc_resp)
  1286. {
  1287. int res;
  1288. u8 *disc_req;
  1289. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  1290. if (!disc_req)
  1291. return -ENOMEM;
  1292. disc_req[1] = SMP_DISCOVER;
  1293. disc_req[9] = phy_id;
  1294. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  1295. disc_resp, DISCOVER_RESP_SIZE);
  1296. if (res)
  1297. goto out;
  1298. else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
  1299. res = disc_resp->result;
  1300. goto out;
  1301. }
  1302. out:
  1303. kfree(disc_req);
  1304. return res;
  1305. }
  1306. static int sas_get_phy_change_count(struct domain_device *dev,
  1307. int phy_id, int *pcc)
  1308. {
  1309. int res;
  1310. struct smp_resp *disc_resp;
  1311. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1312. if (!disc_resp)
  1313. return -ENOMEM;
  1314. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1315. if (!res)
  1316. *pcc = disc_resp->disc.change_count;
  1317. kfree(disc_resp);
  1318. return res;
  1319. }
  1320. static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
  1321. int phy_id, u8 *attached_sas_addr)
  1322. {
  1323. int res;
  1324. struct smp_resp *disc_resp;
  1325. struct discover_resp *dr;
  1326. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1327. if (!disc_resp)
  1328. return -ENOMEM;
  1329. dr = &disc_resp->disc;
  1330. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1331. if (!res) {
  1332. memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
  1333. if (dr->attached_dev_type == 0)
  1334. memset(attached_sas_addr, 0, 8);
  1335. }
  1336. kfree(disc_resp);
  1337. return res;
  1338. }
  1339. static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
  1340. int from_phy)
  1341. {
  1342. struct expander_device *ex = &dev->ex_dev;
  1343. int res = 0;
  1344. int i;
  1345. for (i = from_phy; i < ex->num_phys; i++) {
  1346. int phy_change_count = 0;
  1347. res = sas_get_phy_change_count(dev, i, &phy_change_count);
  1348. if (res)
  1349. goto out;
  1350. else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
  1351. ex->ex_phy[i].phy_change_count = phy_change_count;
  1352. *phy_id = i;
  1353. return 0;
  1354. }
  1355. }
  1356. out:
  1357. return res;
  1358. }
  1359. static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
  1360. {
  1361. int res;
  1362. u8 *rg_req;
  1363. struct smp_resp *rg_resp;
  1364. rg_req = alloc_smp_req(RG_REQ_SIZE);
  1365. if (!rg_req)
  1366. return -ENOMEM;
  1367. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  1368. if (!rg_resp) {
  1369. kfree(rg_req);
  1370. return -ENOMEM;
  1371. }
  1372. rg_req[1] = SMP_REPORT_GENERAL;
  1373. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  1374. RG_RESP_SIZE);
  1375. if (res)
  1376. goto out;
  1377. if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  1378. res = rg_resp->result;
  1379. goto out;
  1380. }
  1381. *ecc = be16_to_cpu(rg_resp->rg.change_count);
  1382. out:
  1383. kfree(rg_resp);
  1384. kfree(rg_req);
  1385. return res;
  1386. }
  1387. static int sas_find_bcast_dev(struct domain_device *dev,
  1388. struct domain_device **src_dev)
  1389. {
  1390. struct expander_device *ex = &dev->ex_dev;
  1391. int ex_change_count = -1;
  1392. int res;
  1393. res = sas_get_ex_change_count(dev, &ex_change_count);
  1394. if (res)
  1395. goto out;
  1396. if (ex_change_count != -1 &&
  1397. ex_change_count != ex->ex_change_count) {
  1398. *src_dev = dev;
  1399. ex->ex_change_count = ex_change_count;
  1400. } else {
  1401. struct domain_device *ch;
  1402. list_for_each_entry(ch, &ex->children, siblings) {
  1403. if (ch->dev_type == EDGE_DEV ||
  1404. ch->dev_type == FANOUT_DEV) {
  1405. res = sas_find_bcast_dev(ch, src_dev);
  1406. if (src_dev)
  1407. return res;
  1408. }
  1409. }
  1410. }
  1411. out:
  1412. return res;
  1413. }
  1414. static void sas_unregister_ex_tree(struct domain_device *dev)
  1415. {
  1416. struct expander_device *ex = &dev->ex_dev;
  1417. struct domain_device *child, *n;
  1418. list_for_each_entry_safe(child, n, &ex->children, siblings) {
  1419. if (child->dev_type == EDGE_DEV ||
  1420. child->dev_type == FANOUT_DEV)
  1421. sas_unregister_ex_tree(child);
  1422. else
  1423. sas_unregister_dev(child);
  1424. }
  1425. sas_unregister_dev(dev);
  1426. }
  1427. static void sas_unregister_devs_sas_addr(struct domain_device *parent,
  1428. int phy_id)
  1429. {
  1430. struct expander_device *ex_dev = &parent->ex_dev;
  1431. struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
  1432. struct domain_device *child, *n;
  1433. list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
  1434. if (SAS_ADDR(child->sas_addr) ==
  1435. SAS_ADDR(phy->attached_sas_addr)) {
  1436. if (child->dev_type == EDGE_DEV ||
  1437. child->dev_type == FANOUT_DEV)
  1438. sas_unregister_ex_tree(child);
  1439. else
  1440. sas_unregister_dev(child);
  1441. break;
  1442. }
  1443. }
  1444. sas_disable_routing(parent, phy->attached_sas_addr);
  1445. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  1446. sas_port_delete_phy(phy->port, phy->phy);
  1447. if (phy->port->num_phys == 0)
  1448. sas_port_delete(phy->port);
  1449. phy->port = NULL;
  1450. }
  1451. static int sas_discover_bfs_by_root_level(struct domain_device *root,
  1452. const int level)
  1453. {
  1454. struct expander_device *ex_root = &root->ex_dev;
  1455. struct domain_device *child;
  1456. int res = 0;
  1457. list_for_each_entry(child, &ex_root->children, siblings) {
  1458. if (child->dev_type == EDGE_DEV ||
  1459. child->dev_type == FANOUT_DEV) {
  1460. struct sas_expander_device *ex =
  1461. rphy_to_expander_device(child->rphy);
  1462. if (level > ex->level)
  1463. res = sas_discover_bfs_by_root_level(child,
  1464. level);
  1465. else if (level == ex->level)
  1466. res = sas_ex_discover_devices(child, -1);
  1467. }
  1468. }
  1469. return res;
  1470. }
  1471. static int sas_discover_bfs_by_root(struct domain_device *dev)
  1472. {
  1473. int res;
  1474. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1475. int level = ex->level+1;
  1476. res = sas_ex_discover_devices(dev, -1);
  1477. if (res)
  1478. goto out;
  1479. do {
  1480. res = sas_discover_bfs_by_root_level(dev, level);
  1481. mb();
  1482. level += 1;
  1483. } while (level <= dev->port->disc.max_level);
  1484. out:
  1485. return res;
  1486. }
  1487. static int sas_discover_new(struct domain_device *dev, int phy_id)
  1488. {
  1489. struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
  1490. struct domain_device *child;
  1491. int res;
  1492. SAS_DPRINTK("ex %016llx phy%d new device attached\n",
  1493. SAS_ADDR(dev->sas_addr), phy_id);
  1494. res = sas_ex_phy_discover(dev, phy_id);
  1495. if (res)
  1496. goto out;
  1497. res = sas_ex_discover_devices(dev, phy_id);
  1498. if (res)
  1499. goto out;
  1500. list_for_each_entry(child, &dev->ex_dev.children, siblings) {
  1501. if (SAS_ADDR(child->sas_addr) ==
  1502. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1503. if (child->dev_type == EDGE_DEV ||
  1504. child->dev_type == FANOUT_DEV)
  1505. res = sas_discover_bfs_by_root(child);
  1506. break;
  1507. }
  1508. }
  1509. out:
  1510. return res;
  1511. }
  1512. static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
  1513. {
  1514. struct expander_device *ex = &dev->ex_dev;
  1515. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1516. u8 attached_sas_addr[8];
  1517. int res;
  1518. res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
  1519. switch (res) {
  1520. case SMP_RESP_NO_PHY:
  1521. phy->phy_state = PHY_NOT_PRESENT;
  1522. sas_unregister_devs_sas_addr(dev, phy_id);
  1523. goto out; break;
  1524. case SMP_RESP_PHY_VACANT:
  1525. phy->phy_state = PHY_VACANT;
  1526. sas_unregister_devs_sas_addr(dev, phy_id);
  1527. goto out; break;
  1528. case SMP_RESP_FUNC_ACC:
  1529. break;
  1530. }
  1531. if (SAS_ADDR(attached_sas_addr) == 0) {
  1532. phy->phy_state = PHY_EMPTY;
  1533. sas_unregister_devs_sas_addr(dev, phy_id);
  1534. } else if (SAS_ADDR(attached_sas_addr) ==
  1535. SAS_ADDR(phy->attached_sas_addr)) {
  1536. SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
  1537. SAS_ADDR(dev->sas_addr), phy_id);
  1538. sas_ex_phy_discover(dev, phy_id);
  1539. } else
  1540. res = sas_discover_new(dev, phy_id);
  1541. out:
  1542. return res;
  1543. }
  1544. static int sas_rediscover(struct domain_device *dev, const int phy_id)
  1545. {
  1546. struct expander_device *ex = &dev->ex_dev;
  1547. struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
  1548. int res = 0;
  1549. int i;
  1550. SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
  1551. SAS_ADDR(dev->sas_addr), phy_id);
  1552. if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
  1553. for (i = 0; i < ex->num_phys; i++) {
  1554. struct ex_phy *phy = &ex->ex_phy[i];
  1555. if (i == phy_id)
  1556. continue;
  1557. if (SAS_ADDR(phy->attached_sas_addr) ==
  1558. SAS_ADDR(changed_phy->attached_sas_addr)) {
  1559. SAS_DPRINTK("phy%d part of wide port with "
  1560. "phy%d\n", phy_id, i);
  1561. goto out;
  1562. }
  1563. }
  1564. res = sas_rediscover_dev(dev, phy_id);
  1565. } else
  1566. res = sas_discover_new(dev, phy_id);
  1567. out:
  1568. return res;
  1569. }
  1570. /**
  1571. * sas_revalidate_domain -- revalidate the domain
  1572. * @port: port to the domain of interest
  1573. *
  1574. * NOTE: this process _must_ quit (return) as soon as any connection
  1575. * errors are encountered. Connection recovery is done elsewhere.
  1576. * Discover process only interrogates devices in order to discover the
  1577. * domain.
  1578. */
  1579. int sas_ex_revalidate_domain(struct domain_device *port_dev)
  1580. {
  1581. int res;
  1582. struct domain_device *dev = NULL;
  1583. res = sas_find_bcast_dev(port_dev, &dev);
  1584. if (res)
  1585. goto out;
  1586. if (dev) {
  1587. struct expander_device *ex = &dev->ex_dev;
  1588. int i = 0, phy_id;
  1589. do {
  1590. phy_id = -1;
  1591. res = sas_find_bcast_phy(dev, &phy_id, i);
  1592. if (phy_id == -1)
  1593. break;
  1594. res = sas_rediscover(dev, phy_id);
  1595. i = phy_id + 1;
  1596. } while (i < ex->num_phys);
  1597. }
  1598. out:
  1599. return res;
  1600. }
  1601. int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1602. struct request *req)
  1603. {
  1604. struct domain_device *dev;
  1605. int ret, type = rphy->identify.device_type;
  1606. struct request *rsp = req->next_rq;
  1607. if (!rsp) {
  1608. printk("%s: space for a smp response is missing\n",
  1609. __FUNCTION__);
  1610. return -EINVAL;
  1611. }
  1612. /* seems aic94xx doesn't support */
  1613. if (!rphy) {
  1614. printk("%s: can we send a smp request to a host?\n",
  1615. __FUNCTION__);
  1616. return -EINVAL;
  1617. }
  1618. if (type != SAS_EDGE_EXPANDER_DEVICE &&
  1619. type != SAS_FANOUT_EXPANDER_DEVICE) {
  1620. printk("%s: can we send a smp request to a device?\n",
  1621. __FUNCTION__);
  1622. return -EINVAL;
  1623. }
  1624. dev = sas_find_dev_by_rphy(rphy);
  1625. if (!dev) {
  1626. printk("%s: fail to find a domain_device?\n", __FUNCTION__);
  1627. return -EINVAL;
  1628. }
  1629. /* do we need to support multiple segments? */
  1630. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1631. printk("%s: multiple segments req %u %u, rsp %u %u\n",
  1632. __FUNCTION__, req->bio->bi_vcnt, req->data_len,
  1633. rsp->bio->bi_vcnt, rsp->data_len);
  1634. return -EINVAL;
  1635. }
  1636. ret = smp_execute_task(dev, bio_data(req->bio), req->data_len,
  1637. bio_data(rsp->bio), rsp->data_len);
  1638. return ret;
  1639. }