sas_expander.c 51 KB

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