sas_expander.c 57 KB

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