sas_expander.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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;
  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. /* no rphy means no smp target support (ie aic94xx host) */
  1613. if (!rphy) {
  1614. printk("%s: can we send a smp request to a host?\n",
  1615. __FUNCTION__);
  1616. return -EINVAL;
  1617. }
  1618. type = rphy->identify.device_type;
  1619. if (type != SAS_EDGE_EXPANDER_DEVICE &&
  1620. type != SAS_FANOUT_EXPANDER_DEVICE) {
  1621. printk("%s: can we send a smp request to a device?\n",
  1622. __FUNCTION__);
  1623. return -EINVAL;
  1624. }
  1625. dev = sas_find_dev_by_rphy(rphy);
  1626. if (!dev) {
  1627. printk("%s: fail to find a domain_device?\n", __FUNCTION__);
  1628. return -EINVAL;
  1629. }
  1630. /* do we need to support multiple segments? */
  1631. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1632. printk("%s: multiple segments req %u %u, rsp %u %u\n",
  1633. __FUNCTION__, req->bio->bi_vcnt, req->data_len,
  1634. rsp->bio->bi_vcnt, rsp->data_len);
  1635. return -EINVAL;
  1636. }
  1637. ret = smp_execute_task(dev, bio_data(req->bio), req->data_len,
  1638. bio_data(rsp->bio), rsp->data_len);
  1639. return ret;
  1640. }