sas_expander.c 55 KB

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