sas_expander.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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, blk_rq_bytes(req),
  1647. rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
  1648. return -EINVAL;
  1649. }
  1650. ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
  1651. bio_data(rsp->bio), blk_rq_bytes(rsp));
  1652. if (ret > 0) {
  1653. /* positive number is the untransferred residual */
  1654. rsp->resid_len = ret;
  1655. req->resid_len = 0;
  1656. ret = 0;
  1657. } else if (ret == 0) {
  1658. rsp->resid_len = 0;
  1659. req->resid_len = 0;
  1660. }
  1661. return ret;
  1662. }