sas_expander.c 56 KB

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