sas_expander.c 51 KB

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