sas_expander.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  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 initial
  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. spin_lock_irq(&parent->port->dev_list_lock);
  719. list_del(&child->dev_list_node);
  720. spin_unlock_irq(&parent->port->dev_list_lock);
  721. kfree(child);
  722. return NULL;
  723. }
  724. list_add_tail(&child->siblings, &parent->ex_dev.children);
  725. return child;
  726. }
  727. static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
  728. {
  729. struct expander_device *ex = &dev->ex_dev;
  730. struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
  731. struct domain_device *child = NULL;
  732. int res = 0;
  733. /* Phy state */
  734. if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
  735. if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
  736. res = sas_ex_phy_discover(dev, phy_id);
  737. if (res)
  738. return res;
  739. }
  740. /* Parent and domain coherency */
  741. if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  742. SAS_ADDR(dev->port->sas_addr))) {
  743. sas_add_parent_port(dev, phy_id);
  744. return 0;
  745. }
  746. if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  747. SAS_ADDR(dev->parent->sas_addr))) {
  748. sas_add_parent_port(dev, phy_id);
  749. if (ex_phy->routing_attr == TABLE_ROUTING)
  750. sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
  751. return 0;
  752. }
  753. if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
  754. sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
  755. if (ex_phy->attached_dev_type == NO_DEVICE) {
  756. if (ex_phy->routing_attr == DIRECT_ROUTING) {
  757. memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  758. sas_configure_routing(dev, ex_phy->attached_sas_addr);
  759. }
  760. return 0;
  761. } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
  762. return 0;
  763. if (ex_phy->attached_dev_type != SAS_END_DEV &&
  764. ex_phy->attached_dev_type != FANOUT_DEV &&
  765. ex_phy->attached_dev_type != EDGE_DEV) {
  766. SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
  767. "phy 0x%x\n", ex_phy->attached_dev_type,
  768. SAS_ADDR(dev->sas_addr),
  769. phy_id);
  770. return 0;
  771. }
  772. res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
  773. if (res) {
  774. SAS_DPRINTK("configure routing for dev %016llx "
  775. "reported 0x%x. Forgotten\n",
  776. SAS_ADDR(ex_phy->attached_sas_addr), res);
  777. sas_disable_routing(dev, ex_phy->attached_sas_addr);
  778. return res;
  779. }
  780. res = sas_ex_join_wide_port(dev, phy_id);
  781. if (!res) {
  782. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  783. phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
  784. return res;
  785. }
  786. switch (ex_phy->attached_dev_type) {
  787. case SAS_END_DEV:
  788. child = sas_ex_discover_end_dev(dev, phy_id);
  789. break;
  790. case FANOUT_DEV:
  791. if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
  792. SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
  793. "attached to ex %016llx phy 0x%x\n",
  794. SAS_ADDR(ex_phy->attached_sas_addr),
  795. ex_phy->attached_phy_id,
  796. SAS_ADDR(dev->sas_addr),
  797. phy_id);
  798. sas_ex_disable_phy(dev, phy_id);
  799. break;
  800. } else
  801. memcpy(dev->port->disc.fanout_sas_addr,
  802. ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
  803. /* fallthrough */
  804. case EDGE_DEV:
  805. child = sas_ex_discover_expander(dev, phy_id);
  806. break;
  807. default:
  808. break;
  809. }
  810. if (child) {
  811. int i;
  812. for (i = 0; i < ex->num_phys; i++) {
  813. if (ex->ex_phy[i].phy_state == PHY_VACANT ||
  814. ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
  815. continue;
  816. /*
  817. * Due to races, the phy might not get added to the
  818. * wide port, so we add the phy to the wide port here.
  819. */
  820. if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
  821. SAS_ADDR(child->sas_addr)) {
  822. ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
  823. res = sas_ex_join_wide_port(dev, i);
  824. if (!res)
  825. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  826. i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
  827. }
  828. }
  829. }
  830. return res;
  831. }
  832. static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
  833. {
  834. struct expander_device *ex = &dev->ex_dev;
  835. int i;
  836. for (i = 0; i < ex->num_phys; i++) {
  837. struct ex_phy *phy = &ex->ex_phy[i];
  838. if (phy->phy_state == PHY_VACANT ||
  839. phy->phy_state == PHY_NOT_PRESENT)
  840. continue;
  841. if ((phy->attached_dev_type == EDGE_DEV ||
  842. phy->attached_dev_type == FANOUT_DEV) &&
  843. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  844. memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
  845. return 1;
  846. }
  847. }
  848. return 0;
  849. }
  850. static int sas_check_level_subtractive_boundary(struct domain_device *dev)
  851. {
  852. struct expander_device *ex = &dev->ex_dev;
  853. struct domain_device *child;
  854. u8 sub_addr[8] = {0, };
  855. list_for_each_entry(child, &ex->children, siblings) {
  856. if (child->dev_type != EDGE_DEV &&
  857. child->dev_type != FANOUT_DEV)
  858. continue;
  859. if (sub_addr[0] == 0) {
  860. sas_find_sub_addr(child, sub_addr);
  861. continue;
  862. } else {
  863. u8 s2[8];
  864. if (sas_find_sub_addr(child, s2) &&
  865. (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
  866. SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
  867. "diverges from subtractive "
  868. "boundary %016llx\n",
  869. SAS_ADDR(dev->sas_addr),
  870. SAS_ADDR(child->sas_addr),
  871. SAS_ADDR(s2),
  872. SAS_ADDR(sub_addr));
  873. sas_ex_disable_port(child, s2);
  874. }
  875. }
  876. }
  877. return 0;
  878. }
  879. /**
  880. * sas_ex_discover_devices -- discover devices attached to this expander
  881. * dev: pointer to the expander domain device
  882. * single: if you want to do a single phy, else set to -1;
  883. *
  884. * Configure this expander for use with its devices and register the
  885. * devices of this expander.
  886. */
  887. static int sas_ex_discover_devices(struct domain_device *dev, int single)
  888. {
  889. struct expander_device *ex = &dev->ex_dev;
  890. int i = 0, end = ex->num_phys;
  891. int res = 0;
  892. if (0 <= single && single < end) {
  893. i = single;
  894. end = i+1;
  895. }
  896. for ( ; i < end; i++) {
  897. struct ex_phy *ex_phy = &ex->ex_phy[i];
  898. if (ex_phy->phy_state == PHY_VACANT ||
  899. ex_phy->phy_state == PHY_NOT_PRESENT ||
  900. ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
  901. continue;
  902. switch (ex_phy->linkrate) {
  903. case SAS_PHY_DISABLED:
  904. case SAS_PHY_RESET_PROBLEM:
  905. case SAS_SATA_PORT_SELECTOR:
  906. continue;
  907. default:
  908. res = sas_ex_discover_dev(dev, i);
  909. if (res)
  910. break;
  911. continue;
  912. }
  913. }
  914. if (!res)
  915. sas_check_level_subtractive_boundary(dev);
  916. return res;
  917. }
  918. static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
  919. {
  920. struct expander_device *ex = &dev->ex_dev;
  921. int i;
  922. u8 *sub_sas_addr = NULL;
  923. if (dev->dev_type != EDGE_DEV)
  924. return 0;
  925. for (i = 0; i < ex->num_phys; i++) {
  926. struct ex_phy *phy = &ex->ex_phy[i];
  927. if (phy->phy_state == PHY_VACANT ||
  928. phy->phy_state == PHY_NOT_PRESENT)
  929. continue;
  930. if ((phy->attached_dev_type == FANOUT_DEV ||
  931. phy->attached_dev_type == EDGE_DEV) &&
  932. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  933. if (!sub_sas_addr)
  934. sub_sas_addr = &phy->attached_sas_addr[0];
  935. else if (SAS_ADDR(sub_sas_addr) !=
  936. SAS_ADDR(phy->attached_sas_addr)) {
  937. SAS_DPRINTK("ex %016llx phy 0x%x "
  938. "diverges(%016llx) on subtractive "
  939. "boundary(%016llx). Disabled\n",
  940. SAS_ADDR(dev->sas_addr), i,
  941. SAS_ADDR(phy->attached_sas_addr),
  942. SAS_ADDR(sub_sas_addr));
  943. sas_ex_disable_phy(dev, i);
  944. }
  945. }
  946. }
  947. return 0;
  948. }
  949. static void sas_print_parent_topology_bug(struct domain_device *child,
  950. struct ex_phy *parent_phy,
  951. struct ex_phy *child_phy)
  952. {
  953. static const char ra_char[] = {
  954. [DIRECT_ROUTING] = 'D',
  955. [SUBTRACTIVE_ROUTING] = 'S',
  956. [TABLE_ROUTING] = 'T',
  957. };
  958. static const char *ex_type[] = {
  959. [EDGE_DEV] = "edge",
  960. [FANOUT_DEV] = "fanout",
  961. };
  962. struct domain_device *parent = child->parent;
  963. sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
  964. "has %c:%c routing link!\n",
  965. ex_type[parent->dev_type],
  966. SAS_ADDR(parent->sas_addr),
  967. parent_phy->phy_id,
  968. ex_type[child->dev_type],
  969. SAS_ADDR(child->sas_addr),
  970. child_phy->phy_id,
  971. ra_char[parent_phy->routing_attr],
  972. ra_char[child_phy->routing_attr]);
  973. }
  974. static int sas_check_eeds(struct domain_device *child,
  975. struct ex_phy *parent_phy,
  976. struct ex_phy *child_phy)
  977. {
  978. int res = 0;
  979. struct domain_device *parent = child->parent;
  980. if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
  981. res = -ENODEV;
  982. SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
  983. "phy S:0x%x, while there is a fanout ex %016llx\n",
  984. SAS_ADDR(parent->sas_addr),
  985. parent_phy->phy_id,
  986. SAS_ADDR(child->sas_addr),
  987. child_phy->phy_id,
  988. SAS_ADDR(parent->port->disc.fanout_sas_addr));
  989. } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
  990. memcpy(parent->port->disc.eeds_a, parent->sas_addr,
  991. SAS_ADDR_SIZE);
  992. memcpy(parent->port->disc.eeds_b, child->sas_addr,
  993. SAS_ADDR_SIZE);
  994. } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
  995. SAS_ADDR(parent->sas_addr)) ||
  996. (SAS_ADDR(parent->port->disc.eeds_a) ==
  997. SAS_ADDR(child->sas_addr)))
  998. &&
  999. ((SAS_ADDR(parent->port->disc.eeds_b) ==
  1000. SAS_ADDR(parent->sas_addr)) ||
  1001. (SAS_ADDR(parent->port->disc.eeds_b) ==
  1002. SAS_ADDR(child->sas_addr))))
  1003. ;
  1004. else {
  1005. res = -ENODEV;
  1006. SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
  1007. "phy 0x%x link forms a third EEDS!\n",
  1008. SAS_ADDR(parent->sas_addr),
  1009. parent_phy->phy_id,
  1010. SAS_ADDR(child->sas_addr),
  1011. child_phy->phy_id);
  1012. }
  1013. return res;
  1014. }
  1015. /* Here we spill over 80 columns. It is intentional.
  1016. */
  1017. static int sas_check_parent_topology(struct domain_device *child)
  1018. {
  1019. struct expander_device *child_ex = &child->ex_dev;
  1020. struct expander_device *parent_ex;
  1021. int i;
  1022. int res = 0;
  1023. if (!child->parent)
  1024. return 0;
  1025. if (child->parent->dev_type != EDGE_DEV &&
  1026. child->parent->dev_type != FANOUT_DEV)
  1027. return 0;
  1028. parent_ex = &child->parent->ex_dev;
  1029. for (i = 0; i < parent_ex->num_phys; i++) {
  1030. struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
  1031. struct ex_phy *child_phy;
  1032. if (parent_phy->phy_state == PHY_VACANT ||
  1033. parent_phy->phy_state == PHY_NOT_PRESENT)
  1034. continue;
  1035. if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
  1036. continue;
  1037. child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
  1038. switch (child->parent->dev_type) {
  1039. case EDGE_DEV:
  1040. if (child->dev_type == FANOUT_DEV) {
  1041. if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
  1042. child_phy->routing_attr != TABLE_ROUTING) {
  1043. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1044. res = -ENODEV;
  1045. }
  1046. } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1047. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1048. res = sas_check_eeds(child, parent_phy, child_phy);
  1049. } else if (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 == TABLE_ROUTING &&
  1054. child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
  1055. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1056. res = -ENODEV;
  1057. }
  1058. break;
  1059. case FANOUT_DEV:
  1060. if (parent_phy->routing_attr != TABLE_ROUTING ||
  1061. child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
  1062. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1063. res = -ENODEV;
  1064. }
  1065. break;
  1066. default:
  1067. break;
  1068. }
  1069. }
  1070. return res;
  1071. }
  1072. #define RRI_REQ_SIZE 16
  1073. #define RRI_RESP_SIZE 44
  1074. static int sas_configure_present(struct domain_device *dev, int phy_id,
  1075. u8 *sas_addr, int *index, int *present)
  1076. {
  1077. int i, res = 0;
  1078. struct expander_device *ex = &dev->ex_dev;
  1079. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1080. u8 *rri_req;
  1081. u8 *rri_resp;
  1082. *present = 0;
  1083. *index = 0;
  1084. rri_req = alloc_smp_req(RRI_REQ_SIZE);
  1085. if (!rri_req)
  1086. return -ENOMEM;
  1087. rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
  1088. if (!rri_resp) {
  1089. kfree(rri_req);
  1090. return -ENOMEM;
  1091. }
  1092. rri_req[1] = SMP_REPORT_ROUTE_INFO;
  1093. rri_req[9] = phy_id;
  1094. for (i = 0; i < ex->max_route_indexes ; i++) {
  1095. *(__be16 *)(rri_req+6) = cpu_to_be16(i);
  1096. res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
  1097. RRI_RESP_SIZE);
  1098. if (res)
  1099. goto out;
  1100. res = rri_resp[2];
  1101. if (res == SMP_RESP_NO_INDEX) {
  1102. SAS_DPRINTK("overflow of indexes: dev %016llx "
  1103. "phy 0x%x index 0x%x\n",
  1104. SAS_ADDR(dev->sas_addr), phy_id, i);
  1105. goto out;
  1106. } else if (res != SMP_RESP_FUNC_ACC) {
  1107. SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
  1108. "result 0x%x\n", __func__,
  1109. SAS_ADDR(dev->sas_addr), phy_id, i, res);
  1110. goto out;
  1111. }
  1112. if (SAS_ADDR(sas_addr) != 0) {
  1113. if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
  1114. *index = i;
  1115. if ((rri_resp[12] & 0x80) == 0x80)
  1116. *present = 0;
  1117. else
  1118. *present = 1;
  1119. goto out;
  1120. } else if (SAS_ADDR(rri_resp+16) == 0) {
  1121. *index = i;
  1122. *present = 0;
  1123. goto out;
  1124. }
  1125. } else if (SAS_ADDR(rri_resp+16) == 0 &&
  1126. phy->last_da_index < i) {
  1127. phy->last_da_index = i;
  1128. *index = i;
  1129. *present = 0;
  1130. goto out;
  1131. }
  1132. }
  1133. res = -1;
  1134. out:
  1135. kfree(rri_req);
  1136. kfree(rri_resp);
  1137. return res;
  1138. }
  1139. #define CRI_REQ_SIZE 44
  1140. #define CRI_RESP_SIZE 8
  1141. static int sas_configure_set(struct domain_device *dev, int phy_id,
  1142. u8 *sas_addr, int index, int include)
  1143. {
  1144. int res;
  1145. u8 *cri_req;
  1146. u8 *cri_resp;
  1147. cri_req = alloc_smp_req(CRI_REQ_SIZE);
  1148. if (!cri_req)
  1149. return -ENOMEM;
  1150. cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
  1151. if (!cri_resp) {
  1152. kfree(cri_req);
  1153. return -ENOMEM;
  1154. }
  1155. cri_req[1] = SMP_CONF_ROUTE_INFO;
  1156. *(__be16 *)(cri_req+6) = cpu_to_be16(index);
  1157. cri_req[9] = phy_id;
  1158. if (SAS_ADDR(sas_addr) == 0 || !include)
  1159. cri_req[12] |= 0x80;
  1160. memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
  1161. res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
  1162. CRI_RESP_SIZE);
  1163. if (res)
  1164. goto out;
  1165. res = cri_resp[2];
  1166. if (res == SMP_RESP_NO_INDEX) {
  1167. SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
  1168. "index 0x%x\n",
  1169. SAS_ADDR(dev->sas_addr), phy_id, index);
  1170. }
  1171. out:
  1172. kfree(cri_req);
  1173. kfree(cri_resp);
  1174. return res;
  1175. }
  1176. static int sas_configure_phy(struct domain_device *dev, int phy_id,
  1177. u8 *sas_addr, int include)
  1178. {
  1179. int index;
  1180. int present;
  1181. int res;
  1182. res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
  1183. if (res)
  1184. return res;
  1185. if (include ^ present)
  1186. return sas_configure_set(dev, phy_id, sas_addr, index,include);
  1187. return res;
  1188. }
  1189. /**
  1190. * sas_configure_parent -- configure routing table of parent
  1191. * parent: parent expander
  1192. * child: child expander
  1193. * sas_addr: SAS port identifier of device directly attached to child
  1194. */
  1195. static int sas_configure_parent(struct domain_device *parent,
  1196. struct domain_device *child,
  1197. u8 *sas_addr, int include)
  1198. {
  1199. struct expander_device *ex_parent = &parent->ex_dev;
  1200. int res = 0;
  1201. int i;
  1202. if (parent->parent) {
  1203. res = sas_configure_parent(parent->parent, parent, sas_addr,
  1204. include);
  1205. if (res)
  1206. return res;
  1207. }
  1208. if (ex_parent->conf_route_table == 0) {
  1209. SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
  1210. SAS_ADDR(parent->sas_addr));
  1211. return 0;
  1212. }
  1213. for (i = 0; i < ex_parent->num_phys; i++) {
  1214. struct ex_phy *phy = &ex_parent->ex_phy[i];
  1215. if ((phy->routing_attr == TABLE_ROUTING) &&
  1216. (SAS_ADDR(phy->attached_sas_addr) ==
  1217. SAS_ADDR(child->sas_addr))) {
  1218. res = sas_configure_phy(parent, i, sas_addr, include);
  1219. if (res)
  1220. return res;
  1221. }
  1222. }
  1223. return res;
  1224. }
  1225. /**
  1226. * sas_configure_routing -- configure routing
  1227. * dev: expander device
  1228. * sas_addr: port identifier of device directly attached to the expander device
  1229. */
  1230. static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
  1231. {
  1232. if (dev->parent)
  1233. return sas_configure_parent(dev->parent, dev, sas_addr, 1);
  1234. return 0;
  1235. }
  1236. static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
  1237. {
  1238. if (dev->parent)
  1239. return sas_configure_parent(dev->parent, dev, sas_addr, 0);
  1240. return 0;
  1241. }
  1242. /**
  1243. * sas_discover_expander -- expander discovery
  1244. * @ex: pointer to expander domain device
  1245. *
  1246. * See comment in sas_discover_sata().
  1247. */
  1248. static int sas_discover_expander(struct domain_device *dev)
  1249. {
  1250. int res;
  1251. res = sas_notify_lldd_dev_found(dev);
  1252. if (res)
  1253. return res;
  1254. res = sas_ex_general(dev);
  1255. if (res)
  1256. goto out_err;
  1257. res = sas_ex_manuf_info(dev);
  1258. if (res)
  1259. goto out_err;
  1260. res = sas_expander_discover(dev);
  1261. if (res) {
  1262. SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
  1263. SAS_ADDR(dev->sas_addr), res);
  1264. goto out_err;
  1265. }
  1266. sas_check_ex_subtractive_boundary(dev);
  1267. res = sas_check_parent_topology(dev);
  1268. if (res)
  1269. goto out_err;
  1270. return 0;
  1271. out_err:
  1272. sas_notify_lldd_dev_gone(dev);
  1273. return res;
  1274. }
  1275. static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
  1276. {
  1277. int res = 0;
  1278. struct domain_device *dev;
  1279. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  1280. if (dev->dev_type == EDGE_DEV ||
  1281. dev->dev_type == FANOUT_DEV) {
  1282. struct sas_expander_device *ex =
  1283. rphy_to_expander_device(dev->rphy);
  1284. if (level == ex->level)
  1285. res = sas_ex_discover_devices(dev, -1);
  1286. else if (level > 0)
  1287. res = sas_ex_discover_devices(port->port_dev, -1);
  1288. }
  1289. }
  1290. return res;
  1291. }
  1292. static int sas_ex_bfs_disc(struct asd_sas_port *port)
  1293. {
  1294. int res;
  1295. int level;
  1296. do {
  1297. level = port->disc.max_level;
  1298. res = sas_ex_level_discovery(port, level);
  1299. mb();
  1300. } while (level < port->disc.max_level);
  1301. return res;
  1302. }
  1303. int sas_discover_root_expander(struct domain_device *dev)
  1304. {
  1305. int res;
  1306. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1307. res = sas_rphy_add(dev->rphy);
  1308. if (res)
  1309. goto out_err;
  1310. ex->level = dev->port->disc.max_level; /* 0 */
  1311. res = sas_discover_expander(dev);
  1312. if (res)
  1313. goto out_err2;
  1314. sas_ex_bfs_disc(dev->port);
  1315. return res;
  1316. out_err2:
  1317. sas_rphy_remove(dev->rphy);
  1318. out_err:
  1319. return res;
  1320. }
  1321. /* ---------- Domain revalidation ---------- */
  1322. static int sas_get_phy_discover(struct domain_device *dev,
  1323. int phy_id, struct smp_resp *disc_resp)
  1324. {
  1325. int res;
  1326. u8 *disc_req;
  1327. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  1328. if (!disc_req)
  1329. return -ENOMEM;
  1330. disc_req[1] = SMP_DISCOVER;
  1331. disc_req[9] = phy_id;
  1332. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  1333. disc_resp, DISCOVER_RESP_SIZE);
  1334. if (res)
  1335. goto out;
  1336. else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
  1337. res = disc_resp->result;
  1338. goto out;
  1339. }
  1340. out:
  1341. kfree(disc_req);
  1342. return res;
  1343. }
  1344. static int sas_get_phy_change_count(struct domain_device *dev,
  1345. int phy_id, int *pcc)
  1346. {
  1347. int res;
  1348. struct smp_resp *disc_resp;
  1349. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1350. if (!disc_resp)
  1351. return -ENOMEM;
  1352. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1353. if (!res)
  1354. *pcc = disc_resp->disc.change_count;
  1355. kfree(disc_resp);
  1356. return res;
  1357. }
  1358. static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
  1359. int phy_id, u8 *attached_sas_addr)
  1360. {
  1361. int res;
  1362. struct smp_resp *disc_resp;
  1363. struct discover_resp *dr;
  1364. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1365. if (!disc_resp)
  1366. return -ENOMEM;
  1367. dr = &disc_resp->disc;
  1368. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1369. if (!res) {
  1370. memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
  1371. if (dr->attached_dev_type == 0)
  1372. memset(attached_sas_addr, 0, 8);
  1373. }
  1374. kfree(disc_resp);
  1375. return res;
  1376. }
  1377. static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
  1378. int from_phy, bool update)
  1379. {
  1380. struct expander_device *ex = &dev->ex_dev;
  1381. int res = 0;
  1382. int i;
  1383. for (i = from_phy; i < ex->num_phys; i++) {
  1384. int phy_change_count = 0;
  1385. res = sas_get_phy_change_count(dev, i, &phy_change_count);
  1386. if (res)
  1387. goto out;
  1388. else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
  1389. if (update)
  1390. ex->ex_phy[i].phy_change_count =
  1391. phy_change_count;
  1392. *phy_id = i;
  1393. return 0;
  1394. }
  1395. }
  1396. out:
  1397. return res;
  1398. }
  1399. static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
  1400. {
  1401. int res;
  1402. u8 *rg_req;
  1403. struct smp_resp *rg_resp;
  1404. rg_req = alloc_smp_req(RG_REQ_SIZE);
  1405. if (!rg_req)
  1406. return -ENOMEM;
  1407. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  1408. if (!rg_resp) {
  1409. kfree(rg_req);
  1410. return -ENOMEM;
  1411. }
  1412. rg_req[1] = SMP_REPORT_GENERAL;
  1413. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  1414. RG_RESP_SIZE);
  1415. if (res)
  1416. goto out;
  1417. if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  1418. res = rg_resp->result;
  1419. goto out;
  1420. }
  1421. *ecc = be16_to_cpu(rg_resp->rg.change_count);
  1422. out:
  1423. kfree(rg_resp);
  1424. kfree(rg_req);
  1425. return res;
  1426. }
  1427. /**
  1428. * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
  1429. * @dev:domain device to be detect.
  1430. * @src_dev: the device which originated BROADCAST(CHANGE).
  1431. *
  1432. * Add self-configuration expander suport. Suppose two expander cascading,
  1433. * when the first level expander is self-configuring, hotplug the disks in
  1434. * second level expander, BROADCAST(CHANGE) will not only be originated
  1435. * in the second level expander, but also be originated in the first level
  1436. * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
  1437. * expander changed count in two level expanders will all increment at least
  1438. * once, but the phy which chang count has changed is the source device which
  1439. * we concerned.
  1440. */
  1441. static int sas_find_bcast_dev(struct domain_device *dev,
  1442. struct domain_device **src_dev)
  1443. {
  1444. struct expander_device *ex = &dev->ex_dev;
  1445. int ex_change_count = -1;
  1446. int phy_id = -1;
  1447. int res;
  1448. struct domain_device *ch;
  1449. res = sas_get_ex_change_count(dev, &ex_change_count);
  1450. if (res)
  1451. goto out;
  1452. if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
  1453. /* Just detect if this expander phys phy change count changed,
  1454. * in order to determine if this expander originate BROADCAST,
  1455. * and do not update phy change count field in our structure.
  1456. */
  1457. res = sas_find_bcast_phy(dev, &phy_id, 0, false);
  1458. if (phy_id != -1) {
  1459. *src_dev = dev;
  1460. ex->ex_change_count = ex_change_count;
  1461. SAS_DPRINTK("Expander phy change count has changed\n");
  1462. return res;
  1463. } else
  1464. SAS_DPRINTK("Expander phys DID NOT change\n");
  1465. }
  1466. list_for_each_entry(ch, &ex->children, siblings) {
  1467. if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
  1468. res = sas_find_bcast_dev(ch, src_dev);
  1469. if (src_dev)
  1470. return res;
  1471. }
  1472. }
  1473. out:
  1474. return res;
  1475. }
  1476. static void sas_unregister_ex_tree(struct domain_device *dev)
  1477. {
  1478. struct expander_device *ex = &dev->ex_dev;
  1479. struct domain_device *child, *n;
  1480. list_for_each_entry_safe(child, n, &ex->children, siblings) {
  1481. child->gone = 1;
  1482. if (child->dev_type == EDGE_DEV ||
  1483. child->dev_type == FANOUT_DEV)
  1484. sas_unregister_ex_tree(child);
  1485. else
  1486. sas_unregister_dev(child);
  1487. }
  1488. sas_unregister_dev(dev);
  1489. }
  1490. static void sas_unregister_devs_sas_addr(struct domain_device *parent,
  1491. int phy_id, bool last)
  1492. {
  1493. struct expander_device *ex_dev = &parent->ex_dev;
  1494. struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
  1495. struct domain_device *child, *n;
  1496. if (last) {
  1497. list_for_each_entry_safe(child, n,
  1498. &ex_dev->children, siblings) {
  1499. if (SAS_ADDR(child->sas_addr) ==
  1500. SAS_ADDR(phy->attached_sas_addr)) {
  1501. child->gone = 1;
  1502. if (child->dev_type == EDGE_DEV ||
  1503. child->dev_type == FANOUT_DEV)
  1504. sas_unregister_ex_tree(child);
  1505. else
  1506. sas_unregister_dev(child);
  1507. break;
  1508. }
  1509. }
  1510. parent->gone = 1;
  1511. sas_disable_routing(parent, phy->attached_sas_addr);
  1512. }
  1513. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  1514. sas_port_delete_phy(phy->port, phy->phy);
  1515. if (phy->port->num_phys == 0)
  1516. sas_port_delete(phy->port);
  1517. phy->port = NULL;
  1518. }
  1519. static int sas_discover_bfs_by_root_level(struct domain_device *root,
  1520. const int level)
  1521. {
  1522. struct expander_device *ex_root = &root->ex_dev;
  1523. struct domain_device *child;
  1524. int res = 0;
  1525. list_for_each_entry(child, &ex_root->children, siblings) {
  1526. if (child->dev_type == EDGE_DEV ||
  1527. child->dev_type == FANOUT_DEV) {
  1528. struct sas_expander_device *ex =
  1529. rphy_to_expander_device(child->rphy);
  1530. if (level > ex->level)
  1531. res = sas_discover_bfs_by_root_level(child,
  1532. level);
  1533. else if (level == ex->level)
  1534. res = sas_ex_discover_devices(child, -1);
  1535. }
  1536. }
  1537. return res;
  1538. }
  1539. static int sas_discover_bfs_by_root(struct domain_device *dev)
  1540. {
  1541. int res;
  1542. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1543. int level = ex->level+1;
  1544. res = sas_ex_discover_devices(dev, -1);
  1545. if (res)
  1546. goto out;
  1547. do {
  1548. res = sas_discover_bfs_by_root_level(dev, level);
  1549. mb();
  1550. level += 1;
  1551. } while (level <= dev->port->disc.max_level);
  1552. out:
  1553. return res;
  1554. }
  1555. static int sas_discover_new(struct domain_device *dev, int phy_id)
  1556. {
  1557. struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
  1558. struct domain_device *child;
  1559. bool found = false;
  1560. int res, i;
  1561. SAS_DPRINTK("ex %016llx phy%d new device attached\n",
  1562. SAS_ADDR(dev->sas_addr), phy_id);
  1563. res = sas_ex_phy_discover(dev, phy_id);
  1564. if (res)
  1565. goto out;
  1566. /* to support the wide port inserted */
  1567. for (i = 0; i < dev->ex_dev.num_phys; i++) {
  1568. struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
  1569. if (i == phy_id)
  1570. continue;
  1571. if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
  1572. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1573. found = true;
  1574. break;
  1575. }
  1576. }
  1577. if (found) {
  1578. sas_ex_join_wide_port(dev, phy_id);
  1579. return 0;
  1580. }
  1581. res = sas_ex_discover_devices(dev, phy_id);
  1582. if (!res)
  1583. goto out;
  1584. list_for_each_entry(child, &dev->ex_dev.children, siblings) {
  1585. if (SAS_ADDR(child->sas_addr) ==
  1586. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1587. if (child->dev_type == EDGE_DEV ||
  1588. child->dev_type == FANOUT_DEV)
  1589. res = sas_discover_bfs_by_root(child);
  1590. break;
  1591. }
  1592. }
  1593. out:
  1594. return res;
  1595. }
  1596. static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
  1597. {
  1598. struct expander_device *ex = &dev->ex_dev;
  1599. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1600. u8 attached_sas_addr[8];
  1601. int res;
  1602. res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
  1603. switch (res) {
  1604. case SMP_RESP_NO_PHY:
  1605. phy->phy_state = PHY_NOT_PRESENT;
  1606. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1607. goto out; break;
  1608. case SMP_RESP_PHY_VACANT:
  1609. phy->phy_state = PHY_VACANT;
  1610. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1611. goto out; break;
  1612. case SMP_RESP_FUNC_ACC:
  1613. break;
  1614. }
  1615. if (SAS_ADDR(attached_sas_addr) == 0) {
  1616. phy->phy_state = PHY_EMPTY;
  1617. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1618. } else if (SAS_ADDR(attached_sas_addr) ==
  1619. SAS_ADDR(phy->attached_sas_addr)) {
  1620. SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
  1621. SAS_ADDR(dev->sas_addr), phy_id);
  1622. sas_ex_phy_discover(dev, phy_id);
  1623. } else
  1624. res = sas_discover_new(dev, phy_id);
  1625. out:
  1626. return res;
  1627. }
  1628. /**
  1629. * sas_rediscover - revalidate the domain.
  1630. * @dev:domain device to be detect.
  1631. * @phy_id: the phy id will be detected.
  1632. *
  1633. * NOTE: this process _must_ quit (return) as soon as any connection
  1634. * errors are encountered. Connection recovery is done elsewhere.
  1635. * Discover process only interrogates devices in order to discover the
  1636. * domain.For plugging out, we un-register the device only when it is
  1637. * the last phy in the port, for other phys in this port, we just delete it
  1638. * from the port.For inserting, we do discovery when it is the
  1639. * first phy,for other phys in this port, we add it to the port to
  1640. * forming the wide-port.
  1641. */
  1642. static int sas_rediscover(struct domain_device *dev, const int phy_id)
  1643. {
  1644. struct expander_device *ex = &dev->ex_dev;
  1645. struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
  1646. int res = 0;
  1647. int i;
  1648. bool last = true; /* is this the last phy of the port */
  1649. SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
  1650. SAS_ADDR(dev->sas_addr), phy_id);
  1651. if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
  1652. for (i = 0; i < ex->num_phys; i++) {
  1653. struct ex_phy *phy = &ex->ex_phy[i];
  1654. if (i == phy_id)
  1655. continue;
  1656. if (SAS_ADDR(phy->attached_sas_addr) ==
  1657. SAS_ADDR(changed_phy->attached_sas_addr)) {
  1658. SAS_DPRINTK("phy%d part of wide port with "
  1659. "phy%d\n", phy_id, i);
  1660. last = false;
  1661. break;
  1662. }
  1663. }
  1664. res = sas_rediscover_dev(dev, phy_id, last);
  1665. } else
  1666. res = sas_discover_new(dev, phy_id);
  1667. return res;
  1668. }
  1669. /**
  1670. * sas_revalidate_domain -- revalidate the domain
  1671. * @port: port to the domain of interest
  1672. *
  1673. * NOTE: this process _must_ quit (return) as soon as any connection
  1674. * errors are encountered. Connection recovery is done elsewhere.
  1675. * Discover process only interrogates devices in order to discover the
  1676. * domain.
  1677. */
  1678. int sas_ex_revalidate_domain(struct domain_device *port_dev)
  1679. {
  1680. int res;
  1681. struct domain_device *dev = NULL;
  1682. res = sas_find_bcast_dev(port_dev, &dev);
  1683. if (res)
  1684. goto out;
  1685. if (dev) {
  1686. struct expander_device *ex = &dev->ex_dev;
  1687. int i = 0, phy_id;
  1688. do {
  1689. phy_id = -1;
  1690. res = sas_find_bcast_phy(dev, &phy_id, i, true);
  1691. if (phy_id == -1)
  1692. break;
  1693. res = sas_rediscover(dev, phy_id);
  1694. i = phy_id + 1;
  1695. } while (i < ex->num_phys);
  1696. }
  1697. out:
  1698. return res;
  1699. }
  1700. int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1701. struct request *req)
  1702. {
  1703. struct domain_device *dev;
  1704. int ret, type;
  1705. struct request *rsp = req->next_rq;
  1706. if (!rsp) {
  1707. printk("%s: space for a smp response is missing\n",
  1708. __func__);
  1709. return -EINVAL;
  1710. }
  1711. /* no rphy means no smp target support (ie aic94xx host) */
  1712. if (!rphy)
  1713. return sas_smp_host_handler(shost, req, rsp);
  1714. type = rphy->identify.device_type;
  1715. if (type != SAS_EDGE_EXPANDER_DEVICE &&
  1716. type != SAS_FANOUT_EXPANDER_DEVICE) {
  1717. printk("%s: can we send a smp request to a device?\n",
  1718. __func__);
  1719. return -EINVAL;
  1720. }
  1721. dev = sas_find_dev_by_rphy(rphy);
  1722. if (!dev) {
  1723. printk("%s: fail to find a domain_device?\n", __func__);
  1724. return -EINVAL;
  1725. }
  1726. /* do we need to support multiple segments? */
  1727. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1728. printk("%s: multiple segments req %u %u, rsp %u %u\n",
  1729. __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
  1730. rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
  1731. return -EINVAL;
  1732. }
  1733. ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
  1734. bio_data(rsp->bio), blk_rq_bytes(rsp));
  1735. if (ret > 0) {
  1736. /* positive number is the untransferred residual */
  1737. rsp->resid_len = ret;
  1738. req->resid_len = 0;
  1739. ret = 0;
  1740. } else if (ret == 0) {
  1741. rsp->resid_len = 0;
  1742. req->resid_len = 0;
  1743. }
  1744. return ret;
  1745. }