sas_expander.c 50 KB

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