sas_expander.c 54 KB

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