sas_expander.c 53 KB

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