hisi_sas_v2_hw.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  1. /*
  2. * Copyright (c) 2016 Linaro Ltd.
  3. * Copyright (c) 2016 Hisilicon Limited.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. */
  11. #include "hisi_sas.h"
  12. #define DRV_NAME "hisi_sas_v2_hw"
  13. /* global registers need init*/
  14. #define DLVRY_QUEUE_ENABLE 0x0
  15. #define IOST_BASE_ADDR_LO 0x8
  16. #define IOST_BASE_ADDR_HI 0xc
  17. #define ITCT_BASE_ADDR_LO 0x10
  18. #define ITCT_BASE_ADDR_HI 0x14
  19. #define IO_BROKEN_MSG_ADDR_LO 0x18
  20. #define IO_BROKEN_MSG_ADDR_HI 0x1c
  21. #define PHY_CONTEXT 0x20
  22. #define PHY_STATE 0x24
  23. #define PHY_PORT_NUM_MA 0x28
  24. #define PORT_STATE 0x2c
  25. #define PORT_STATE_PHY8_PORT_NUM_OFF 16
  26. #define PORT_STATE_PHY8_PORT_NUM_MSK (0xf << PORT_STATE_PHY8_PORT_NUM_OFF)
  27. #define PORT_STATE_PHY8_CONN_RATE_OFF 20
  28. #define PORT_STATE_PHY8_CONN_RATE_MSK (0xf << PORT_STATE_PHY8_CONN_RATE_OFF)
  29. #define PHY_CONN_RATE 0x30
  30. #define HGC_TRANS_TASK_CNT_LIMIT 0x38
  31. #define AXI_AHB_CLK_CFG 0x3c
  32. #define ITCT_CLR 0x44
  33. #define ITCT_CLR_EN_OFF 16
  34. #define ITCT_CLR_EN_MSK (0x1 << ITCT_CLR_EN_OFF)
  35. #define ITCT_DEV_OFF 0
  36. #define ITCT_DEV_MSK (0x7ff << ITCT_DEV_OFF)
  37. #define AXI_USER1 0x48
  38. #define AXI_USER2 0x4c
  39. #define IO_SATA_BROKEN_MSG_ADDR_LO 0x58
  40. #define IO_SATA_BROKEN_MSG_ADDR_HI 0x5c
  41. #define SATA_INITI_D2H_STORE_ADDR_LO 0x60
  42. #define SATA_INITI_D2H_STORE_ADDR_HI 0x64
  43. #define HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL 0x84
  44. #define HGC_SAS_TXFAIL_RETRY_CTRL 0x88
  45. #define HGC_GET_ITV_TIME 0x90
  46. #define DEVICE_MSG_WORK_MODE 0x94
  47. #define OPENA_WT_CONTI_TIME 0x9c
  48. #define I_T_NEXUS_LOSS_TIME 0xa0
  49. #define MAX_CON_TIME_LIMIT_TIME 0xa4
  50. #define BUS_INACTIVE_LIMIT_TIME 0xa8
  51. #define REJECT_TO_OPEN_LIMIT_TIME 0xac
  52. #define CFG_AGING_TIME 0xbc
  53. #define HGC_DFX_CFG2 0xc0
  54. #define HGC_IOMB_PROC1_STATUS 0x104
  55. #define CFG_1US_TIMER_TRSH 0xcc
  56. #define HGC_INVLD_DQE_INFO 0x148
  57. #define HGC_INVLD_DQE_INFO_FB_CH0_OFF 9
  58. #define HGC_INVLD_DQE_INFO_FB_CH0_MSK (0x1 << HGC_INVLD_DQE_INFO_FB_CH0_OFF)
  59. #define HGC_INVLD_DQE_INFO_FB_CH3_OFF 18
  60. #define INT_COAL_EN 0x19c
  61. #define OQ_INT_COAL_TIME 0x1a0
  62. #define OQ_INT_COAL_CNT 0x1a4
  63. #define ENT_INT_COAL_TIME 0x1a8
  64. #define ENT_INT_COAL_CNT 0x1ac
  65. #define OQ_INT_SRC 0x1b0
  66. #define OQ_INT_SRC_MSK 0x1b4
  67. #define ENT_INT_SRC1 0x1b8
  68. #define ENT_INT_SRC1_D2H_FIS_CH0_OFF 0
  69. #define ENT_INT_SRC1_D2H_FIS_CH0_MSK (0x1 << ENT_INT_SRC1_D2H_FIS_CH0_OFF)
  70. #define ENT_INT_SRC1_D2H_FIS_CH1_OFF 8
  71. #define ENT_INT_SRC1_D2H_FIS_CH1_MSK (0x1 << ENT_INT_SRC1_D2H_FIS_CH1_OFF)
  72. #define ENT_INT_SRC2 0x1bc
  73. #define ENT_INT_SRC3 0x1c0
  74. #define ENT_INT_SRC3_ITC_INT_OFF 15
  75. #define ENT_INT_SRC3_ITC_INT_MSK (0x1 << ENT_INT_SRC3_ITC_INT_OFF)
  76. #define ENT_INT_SRC_MSK1 0x1c4
  77. #define ENT_INT_SRC_MSK2 0x1c8
  78. #define ENT_INT_SRC_MSK3 0x1cc
  79. #define ENT_INT_SRC_MSK3_ENT95_MSK_OFF 31
  80. #define ENT_INT_SRC_MSK3_ENT95_MSK_MSK (0x1 << ENT_INT_SRC_MSK3_ENT95_MSK_OFF)
  81. #define SAS_ECC_INTR_MSK 0x1ec
  82. #define HGC_ERR_STAT_EN 0x238
  83. #define DLVRY_Q_0_BASE_ADDR_LO 0x260
  84. #define DLVRY_Q_0_BASE_ADDR_HI 0x264
  85. #define DLVRY_Q_0_DEPTH 0x268
  86. #define DLVRY_Q_0_WR_PTR 0x26c
  87. #define DLVRY_Q_0_RD_PTR 0x270
  88. #define HYPER_STREAM_ID_EN_CFG 0xc80
  89. #define OQ0_INT_SRC_MSK 0xc90
  90. #define COMPL_Q_0_BASE_ADDR_LO 0x4e0
  91. #define COMPL_Q_0_BASE_ADDR_HI 0x4e4
  92. #define COMPL_Q_0_DEPTH 0x4e8
  93. #define COMPL_Q_0_WR_PTR 0x4ec
  94. #define COMPL_Q_0_RD_PTR 0x4f0
  95. /* phy registers need init */
  96. #define PORT_BASE (0x2000)
  97. #define PHY_CFG (PORT_BASE + 0x0)
  98. #define HARD_PHY_LINKRATE (PORT_BASE + 0x4)
  99. #define PHY_CFG_ENA_OFF 0
  100. #define PHY_CFG_ENA_MSK (0x1 << PHY_CFG_ENA_OFF)
  101. #define PHY_CFG_DC_OPT_OFF 2
  102. #define PHY_CFG_DC_OPT_MSK (0x1 << PHY_CFG_DC_OPT_OFF)
  103. #define PROG_PHY_LINK_RATE (PORT_BASE + 0x8)
  104. #define PROG_PHY_LINK_RATE_MAX_OFF 0
  105. #define PROG_PHY_LINK_RATE_MAX_MSK (0xff << PROG_PHY_LINK_RATE_MAX_OFF)
  106. #define PHY_CTRL (PORT_BASE + 0x14)
  107. #define PHY_CTRL_RESET_OFF 0
  108. #define PHY_CTRL_RESET_MSK (0x1 << PHY_CTRL_RESET_OFF)
  109. #define SAS_PHY_CTRL (PORT_BASE + 0x20)
  110. #define SL_CFG (PORT_BASE + 0x84)
  111. #define PHY_PCN (PORT_BASE + 0x44)
  112. #define SL_TOUT_CFG (PORT_BASE + 0x8c)
  113. #define SL_CONTROL (PORT_BASE + 0x94)
  114. #define SL_CONTROL_NOTIFY_EN_OFF 0
  115. #define SL_CONTROL_NOTIFY_EN_MSK (0x1 << SL_CONTROL_NOTIFY_EN_OFF)
  116. #define SL_CONTROL_CTA_OFF 17
  117. #define SL_CONTROL_CTA_MSK (0x1 << SL_CONTROL_CTA_OFF)
  118. #define TX_ID_DWORD0 (PORT_BASE + 0x9c)
  119. #define TX_ID_DWORD1 (PORT_BASE + 0xa0)
  120. #define TX_ID_DWORD2 (PORT_BASE + 0xa4)
  121. #define TX_ID_DWORD3 (PORT_BASE + 0xa8)
  122. #define TX_ID_DWORD4 (PORT_BASE + 0xaC)
  123. #define TX_ID_DWORD5 (PORT_BASE + 0xb0)
  124. #define TX_ID_DWORD6 (PORT_BASE + 0xb4)
  125. #define TXID_AUTO (PORT_BASE + 0xb8)
  126. #define TXID_AUTO_CT3_OFF 1
  127. #define TXID_AUTO_CT3_MSK (0x1 << TXID_AUTO_CT3_OFF)
  128. #define RX_IDAF_DWORD0 (PORT_BASE + 0xc4)
  129. #define RX_IDAF_DWORD1 (PORT_BASE + 0xc8)
  130. #define RX_IDAF_DWORD2 (PORT_BASE + 0xcc)
  131. #define RX_IDAF_DWORD3 (PORT_BASE + 0xd0)
  132. #define RX_IDAF_DWORD4 (PORT_BASE + 0xd4)
  133. #define RX_IDAF_DWORD5 (PORT_BASE + 0xd8)
  134. #define RX_IDAF_DWORD6 (PORT_BASE + 0xdc)
  135. #define RXOP_CHECK_CFG_H (PORT_BASE + 0xfc)
  136. #define DONE_RECEIVED_TIME (PORT_BASE + 0x11c)
  137. #define CHL_INT0 (PORT_BASE + 0x1b4)
  138. #define CHL_INT0_HOTPLUG_TOUT_OFF 0
  139. #define CHL_INT0_HOTPLUG_TOUT_MSK (0x1 << CHL_INT0_HOTPLUG_TOUT_OFF)
  140. #define CHL_INT0_SL_RX_BCST_ACK_OFF 1
  141. #define CHL_INT0_SL_RX_BCST_ACK_MSK (0x1 << CHL_INT0_SL_RX_BCST_ACK_OFF)
  142. #define CHL_INT0_SL_PHY_ENABLE_OFF 2
  143. #define CHL_INT0_SL_PHY_ENABLE_MSK (0x1 << CHL_INT0_SL_PHY_ENABLE_OFF)
  144. #define CHL_INT0_NOT_RDY_OFF 4
  145. #define CHL_INT0_NOT_RDY_MSK (0x1 << CHL_INT0_NOT_RDY_OFF)
  146. #define CHL_INT0_PHY_RDY_OFF 5
  147. #define CHL_INT0_PHY_RDY_MSK (0x1 << CHL_INT0_PHY_RDY_OFF)
  148. #define CHL_INT1 (PORT_BASE + 0x1b8)
  149. #define CHL_INT1_DMAC_TX_ECC_ERR_OFF 15
  150. #define CHL_INT1_DMAC_TX_ECC_ERR_MSK (0x1 << CHL_INT1_DMAC_TX_ECC_ERR_OFF)
  151. #define CHL_INT1_DMAC_RX_ECC_ERR_OFF 17
  152. #define CHL_INT1_DMAC_RX_ECC_ERR_MSK (0x1 << CHL_INT1_DMAC_RX_ECC_ERR_OFF)
  153. #define CHL_INT2 (PORT_BASE + 0x1bc)
  154. #define CHL_INT0_MSK (PORT_BASE + 0x1c0)
  155. #define CHL_INT1_MSK (PORT_BASE + 0x1c4)
  156. #define CHL_INT2_MSK (PORT_BASE + 0x1c8)
  157. #define CHL_INT_COAL_EN (PORT_BASE + 0x1d0)
  158. #define PHY_CTRL_RDY_MSK (PORT_BASE + 0x2b0)
  159. #define PHYCTRL_NOT_RDY_MSK (PORT_BASE + 0x2b4)
  160. #define PHYCTRL_DWS_RESET_MSK (PORT_BASE + 0x2b8)
  161. #define PHYCTRL_PHY_ENA_MSK (PORT_BASE + 0x2bc)
  162. #define SL_RX_BCAST_CHK_MSK (PORT_BASE + 0x2c0)
  163. #define PHYCTRL_OOB_RESTART_MSK (PORT_BASE + 0x2c4)
  164. #define DMA_TX_STATUS (PORT_BASE + 0x2d0)
  165. #define DMA_TX_STATUS_BUSY_OFF 0
  166. #define DMA_TX_STATUS_BUSY_MSK (0x1 << DMA_TX_STATUS_BUSY_OFF)
  167. #define DMA_RX_STATUS (PORT_BASE + 0x2e8)
  168. #define DMA_RX_STATUS_BUSY_OFF 0
  169. #define DMA_RX_STATUS_BUSY_MSK (0x1 << DMA_RX_STATUS_BUSY_OFF)
  170. #define AXI_CFG (0x5100)
  171. #define AM_CFG_MAX_TRANS (0x5010)
  172. #define AM_CFG_SINGLE_PORT_MAX_TRANS (0x5014)
  173. /* HW dma structures */
  174. /* Delivery queue header */
  175. /* dw0 */
  176. #define CMD_HDR_ABORT_FLAG_OFF 0
  177. #define CMD_HDR_ABORT_FLAG_MSK (0x3 << CMD_HDR_ABORT_FLAG_OFF)
  178. #define CMD_HDR_ABORT_DEVICE_TYPE_OFF 2
  179. #define CMD_HDR_ABORT_DEVICE_TYPE_MSK (0x1 << CMD_HDR_ABORT_DEVICE_TYPE_OFF)
  180. #define CMD_HDR_RESP_REPORT_OFF 5
  181. #define CMD_HDR_RESP_REPORT_MSK (0x1 << CMD_HDR_RESP_REPORT_OFF)
  182. #define CMD_HDR_TLR_CTRL_OFF 6
  183. #define CMD_HDR_TLR_CTRL_MSK (0x3 << CMD_HDR_TLR_CTRL_OFF)
  184. #define CMD_HDR_PORT_OFF 18
  185. #define CMD_HDR_PORT_MSK (0xf << CMD_HDR_PORT_OFF)
  186. #define CMD_HDR_PRIORITY_OFF 27
  187. #define CMD_HDR_PRIORITY_MSK (0x1 << CMD_HDR_PRIORITY_OFF)
  188. #define CMD_HDR_CMD_OFF 29
  189. #define CMD_HDR_CMD_MSK (0x7 << CMD_HDR_CMD_OFF)
  190. /* dw1 */
  191. #define CMD_HDR_DIR_OFF 5
  192. #define CMD_HDR_DIR_MSK (0x3 << CMD_HDR_DIR_OFF)
  193. #define CMD_HDR_RESET_OFF 7
  194. #define CMD_HDR_RESET_MSK (0x1 << CMD_HDR_RESET_OFF)
  195. #define CMD_HDR_VDTL_OFF 10
  196. #define CMD_HDR_VDTL_MSK (0x1 << CMD_HDR_VDTL_OFF)
  197. #define CMD_HDR_FRAME_TYPE_OFF 11
  198. #define CMD_HDR_FRAME_TYPE_MSK (0x1f << CMD_HDR_FRAME_TYPE_OFF)
  199. #define CMD_HDR_DEV_ID_OFF 16
  200. #define CMD_HDR_DEV_ID_MSK (0xffff << CMD_HDR_DEV_ID_OFF)
  201. /* dw2 */
  202. #define CMD_HDR_CFL_OFF 0
  203. #define CMD_HDR_CFL_MSK (0x1ff << CMD_HDR_CFL_OFF)
  204. #define CMD_HDR_NCQ_TAG_OFF 10
  205. #define CMD_HDR_NCQ_TAG_MSK (0x1f << CMD_HDR_NCQ_TAG_OFF)
  206. #define CMD_HDR_MRFL_OFF 15
  207. #define CMD_HDR_MRFL_MSK (0x1ff << CMD_HDR_MRFL_OFF)
  208. #define CMD_HDR_SG_MOD_OFF 24
  209. #define CMD_HDR_SG_MOD_MSK (0x3 << CMD_HDR_SG_MOD_OFF)
  210. #define CMD_HDR_FIRST_BURST_OFF 26
  211. #define CMD_HDR_FIRST_BURST_MSK (0x1 << CMD_HDR_SG_MOD_OFF)
  212. /* dw3 */
  213. #define CMD_HDR_IPTT_OFF 0
  214. #define CMD_HDR_IPTT_MSK (0xffff << CMD_HDR_IPTT_OFF)
  215. /* dw6 */
  216. #define CMD_HDR_DIF_SGL_LEN_OFF 0
  217. #define CMD_HDR_DIF_SGL_LEN_MSK (0xffff << CMD_HDR_DIF_SGL_LEN_OFF)
  218. #define CMD_HDR_DATA_SGL_LEN_OFF 16
  219. #define CMD_HDR_DATA_SGL_LEN_MSK (0xffff << CMD_HDR_DATA_SGL_LEN_OFF)
  220. #define CMD_HDR_ABORT_IPTT_OFF 16
  221. #define CMD_HDR_ABORT_IPTT_MSK (0xffff << CMD_HDR_ABORT_IPTT_OFF)
  222. /* Completion header */
  223. /* dw0 */
  224. #define CMPLT_HDR_RSPNS_XFRD_OFF 10
  225. #define CMPLT_HDR_RSPNS_XFRD_MSK (0x1 << CMPLT_HDR_RSPNS_XFRD_OFF)
  226. #define CMPLT_HDR_ERX_OFF 12
  227. #define CMPLT_HDR_ERX_MSK (0x1 << CMPLT_HDR_ERX_OFF)
  228. #define CMPLT_HDR_ABORT_STAT_OFF 13
  229. #define CMPLT_HDR_ABORT_STAT_MSK (0x7 << CMPLT_HDR_ABORT_STAT_OFF)
  230. /* abort_stat */
  231. #define STAT_IO_NOT_VALID 0x1
  232. #define STAT_IO_NO_DEVICE 0x2
  233. #define STAT_IO_COMPLETE 0x3
  234. #define STAT_IO_ABORTED 0x4
  235. /* dw1 */
  236. #define CMPLT_HDR_IPTT_OFF 0
  237. #define CMPLT_HDR_IPTT_MSK (0xffff << CMPLT_HDR_IPTT_OFF)
  238. #define CMPLT_HDR_DEV_ID_OFF 16
  239. #define CMPLT_HDR_DEV_ID_MSK (0xffff << CMPLT_HDR_DEV_ID_OFF)
  240. /* ITCT header */
  241. /* qw0 */
  242. #define ITCT_HDR_DEV_TYPE_OFF 0
  243. #define ITCT_HDR_DEV_TYPE_MSK (0x3 << ITCT_HDR_DEV_TYPE_OFF)
  244. #define ITCT_HDR_VALID_OFF 2
  245. #define ITCT_HDR_VALID_MSK (0x1 << ITCT_HDR_VALID_OFF)
  246. #define ITCT_HDR_MCR_OFF 5
  247. #define ITCT_HDR_MCR_MSK (0xf << ITCT_HDR_MCR_OFF)
  248. #define ITCT_HDR_VLN_OFF 9
  249. #define ITCT_HDR_VLN_MSK (0xf << ITCT_HDR_VLN_OFF)
  250. #define ITCT_HDR_PORT_ID_OFF 28
  251. #define ITCT_HDR_PORT_ID_MSK (0xf << ITCT_HDR_PORT_ID_OFF)
  252. /* qw2 */
  253. #define ITCT_HDR_INLT_OFF 0
  254. #define ITCT_HDR_INLT_MSK (0xffffULL << ITCT_HDR_INLT_OFF)
  255. #define ITCT_HDR_BITLT_OFF 16
  256. #define ITCT_HDR_BITLT_MSK (0xffffULL << ITCT_HDR_BITLT_OFF)
  257. #define ITCT_HDR_MCTLT_OFF 32
  258. #define ITCT_HDR_MCTLT_MSK (0xffffULL << ITCT_HDR_MCTLT_OFF)
  259. #define ITCT_HDR_RTOLT_OFF 48
  260. #define ITCT_HDR_RTOLT_MSK (0xffffULL << ITCT_HDR_RTOLT_OFF)
  261. struct hisi_sas_complete_v2_hdr {
  262. __le32 dw0;
  263. __le32 dw1;
  264. __le32 act;
  265. __le32 dw3;
  266. };
  267. struct hisi_sas_err_record_v2 {
  268. /* dw0 */
  269. __le32 trans_tx_fail_type;
  270. /* dw1 */
  271. __le32 trans_rx_fail_type;
  272. /* dw2 */
  273. __le16 dma_tx_err_type;
  274. __le16 sipc_rx_err_type;
  275. /* dw3 */
  276. __le32 dma_rx_err_type;
  277. };
  278. enum {
  279. HISI_SAS_PHY_PHY_UPDOWN,
  280. HISI_SAS_PHY_CHNL_INT,
  281. HISI_SAS_PHY_INT_NR
  282. };
  283. enum {
  284. TRANS_TX_FAIL_BASE = 0x0, /* dw0 */
  285. TRANS_RX_FAIL_BASE = 0x100, /* dw1 */
  286. DMA_TX_ERR_BASE = 0x200, /* dw2 bit 15-0 */
  287. SIPC_RX_ERR_BASE = 0x300, /* dw2 bit 31-16*/
  288. DMA_RX_ERR_BASE = 0x400, /* dw3 */
  289. /* trans tx*/
  290. TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS = TRANS_TX_FAIL_BASE, /* 0x0 */
  291. TRANS_TX_ERR_PHY_NOT_ENABLE, /* 0x1 */
  292. TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION, /* 0x2 */
  293. TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION, /* 0x3 */
  294. TRANS_TX_OPEN_CNX_ERR_BY_OTHER, /* 0x4 */
  295. RESERVED0, /* 0x5 */
  296. TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT, /* 0x6 */
  297. TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY, /* 0x7 */
  298. TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED, /* 0x8 */
  299. TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED, /* 0x9 */
  300. TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION, /* 0xa */
  301. TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD, /* 0xb */
  302. TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER, /* 0xc */
  303. TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED, /* 0xd */
  304. TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT, /* 0xe */
  305. TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION, /* 0xf */
  306. TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED, /* 0x10 */
  307. TRANS_TX_ERR_FRAME_TXED, /* 0x11 */
  308. TRANS_TX_ERR_WITH_BREAK_TIMEOUT, /* 0x12 */
  309. TRANS_TX_ERR_WITH_BREAK_REQUEST, /* 0x13 */
  310. TRANS_TX_ERR_WITH_BREAK_RECEVIED, /* 0x14 */
  311. TRANS_TX_ERR_WITH_CLOSE_TIMEOUT, /* 0x15 */
  312. TRANS_TX_ERR_WITH_CLOSE_NORMAL, /* 0x16 for ssp*/
  313. TRANS_TX_ERR_WITH_CLOSE_PHYDISALE, /* 0x17 */
  314. TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT, /* 0x18 */
  315. TRANS_TX_ERR_WITH_CLOSE_COMINIT, /* 0x19 */
  316. TRANS_TX_ERR_WITH_NAK_RECEVIED, /* 0x1a for ssp*/
  317. TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT, /* 0x1b for ssp*/
  318. /*IO_TX_ERR_WITH_R_ERR_RECEVIED, [> 0x1b for sata/stp<] */
  319. TRANS_TX_ERR_WITH_CREDIT_TIMEOUT, /* 0x1c for ssp */
  320. /*IO_RX_ERR_WITH_SATA_DEVICE_LOST 0x1c for sata/stp */
  321. TRANS_TX_ERR_WITH_IPTT_CONFLICT, /* 0x1d for ssp/smp */
  322. TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS, /* 0x1e */
  323. /*IO_TX_ERR_WITH_SYNC_RXD, [> 0x1e <] for sata/stp */
  324. TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT, /* 0x1f for sata/stp */
  325. /* trans rx */
  326. TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR = TRANS_RX_FAIL_BASE, /* 0x100 */
  327. TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR, /* 0x101 for sata/stp */
  328. TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM, /* 0x102 for ssp/smp */
  329. /*IO_ERR_WITH_RXFIS_8B10B_CODE_ERR, [> 0x102 <] for sata/stp */
  330. TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR, /* 0x103 for sata/stp */
  331. TRANS_RX_ERR_WITH_RXFIS_CRC_ERR, /* 0x104 for sata/stp */
  332. TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN, /* 0x105 for smp */
  333. /*IO_ERR_WITH_RXFIS_TX SYNCP, [> 0x105 <] for sata/stp */
  334. TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP, /* 0x106 for sata/stp*/
  335. TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN, /* 0x107 */
  336. TRANS_RX_ERR_WITH_BREAK_TIMEOUT, /* 0x108 */
  337. TRANS_RX_ERR_WITH_BREAK_REQUEST, /* 0x109 */
  338. TRANS_RX_ERR_WITH_BREAK_RECEVIED, /* 0x10a */
  339. RESERVED1, /* 0x10b */
  340. TRANS_RX_ERR_WITH_CLOSE_NORMAL, /* 0x10c */
  341. TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE, /* 0x10d */
  342. TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT, /* 0x10e */
  343. TRANS_RX_ERR_WITH_CLOSE_COMINIT, /* 0x10f */
  344. TRANS_RX_ERR_WITH_DATA_LEN0, /* 0x110 for ssp/smp */
  345. TRANS_RX_ERR_WITH_BAD_HASH, /* 0x111 for ssp */
  346. /*IO_RX_ERR_WITH_FIS_TOO_SHORT, [> 0x111 <] for sata/stp */
  347. TRANS_RX_XRDY_WLEN_ZERO_ERR, /* 0x112 for ssp*/
  348. /*IO_RX_ERR_WITH_FIS_TOO_LONG, [> 0x112 <] for sata/stp */
  349. TRANS_RX_SSP_FRM_LEN_ERR, /* 0x113 for ssp */
  350. /*IO_RX_ERR_WITH_SATA_DEVICE_LOST, [> 0x113 <] for sata */
  351. RESERVED2, /* 0x114 */
  352. RESERVED3, /* 0x115 */
  353. RESERVED4, /* 0x116 */
  354. RESERVED5, /* 0x117 */
  355. TRANS_RX_ERR_WITH_BAD_FRM_TYPE, /* 0x118 */
  356. TRANS_RX_SMP_FRM_LEN_ERR, /* 0x119 */
  357. TRANS_RX_SMP_RESP_TIMEOUT_ERR, /* 0x11a */
  358. RESERVED6, /* 0x11b */
  359. RESERVED7, /* 0x11c */
  360. RESERVED8, /* 0x11d */
  361. RESERVED9, /* 0x11e */
  362. TRANS_RX_R_ERR, /* 0x11f */
  363. /* dma tx */
  364. DMA_TX_DIF_CRC_ERR = DMA_TX_ERR_BASE, /* 0x200 */
  365. DMA_TX_DIF_APP_ERR, /* 0x201 */
  366. DMA_TX_DIF_RPP_ERR, /* 0x202 */
  367. DMA_TX_DATA_SGL_OVERFLOW, /* 0x203 */
  368. DMA_TX_DIF_SGL_OVERFLOW, /* 0x204 */
  369. DMA_TX_UNEXP_XFER_ERR, /* 0x205 */
  370. DMA_TX_UNEXP_RETRANS_ERR, /* 0x206 */
  371. DMA_TX_XFER_LEN_OVERFLOW, /* 0x207 */
  372. DMA_TX_XFER_OFFSET_ERR, /* 0x208 */
  373. DMA_TX_RAM_ECC_ERR, /* 0x209 */
  374. DMA_TX_DIF_LEN_ALIGN_ERR, /* 0x20a */
  375. /* sipc rx */
  376. SIPC_RX_FIS_STATUS_ERR_BIT_VLD = SIPC_RX_ERR_BASE, /* 0x300 */
  377. SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR, /* 0x301 */
  378. SIPC_RX_FIS_STATUS_BSY_BIT_ERR, /* 0x302 */
  379. SIPC_RX_WRSETUP_LEN_ODD_ERR, /* 0x303 */
  380. SIPC_RX_WRSETUP_LEN_ZERO_ERR, /* 0x304 */
  381. SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR, /* 0x305 */
  382. SIPC_RX_NCQ_WRSETUP_OFFSET_ERR, /* 0x306 */
  383. SIPC_RX_NCQ_WRSETUP_AUTO_ACTIVE_ERR, /* 0x307 */
  384. SIPC_RX_SATA_UNEXP_FIS_ERR, /* 0x308 */
  385. SIPC_RX_WRSETUP_ESTATUS_ERR, /* 0x309 */
  386. SIPC_RX_DATA_UNDERFLOW_ERR, /* 0x30a */
  387. /* dma rx */
  388. DMA_RX_DIF_CRC_ERR = DMA_RX_ERR_BASE, /* 0x400 */
  389. DMA_RX_DIF_APP_ERR, /* 0x401 */
  390. DMA_RX_DIF_RPP_ERR, /* 0x402 */
  391. DMA_RX_DATA_SGL_OVERFLOW, /* 0x403 */
  392. DMA_RX_DIF_SGL_OVERFLOW, /* 0x404 */
  393. DMA_RX_DATA_LEN_OVERFLOW, /* 0x405 */
  394. DMA_RX_DATA_LEN_UNDERFLOW, /* 0x406 */
  395. DMA_RX_DATA_OFFSET_ERR, /* 0x407 */
  396. RESERVED10, /* 0x408 */
  397. DMA_RX_SATA_FRAME_TYPE_ERR, /* 0x409 */
  398. DMA_RX_RESP_BUF_OVERFLOW, /* 0x40a */
  399. DMA_RX_UNEXP_RETRANS_RESP_ERR, /* 0x40b */
  400. DMA_RX_UNEXP_NORM_RESP_ERR, /* 0x40c */
  401. DMA_RX_UNEXP_RDFRAME_ERR, /* 0x40d */
  402. DMA_RX_PIO_DATA_LEN_ERR, /* 0x40e */
  403. DMA_RX_RDSETUP_STATUS_ERR, /* 0x40f */
  404. DMA_RX_RDSETUP_STATUS_DRQ_ERR, /* 0x410 */
  405. DMA_RX_RDSETUP_STATUS_BSY_ERR, /* 0x411 */
  406. DMA_RX_RDSETUP_LEN_ODD_ERR, /* 0x412 */
  407. DMA_RX_RDSETUP_LEN_ZERO_ERR, /* 0x413 */
  408. DMA_RX_RDSETUP_LEN_OVER_ERR, /* 0x414 */
  409. DMA_RX_RDSETUP_OFFSET_ERR, /* 0x415 */
  410. DMA_RX_RDSETUP_ACTIVE_ERR, /* 0x416 */
  411. DMA_RX_RDSETUP_ESTATUS_ERR, /* 0x417 */
  412. DMA_RX_RAM_ECC_ERR, /* 0x418 */
  413. DMA_RX_UNKNOWN_FRM_ERR, /* 0x419 */
  414. };
  415. #define HISI_SAS_COMMAND_ENTRIES_V2_HW 4096
  416. #define DIR_NO_DATA 0
  417. #define DIR_TO_INI 1
  418. #define DIR_TO_DEVICE 2
  419. #define DIR_RESERVED 3
  420. #define SATA_PROTOCOL_NONDATA 0x1
  421. #define SATA_PROTOCOL_PIO 0x2
  422. #define SATA_PROTOCOL_DMA 0x4
  423. #define SATA_PROTOCOL_FPDMA 0x8
  424. #define SATA_PROTOCOL_ATAPI 0x10
  425. static u32 hisi_sas_read32(struct hisi_hba *hisi_hba, u32 off)
  426. {
  427. void __iomem *regs = hisi_hba->regs + off;
  428. return readl(regs);
  429. }
  430. static u32 hisi_sas_read32_relaxed(struct hisi_hba *hisi_hba, u32 off)
  431. {
  432. void __iomem *regs = hisi_hba->regs + off;
  433. return readl_relaxed(regs);
  434. }
  435. static void hisi_sas_write32(struct hisi_hba *hisi_hba, u32 off, u32 val)
  436. {
  437. void __iomem *regs = hisi_hba->regs + off;
  438. writel(val, regs);
  439. }
  440. static void hisi_sas_phy_write32(struct hisi_hba *hisi_hba, int phy_no,
  441. u32 off, u32 val)
  442. {
  443. void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
  444. writel(val, regs);
  445. }
  446. static u32 hisi_sas_phy_read32(struct hisi_hba *hisi_hba,
  447. int phy_no, u32 off)
  448. {
  449. void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
  450. return readl(regs);
  451. }
  452. /* This function needs to be protected from pre-emption. */
  453. static int
  454. slot_index_alloc_quirk_v2_hw(struct hisi_hba *hisi_hba, int *slot_idx,
  455. struct domain_device *device)
  456. {
  457. unsigned int index = 0;
  458. void *bitmap = hisi_hba->slot_index_tags;
  459. int sata_dev = dev_is_sata(device);
  460. while (1) {
  461. index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
  462. index);
  463. if (index >= hisi_hba->slot_index_count)
  464. return -SAS_QUEUE_FULL;
  465. /*
  466. * SAS IPTT bit0 should be 1
  467. */
  468. if (sata_dev || (index & 1))
  469. break;
  470. index++;
  471. }
  472. set_bit(index, bitmap);
  473. *slot_idx = index;
  474. return 0;
  475. }
  476. static struct
  477. hisi_sas_device *alloc_dev_quirk_v2_hw(struct domain_device *device)
  478. {
  479. struct hisi_hba *hisi_hba = device->port->ha->lldd_ha;
  480. struct hisi_sas_device *sas_dev = NULL;
  481. int i, sata_dev = dev_is_sata(device);
  482. spin_lock(&hisi_hba->lock);
  483. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  484. /*
  485. * SATA device id bit0 should be 0
  486. */
  487. if (sata_dev && (i & 1))
  488. continue;
  489. if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
  490. hisi_hba->devices[i].device_id = i;
  491. sas_dev = &hisi_hba->devices[i];
  492. sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
  493. sas_dev->dev_type = device->dev_type;
  494. sas_dev->hisi_hba = hisi_hba;
  495. sas_dev->sas_device = device;
  496. break;
  497. }
  498. }
  499. spin_unlock(&hisi_hba->lock);
  500. return sas_dev;
  501. }
  502. static void config_phy_opt_mode_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
  503. {
  504. u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
  505. cfg &= ~PHY_CFG_DC_OPT_MSK;
  506. cfg |= 1 << PHY_CFG_DC_OPT_OFF;
  507. hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
  508. }
  509. static void config_id_frame_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
  510. {
  511. struct sas_identify_frame identify_frame;
  512. u32 *identify_buffer;
  513. memset(&identify_frame, 0, sizeof(identify_frame));
  514. identify_frame.dev_type = SAS_END_DEVICE;
  515. identify_frame.frame_type = 0;
  516. identify_frame._un1 = 1;
  517. identify_frame.initiator_bits = SAS_PROTOCOL_ALL;
  518. identify_frame.target_bits = SAS_PROTOCOL_NONE;
  519. memcpy(&identify_frame._un4_11[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
  520. memcpy(&identify_frame.sas_addr[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
  521. identify_frame.phy_id = phy_no;
  522. identify_buffer = (u32 *)(&identify_frame);
  523. hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD0,
  524. __swab32(identify_buffer[0]));
  525. hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD1,
  526. __swab32(identify_buffer[1]));
  527. hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD2,
  528. __swab32(identify_buffer[2]));
  529. hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD3,
  530. __swab32(identify_buffer[3]));
  531. hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD4,
  532. __swab32(identify_buffer[4]));
  533. hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD5,
  534. __swab32(identify_buffer[5]));
  535. }
  536. static void setup_itct_v2_hw(struct hisi_hba *hisi_hba,
  537. struct hisi_sas_device *sas_dev)
  538. {
  539. struct domain_device *device = sas_dev->sas_device;
  540. struct device *dev = &hisi_hba->pdev->dev;
  541. u64 qw0, device_id = sas_dev->device_id;
  542. struct hisi_sas_itct *itct = &hisi_hba->itct[device_id];
  543. struct domain_device *parent_dev = device->parent;
  544. struct hisi_sas_port *port = device->port->lldd_port;
  545. memset(itct, 0, sizeof(*itct));
  546. /* qw0 */
  547. qw0 = 0;
  548. switch (sas_dev->dev_type) {
  549. case SAS_END_DEVICE:
  550. case SAS_EDGE_EXPANDER_DEVICE:
  551. case SAS_FANOUT_EXPANDER_DEVICE:
  552. qw0 = HISI_SAS_DEV_TYPE_SSP << ITCT_HDR_DEV_TYPE_OFF;
  553. break;
  554. case SAS_SATA_DEV:
  555. case SAS_SATA_PENDING:
  556. if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
  557. qw0 = HISI_SAS_DEV_TYPE_STP << ITCT_HDR_DEV_TYPE_OFF;
  558. else
  559. qw0 = HISI_SAS_DEV_TYPE_SATA << ITCT_HDR_DEV_TYPE_OFF;
  560. break;
  561. default:
  562. dev_warn(dev, "setup itct: unsupported dev type (%d)\n",
  563. sas_dev->dev_type);
  564. }
  565. qw0 |= ((1 << ITCT_HDR_VALID_OFF) |
  566. (device->linkrate << ITCT_HDR_MCR_OFF) |
  567. (1 << ITCT_HDR_VLN_OFF) |
  568. (port->id << ITCT_HDR_PORT_ID_OFF));
  569. itct->qw0 = cpu_to_le64(qw0);
  570. /* qw1 */
  571. memcpy(&itct->sas_addr, device->sas_addr, SAS_ADDR_SIZE);
  572. itct->sas_addr = __swab64(itct->sas_addr);
  573. /* qw2 */
  574. if (!dev_is_sata(device))
  575. itct->qw2 = cpu_to_le64((500ULL << ITCT_HDR_INLT_OFF) |
  576. (0x1ULL << ITCT_HDR_BITLT_OFF) |
  577. (0x32ULL << ITCT_HDR_MCTLT_OFF) |
  578. (0x1ULL << ITCT_HDR_RTOLT_OFF));
  579. }
  580. static void free_device_v2_hw(struct hisi_hba *hisi_hba,
  581. struct hisi_sas_device *sas_dev)
  582. {
  583. u64 qw0, dev_id = sas_dev->device_id;
  584. struct device *dev = &hisi_hba->pdev->dev;
  585. struct hisi_sas_itct *itct = &hisi_hba->itct[dev_id];
  586. u32 reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
  587. int i;
  588. /* clear the itct interrupt state */
  589. if (ENT_INT_SRC3_ITC_INT_MSK & reg_val)
  590. hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
  591. ENT_INT_SRC3_ITC_INT_MSK);
  592. /* clear the itct int*/
  593. for (i = 0; i < 2; i++) {
  594. /* clear the itct table*/
  595. reg_val = hisi_sas_read32(hisi_hba, ITCT_CLR);
  596. reg_val |= ITCT_CLR_EN_MSK | (dev_id & ITCT_DEV_MSK);
  597. hisi_sas_write32(hisi_hba, ITCT_CLR, reg_val);
  598. udelay(10);
  599. reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
  600. if (ENT_INT_SRC3_ITC_INT_MSK & reg_val) {
  601. dev_dbg(dev, "got clear ITCT done interrupt\n");
  602. /* invalid the itct state*/
  603. qw0 = cpu_to_le64(itct->qw0);
  604. qw0 &= ~(1 << ITCT_HDR_VALID_OFF);
  605. hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
  606. ENT_INT_SRC3_ITC_INT_MSK);
  607. hisi_hba->devices[dev_id].dev_type = SAS_PHY_UNUSED;
  608. hisi_hba->devices[dev_id].dev_status = HISI_SAS_DEV_NORMAL;
  609. /* clear the itct */
  610. hisi_sas_write32(hisi_hba, ITCT_CLR, 0);
  611. dev_dbg(dev, "clear ITCT ok\n");
  612. break;
  613. }
  614. }
  615. }
  616. static int reset_hw_v2_hw(struct hisi_hba *hisi_hba)
  617. {
  618. int i, reset_val;
  619. u32 val;
  620. unsigned long end_time;
  621. struct device *dev = &hisi_hba->pdev->dev;
  622. /* The mask needs to be set depending on the number of phys */
  623. if (hisi_hba->n_phy == 9)
  624. reset_val = 0x1fffff;
  625. else
  626. reset_val = 0x7ffff;
  627. hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0);
  628. /* Disable all of the PHYs */
  629. for (i = 0; i < hisi_hba->n_phy; i++) {
  630. u32 phy_cfg = hisi_sas_phy_read32(hisi_hba, i, PHY_CFG);
  631. phy_cfg &= ~PHY_CTRL_RESET_MSK;
  632. hisi_sas_phy_write32(hisi_hba, i, PHY_CFG, phy_cfg);
  633. }
  634. udelay(50);
  635. /* Ensure DMA tx & rx idle */
  636. for (i = 0; i < hisi_hba->n_phy; i++) {
  637. u32 dma_tx_status, dma_rx_status;
  638. end_time = jiffies + msecs_to_jiffies(1000);
  639. while (1) {
  640. dma_tx_status = hisi_sas_phy_read32(hisi_hba, i,
  641. DMA_TX_STATUS);
  642. dma_rx_status = hisi_sas_phy_read32(hisi_hba, i,
  643. DMA_RX_STATUS);
  644. if (!(dma_tx_status & DMA_TX_STATUS_BUSY_MSK) &&
  645. !(dma_rx_status & DMA_RX_STATUS_BUSY_MSK))
  646. break;
  647. msleep(20);
  648. if (time_after(jiffies, end_time))
  649. return -EIO;
  650. }
  651. }
  652. /* Ensure axi bus idle */
  653. end_time = jiffies + msecs_to_jiffies(1000);
  654. while (1) {
  655. u32 axi_status =
  656. hisi_sas_read32(hisi_hba, AXI_CFG);
  657. if (axi_status == 0)
  658. break;
  659. msleep(20);
  660. if (time_after(jiffies, end_time))
  661. return -EIO;
  662. }
  663. if (ACPI_HANDLE(dev)) {
  664. acpi_status s;
  665. s = acpi_evaluate_object(ACPI_HANDLE(dev), "_RST", NULL, NULL);
  666. if (ACPI_FAILURE(s)) {
  667. dev_err(dev, "Reset failed\n");
  668. return -EIO;
  669. }
  670. } else if (hisi_hba->ctrl) {
  671. /* reset and disable clock*/
  672. regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg,
  673. reset_val);
  674. regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg + 4,
  675. reset_val);
  676. msleep(1);
  677. regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg, &val);
  678. if (reset_val != (val & reset_val)) {
  679. dev_err(dev, "SAS reset fail.\n");
  680. return -EIO;
  681. }
  682. /* De-reset and enable clock*/
  683. regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg + 4,
  684. reset_val);
  685. regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg,
  686. reset_val);
  687. msleep(1);
  688. regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg,
  689. &val);
  690. if (val & reset_val) {
  691. dev_err(dev, "SAS de-reset fail.\n");
  692. return -EIO;
  693. }
  694. } else
  695. dev_warn(dev, "no reset method\n");
  696. return 0;
  697. }
  698. static void init_reg_v2_hw(struct hisi_hba *hisi_hba)
  699. {
  700. struct device *dev = &hisi_hba->pdev->dev;
  701. int i;
  702. /* Global registers init */
  703. /* Deal with am-max-transmissions quirk */
  704. if (device_property_present(dev, "hip06-sas-v2-quirk-amt")) {
  705. hisi_sas_write32(hisi_hba, AM_CFG_MAX_TRANS, 0x2020);
  706. hisi_sas_write32(hisi_hba, AM_CFG_SINGLE_PORT_MAX_TRANS,
  707. 0x2020);
  708. } /* Else, use defaults -> do nothing */
  709. hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
  710. (u32)((1ULL << hisi_hba->queue_count) - 1));
  711. hisi_sas_write32(hisi_hba, AXI_USER1, 0xc0000000);
  712. hisi_sas_write32(hisi_hba, AXI_USER2, 0x10000);
  713. hisi_sas_write32(hisi_hba, HGC_SAS_TXFAIL_RETRY_CTRL, 0x108);
  714. hisi_sas_write32(hisi_hba, HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL, 0x7FF);
  715. hisi_sas_write32(hisi_hba, OPENA_WT_CONTI_TIME, 0x1);
  716. hisi_sas_write32(hisi_hba, I_T_NEXUS_LOSS_TIME, 0x1F4);
  717. hisi_sas_write32(hisi_hba, MAX_CON_TIME_LIMIT_TIME, 0x32);
  718. hisi_sas_write32(hisi_hba, BUS_INACTIVE_LIMIT_TIME, 0x1);
  719. hisi_sas_write32(hisi_hba, CFG_AGING_TIME, 0x1);
  720. hisi_sas_write32(hisi_hba, HGC_ERR_STAT_EN, 0x1);
  721. hisi_sas_write32(hisi_hba, HGC_GET_ITV_TIME, 0x1);
  722. hisi_sas_write32(hisi_hba, INT_COAL_EN, 0x1);
  723. hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x1);
  724. hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x1);
  725. hisi_sas_write32(hisi_hba, ENT_INT_COAL_TIME, 0x1);
  726. hisi_sas_write32(hisi_hba, ENT_INT_COAL_CNT, 0x1);
  727. hisi_sas_write32(hisi_hba, OQ_INT_SRC, 0x0);
  728. hisi_sas_write32(hisi_hba, ENT_INT_SRC1, 0xffffffff);
  729. hisi_sas_write32(hisi_hba, ENT_INT_SRC2, 0xffffffff);
  730. hisi_sas_write32(hisi_hba, ENT_INT_SRC3, 0xffffffff);
  731. hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0x7efefefe);
  732. hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0x7efefefe);
  733. hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0x7ffffffe);
  734. hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xfffff3c0);
  735. for (i = 0; i < hisi_hba->queue_count; i++)
  736. hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK+0x4*i, 0);
  737. hisi_sas_write32(hisi_hba, AXI_AHB_CLK_CFG, 1);
  738. hisi_sas_write32(hisi_hba, HYPER_STREAM_ID_EN_CFG, 1);
  739. for (i = 0; i < hisi_hba->n_phy; i++) {
  740. hisi_sas_phy_write32(hisi_hba, i, PROG_PHY_LINK_RATE, 0x855);
  741. hisi_sas_phy_write32(hisi_hba, i, SAS_PHY_CTRL, 0x30b9908);
  742. hisi_sas_phy_write32(hisi_hba, i, SL_TOUT_CFG, 0x7d7d7d7d);
  743. hisi_sas_phy_write32(hisi_hba, i, SL_CONTROL, 0x0);
  744. hisi_sas_phy_write32(hisi_hba, i, TXID_AUTO, 0x2);
  745. hisi_sas_phy_write32(hisi_hba, i, DONE_RECEIVED_TIME, 0x10);
  746. hisi_sas_phy_write32(hisi_hba, i, CHL_INT0, 0xffffffff);
  747. hisi_sas_phy_write32(hisi_hba, i, CHL_INT1, 0xffffffff);
  748. hisi_sas_phy_write32(hisi_hba, i, CHL_INT2, 0xffffffff);
  749. hisi_sas_phy_write32(hisi_hba, i, RXOP_CHECK_CFG_H, 0x1000);
  750. hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xffffffff);
  751. hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0x8ffffbff);
  752. hisi_sas_phy_write32(hisi_hba, i, SL_CFG, 0x23f801fc);
  753. hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL_RDY_MSK, 0x0);
  754. hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_NOT_RDY_MSK, 0x0);
  755. hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_DWS_RESET_MSK, 0x0);
  756. hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_PHY_ENA_MSK, 0x0);
  757. hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x0);
  758. hisi_sas_phy_write32(hisi_hba, i, CHL_INT_COAL_EN, 0x0);
  759. hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_OOB_RESTART_MSK, 0x0);
  760. hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL, 0x199B694);
  761. }
  762. for (i = 0; i < hisi_hba->queue_count; i++) {
  763. /* Delivery queue */
  764. hisi_sas_write32(hisi_hba,
  765. DLVRY_Q_0_BASE_ADDR_HI + (i * 0x14),
  766. upper_32_bits(hisi_hba->cmd_hdr_dma[i]));
  767. hisi_sas_write32(hisi_hba, DLVRY_Q_0_BASE_ADDR_LO + (i * 0x14),
  768. lower_32_bits(hisi_hba->cmd_hdr_dma[i]));
  769. hisi_sas_write32(hisi_hba, DLVRY_Q_0_DEPTH + (i * 0x14),
  770. HISI_SAS_QUEUE_SLOTS);
  771. /* Completion queue */
  772. hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_HI + (i * 0x14),
  773. upper_32_bits(hisi_hba->complete_hdr_dma[i]));
  774. hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_LO + (i * 0x14),
  775. lower_32_bits(hisi_hba->complete_hdr_dma[i]));
  776. hisi_sas_write32(hisi_hba, COMPL_Q_0_DEPTH + (i * 0x14),
  777. HISI_SAS_QUEUE_SLOTS);
  778. }
  779. /* itct */
  780. hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_LO,
  781. lower_32_bits(hisi_hba->itct_dma));
  782. hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_HI,
  783. upper_32_bits(hisi_hba->itct_dma));
  784. /* iost */
  785. hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_LO,
  786. lower_32_bits(hisi_hba->iost_dma));
  787. hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_HI,
  788. upper_32_bits(hisi_hba->iost_dma));
  789. /* breakpoint */
  790. hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_LO,
  791. lower_32_bits(hisi_hba->breakpoint_dma));
  792. hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_HI,
  793. upper_32_bits(hisi_hba->breakpoint_dma));
  794. /* SATA broken msg */
  795. hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_LO,
  796. lower_32_bits(hisi_hba->sata_breakpoint_dma));
  797. hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_HI,
  798. upper_32_bits(hisi_hba->sata_breakpoint_dma));
  799. /* SATA initial fis */
  800. hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_LO,
  801. lower_32_bits(hisi_hba->initial_fis_dma));
  802. hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_HI,
  803. upper_32_bits(hisi_hba->initial_fis_dma));
  804. }
  805. static int hw_init_v2_hw(struct hisi_hba *hisi_hba)
  806. {
  807. struct device *dev = &hisi_hba->pdev->dev;
  808. int rc;
  809. rc = reset_hw_v2_hw(hisi_hba);
  810. if (rc) {
  811. dev_err(dev, "hisi_sas_reset_hw failed, rc=%d", rc);
  812. return rc;
  813. }
  814. msleep(100);
  815. init_reg_v2_hw(hisi_hba);
  816. return 0;
  817. }
  818. static void enable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
  819. {
  820. u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
  821. cfg |= PHY_CFG_ENA_MSK;
  822. hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
  823. }
  824. static void disable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
  825. {
  826. u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
  827. cfg &= ~PHY_CFG_ENA_MSK;
  828. hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
  829. }
  830. static void start_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
  831. {
  832. config_id_frame_v2_hw(hisi_hba, phy_no);
  833. config_phy_opt_mode_v2_hw(hisi_hba, phy_no);
  834. enable_phy_v2_hw(hisi_hba, phy_no);
  835. }
  836. static void stop_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
  837. {
  838. disable_phy_v2_hw(hisi_hba, phy_no);
  839. }
  840. static void phy_hard_reset_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
  841. {
  842. stop_phy_v2_hw(hisi_hba, phy_no);
  843. msleep(100);
  844. start_phy_v2_hw(hisi_hba, phy_no);
  845. }
  846. static void start_phys_v2_hw(unsigned long data)
  847. {
  848. struct hisi_hba *hisi_hba = (struct hisi_hba *)data;
  849. int i;
  850. for (i = 0; i < hisi_hba->n_phy; i++)
  851. start_phy_v2_hw(hisi_hba, i);
  852. }
  853. static void phys_init_v2_hw(struct hisi_hba *hisi_hba)
  854. {
  855. struct timer_list *timer = &hisi_hba->timer;
  856. setup_timer(timer, start_phys_v2_hw, (unsigned long)hisi_hba);
  857. mod_timer(timer, jiffies + HZ);
  858. }
  859. static void sl_notify_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
  860. {
  861. u32 sl_control;
  862. sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
  863. sl_control |= SL_CONTROL_NOTIFY_EN_MSK;
  864. hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
  865. msleep(1);
  866. sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
  867. sl_control &= ~SL_CONTROL_NOTIFY_EN_MSK;
  868. hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
  869. }
  870. static int get_wideport_bitmap_v2_hw(struct hisi_hba *hisi_hba, int port_id)
  871. {
  872. int i, bitmap = 0;
  873. u32 phy_port_num_ma = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
  874. u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
  875. for (i = 0; i < (hisi_hba->n_phy < 9 ? hisi_hba->n_phy : 8); i++)
  876. if (phy_state & 1 << i)
  877. if (((phy_port_num_ma >> (i * 4)) & 0xf) == port_id)
  878. bitmap |= 1 << i;
  879. if (hisi_hba->n_phy == 9) {
  880. u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
  881. if (phy_state & 1 << 8)
  882. if (((port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
  883. PORT_STATE_PHY8_PORT_NUM_OFF) == port_id)
  884. bitmap |= 1 << 9;
  885. }
  886. return bitmap;
  887. }
  888. /**
  889. * This function allocates across all queues to load balance.
  890. * Slots are allocated from queues in a round-robin fashion.
  891. *
  892. * The callpath to this function and upto writing the write
  893. * queue pointer should be safe from interruption.
  894. */
  895. static int get_free_slot_v2_hw(struct hisi_hba *hisi_hba, int *q, int *s)
  896. {
  897. struct device *dev = &hisi_hba->pdev->dev;
  898. struct hisi_sas_dq *dq;
  899. u32 r, w;
  900. int queue = hisi_hba->queue;
  901. while (1) {
  902. dq = &hisi_hba->dq[queue];
  903. w = dq->wr_point;
  904. r = hisi_sas_read32_relaxed(hisi_hba,
  905. DLVRY_Q_0_RD_PTR + (queue * 0x14));
  906. if (r == (w+1) % HISI_SAS_QUEUE_SLOTS) {
  907. queue = (queue + 1) % hisi_hba->queue_count;
  908. if (queue == hisi_hba->queue) {
  909. dev_warn(dev, "could not find free slot\n");
  910. return -EAGAIN;
  911. }
  912. continue;
  913. }
  914. break;
  915. }
  916. hisi_hba->queue = (queue + 1) % hisi_hba->queue_count;
  917. *q = queue;
  918. *s = w;
  919. return 0;
  920. }
  921. static void start_delivery_v2_hw(struct hisi_hba *hisi_hba)
  922. {
  923. int dlvry_queue = hisi_hba->slot_prep->dlvry_queue;
  924. int dlvry_queue_slot = hisi_hba->slot_prep->dlvry_queue_slot;
  925. struct hisi_sas_dq *dq = &hisi_hba->dq[dlvry_queue];
  926. dq->wr_point = ++dlvry_queue_slot % HISI_SAS_QUEUE_SLOTS;
  927. hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14),
  928. dq->wr_point);
  929. }
  930. static int prep_prd_sge_v2_hw(struct hisi_hba *hisi_hba,
  931. struct hisi_sas_slot *slot,
  932. struct hisi_sas_cmd_hdr *hdr,
  933. struct scatterlist *scatter,
  934. int n_elem)
  935. {
  936. struct device *dev = &hisi_hba->pdev->dev;
  937. struct scatterlist *sg;
  938. int i;
  939. if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
  940. dev_err(dev, "prd err: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
  941. n_elem);
  942. return -EINVAL;
  943. }
  944. slot->sge_page = dma_pool_alloc(hisi_hba->sge_page_pool, GFP_ATOMIC,
  945. &slot->sge_page_dma);
  946. if (!slot->sge_page)
  947. return -ENOMEM;
  948. for_each_sg(scatter, sg, n_elem, i) {
  949. struct hisi_sas_sge *entry = &slot->sge_page->sge[i];
  950. entry->addr = cpu_to_le64(sg_dma_address(sg));
  951. entry->page_ctrl_0 = entry->page_ctrl_1 = 0;
  952. entry->data_len = cpu_to_le32(sg_dma_len(sg));
  953. entry->data_off = 0;
  954. }
  955. hdr->prd_table_addr = cpu_to_le64(slot->sge_page_dma);
  956. hdr->sg_len = cpu_to_le32(n_elem << CMD_HDR_DATA_SGL_LEN_OFF);
  957. return 0;
  958. }
  959. static int prep_smp_v2_hw(struct hisi_hba *hisi_hba,
  960. struct hisi_sas_slot *slot)
  961. {
  962. struct sas_task *task = slot->task;
  963. struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
  964. struct domain_device *device = task->dev;
  965. struct device *dev = &hisi_hba->pdev->dev;
  966. struct hisi_sas_port *port = slot->port;
  967. struct scatterlist *sg_req, *sg_resp;
  968. struct hisi_sas_device *sas_dev = device->lldd_dev;
  969. dma_addr_t req_dma_addr;
  970. unsigned int req_len, resp_len;
  971. int elem, rc;
  972. /*
  973. * DMA-map SMP request, response buffers
  974. */
  975. /* req */
  976. sg_req = &task->smp_task.smp_req;
  977. elem = dma_map_sg(dev, sg_req, 1, DMA_TO_DEVICE);
  978. if (!elem)
  979. return -ENOMEM;
  980. req_len = sg_dma_len(sg_req);
  981. req_dma_addr = sg_dma_address(sg_req);
  982. /* resp */
  983. sg_resp = &task->smp_task.smp_resp;
  984. elem = dma_map_sg(dev, sg_resp, 1, DMA_FROM_DEVICE);
  985. if (!elem) {
  986. rc = -ENOMEM;
  987. goto err_out_req;
  988. }
  989. resp_len = sg_dma_len(sg_resp);
  990. if ((req_len & 0x3) || (resp_len & 0x3)) {
  991. rc = -EINVAL;
  992. goto err_out_resp;
  993. }
  994. /* create header */
  995. /* dw0 */
  996. hdr->dw0 = cpu_to_le32((port->id << CMD_HDR_PORT_OFF) |
  997. (1 << CMD_HDR_PRIORITY_OFF) | /* high pri */
  998. (2 << CMD_HDR_CMD_OFF)); /* smp */
  999. /* map itct entry */
  1000. hdr->dw1 = cpu_to_le32((sas_dev->device_id << CMD_HDR_DEV_ID_OFF) |
  1001. (1 << CMD_HDR_FRAME_TYPE_OFF) |
  1002. (DIR_NO_DATA << CMD_HDR_DIR_OFF));
  1003. /* dw2 */
  1004. hdr->dw2 = cpu_to_le32((((req_len - 4) / 4) << CMD_HDR_CFL_OFF) |
  1005. (HISI_SAS_MAX_SMP_RESP_SZ / 4 <<
  1006. CMD_HDR_MRFL_OFF));
  1007. hdr->transfer_tags = cpu_to_le32(slot->idx << CMD_HDR_IPTT_OFF);
  1008. hdr->cmd_table_addr = cpu_to_le64(req_dma_addr);
  1009. hdr->sts_buffer_addr = cpu_to_le64(slot->status_buffer_dma);
  1010. return 0;
  1011. err_out_resp:
  1012. dma_unmap_sg(dev, &slot->task->smp_task.smp_resp, 1,
  1013. DMA_FROM_DEVICE);
  1014. err_out_req:
  1015. dma_unmap_sg(dev, &slot->task->smp_task.smp_req, 1,
  1016. DMA_TO_DEVICE);
  1017. return rc;
  1018. }
  1019. static int prep_ssp_v2_hw(struct hisi_hba *hisi_hba,
  1020. struct hisi_sas_slot *slot, int is_tmf,
  1021. struct hisi_sas_tmf_task *tmf)
  1022. {
  1023. struct sas_task *task = slot->task;
  1024. struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
  1025. struct domain_device *device = task->dev;
  1026. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1027. struct hisi_sas_port *port = slot->port;
  1028. struct sas_ssp_task *ssp_task = &task->ssp_task;
  1029. struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
  1030. int has_data = 0, rc, priority = is_tmf;
  1031. u8 *buf_cmd;
  1032. u32 dw1 = 0, dw2 = 0;
  1033. hdr->dw0 = cpu_to_le32((1 << CMD_HDR_RESP_REPORT_OFF) |
  1034. (2 << CMD_HDR_TLR_CTRL_OFF) |
  1035. (port->id << CMD_HDR_PORT_OFF) |
  1036. (priority << CMD_HDR_PRIORITY_OFF) |
  1037. (1 << CMD_HDR_CMD_OFF)); /* ssp */
  1038. dw1 = 1 << CMD_HDR_VDTL_OFF;
  1039. if (is_tmf) {
  1040. dw1 |= 2 << CMD_HDR_FRAME_TYPE_OFF;
  1041. dw1 |= DIR_NO_DATA << CMD_HDR_DIR_OFF;
  1042. } else {
  1043. dw1 |= 1 << CMD_HDR_FRAME_TYPE_OFF;
  1044. switch (scsi_cmnd->sc_data_direction) {
  1045. case DMA_TO_DEVICE:
  1046. has_data = 1;
  1047. dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
  1048. break;
  1049. case DMA_FROM_DEVICE:
  1050. has_data = 1;
  1051. dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
  1052. break;
  1053. default:
  1054. dw1 &= ~CMD_HDR_DIR_MSK;
  1055. }
  1056. }
  1057. /* map itct entry */
  1058. dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
  1059. hdr->dw1 = cpu_to_le32(dw1);
  1060. dw2 = (((sizeof(struct ssp_command_iu) + sizeof(struct ssp_frame_hdr)
  1061. + 3) / 4) << CMD_HDR_CFL_OFF) |
  1062. ((HISI_SAS_MAX_SSP_RESP_SZ / 4) << CMD_HDR_MRFL_OFF) |
  1063. (2 << CMD_HDR_SG_MOD_OFF);
  1064. hdr->dw2 = cpu_to_le32(dw2);
  1065. hdr->transfer_tags = cpu_to_le32(slot->idx);
  1066. if (has_data) {
  1067. rc = prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
  1068. slot->n_elem);
  1069. if (rc)
  1070. return rc;
  1071. }
  1072. hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
  1073. hdr->cmd_table_addr = cpu_to_le64(slot->command_table_dma);
  1074. hdr->sts_buffer_addr = cpu_to_le64(slot->status_buffer_dma);
  1075. buf_cmd = slot->command_table + sizeof(struct ssp_frame_hdr);
  1076. memcpy(buf_cmd, &task->ssp_task.LUN, 8);
  1077. if (!is_tmf) {
  1078. buf_cmd[9] = task->ssp_task.task_attr |
  1079. (task->ssp_task.task_prio << 3);
  1080. memcpy(buf_cmd + 12, task->ssp_task.cmd->cmnd,
  1081. task->ssp_task.cmd->cmd_len);
  1082. } else {
  1083. buf_cmd[10] = tmf->tmf;
  1084. switch (tmf->tmf) {
  1085. case TMF_ABORT_TASK:
  1086. case TMF_QUERY_TASK:
  1087. buf_cmd[12] =
  1088. (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
  1089. buf_cmd[13] =
  1090. tmf->tag_of_task_to_be_managed & 0xff;
  1091. break;
  1092. default:
  1093. break;
  1094. }
  1095. }
  1096. return 0;
  1097. }
  1098. static void sata_done_v2_hw(struct hisi_hba *hisi_hba, struct sas_task *task,
  1099. struct hisi_sas_slot *slot)
  1100. {
  1101. struct task_status_struct *ts = &task->task_status;
  1102. struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
  1103. struct dev_to_host_fis *d2h = slot->status_buffer +
  1104. sizeof(struct hisi_sas_err_record);
  1105. resp->frame_len = sizeof(struct dev_to_host_fis);
  1106. memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
  1107. ts->buf_valid_size = sizeof(*resp);
  1108. }
  1109. /* by default, task resp is complete */
  1110. static void slot_err_v2_hw(struct hisi_hba *hisi_hba,
  1111. struct sas_task *task,
  1112. struct hisi_sas_slot *slot)
  1113. {
  1114. struct task_status_struct *ts = &task->task_status;
  1115. struct hisi_sas_err_record_v2 *err_record = slot->status_buffer;
  1116. u32 trans_tx_fail_type = cpu_to_le32(err_record->trans_tx_fail_type);
  1117. u32 trans_rx_fail_type = cpu_to_le32(err_record->trans_rx_fail_type);
  1118. u16 dma_tx_err_type = cpu_to_le16(err_record->dma_tx_err_type);
  1119. u16 sipc_rx_err_type = cpu_to_le16(err_record->sipc_rx_err_type);
  1120. u32 dma_rx_err_type = cpu_to_le32(err_record->dma_rx_err_type);
  1121. int error = -1;
  1122. if (dma_rx_err_type) {
  1123. error = ffs(dma_rx_err_type)
  1124. - 1 + DMA_RX_ERR_BASE;
  1125. } else if (sipc_rx_err_type) {
  1126. error = ffs(sipc_rx_err_type)
  1127. - 1 + SIPC_RX_ERR_BASE;
  1128. } else if (dma_tx_err_type) {
  1129. error = ffs(dma_tx_err_type)
  1130. - 1 + DMA_TX_ERR_BASE;
  1131. } else if (trans_rx_fail_type) {
  1132. error = ffs(trans_rx_fail_type)
  1133. - 1 + TRANS_RX_FAIL_BASE;
  1134. } else if (trans_tx_fail_type) {
  1135. error = ffs(trans_tx_fail_type)
  1136. - 1 + TRANS_TX_FAIL_BASE;
  1137. }
  1138. switch (task->task_proto) {
  1139. case SAS_PROTOCOL_SSP:
  1140. {
  1141. switch (error) {
  1142. case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
  1143. {
  1144. ts->stat = SAS_OPEN_REJECT;
  1145. ts->open_rej_reason = SAS_OREJ_NO_DEST;
  1146. break;
  1147. }
  1148. case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
  1149. {
  1150. ts->stat = SAS_OPEN_REJECT;
  1151. ts->open_rej_reason = SAS_OREJ_PATH_BLOCKED;
  1152. break;
  1153. }
  1154. case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
  1155. {
  1156. ts->stat = SAS_OPEN_REJECT;
  1157. ts->open_rej_reason = SAS_OREJ_EPROTO;
  1158. break;
  1159. }
  1160. case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
  1161. {
  1162. ts->stat = SAS_OPEN_REJECT;
  1163. ts->open_rej_reason = SAS_OREJ_CONN_RATE;
  1164. break;
  1165. }
  1166. case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
  1167. {
  1168. ts->stat = SAS_OPEN_REJECT;
  1169. ts->open_rej_reason = SAS_OREJ_BAD_DEST;
  1170. break;
  1171. }
  1172. case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
  1173. {
  1174. ts->stat = SAS_OPEN_REJECT;
  1175. ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
  1176. break;
  1177. }
  1178. case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
  1179. {
  1180. ts->stat = SAS_OPEN_REJECT;
  1181. ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
  1182. break;
  1183. }
  1184. case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
  1185. {
  1186. ts->stat = SAS_OPEN_REJECT;
  1187. ts->open_rej_reason = SAS_OREJ_UNKNOWN;
  1188. break;
  1189. }
  1190. case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
  1191. {
  1192. /* not sure */
  1193. ts->stat = SAS_DEV_NO_RESPONSE;
  1194. break;
  1195. }
  1196. case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
  1197. {
  1198. ts->stat = SAS_PHY_DOWN;
  1199. break;
  1200. }
  1201. case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
  1202. {
  1203. ts->stat = SAS_OPEN_TO;
  1204. break;
  1205. }
  1206. case DMA_RX_DATA_LEN_OVERFLOW:
  1207. {
  1208. ts->stat = SAS_DATA_OVERRUN;
  1209. ts->residual = 0;
  1210. break;
  1211. }
  1212. case DMA_RX_DATA_LEN_UNDERFLOW:
  1213. case SIPC_RX_DATA_UNDERFLOW_ERR:
  1214. {
  1215. ts->residual = trans_tx_fail_type;
  1216. ts->stat = SAS_DATA_UNDERRUN;
  1217. break;
  1218. }
  1219. case TRANS_TX_ERR_FRAME_TXED:
  1220. {
  1221. /* This will request a retry */
  1222. ts->stat = SAS_QUEUE_FULL;
  1223. slot->abort = 1;
  1224. break;
  1225. }
  1226. case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
  1227. case TRANS_TX_ERR_PHY_NOT_ENABLE:
  1228. case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
  1229. case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
  1230. case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
  1231. case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
  1232. case TRANS_TX_ERR_WITH_BREAK_REQUEST:
  1233. case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
  1234. case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
  1235. case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
  1236. case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
  1237. case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
  1238. case TRANS_TX_ERR_WITH_NAK_RECEVIED:
  1239. case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
  1240. case TRANS_TX_ERR_WITH_IPTT_CONFLICT:
  1241. case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
  1242. case TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR:
  1243. case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
  1244. case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
  1245. case TRANS_RX_ERR_WITH_BREAK_TIMEOUT:
  1246. case TRANS_RX_ERR_WITH_BREAK_REQUEST:
  1247. case TRANS_RX_ERR_WITH_BREAK_RECEVIED:
  1248. case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
  1249. case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
  1250. case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
  1251. case TRANS_RX_ERR_WITH_DATA_LEN0:
  1252. case TRANS_RX_ERR_WITH_BAD_HASH:
  1253. case TRANS_RX_XRDY_WLEN_ZERO_ERR:
  1254. case TRANS_RX_SSP_FRM_LEN_ERR:
  1255. case TRANS_RX_ERR_WITH_BAD_FRM_TYPE:
  1256. case DMA_TX_UNEXP_XFER_ERR:
  1257. case DMA_TX_UNEXP_RETRANS_ERR:
  1258. case DMA_TX_XFER_LEN_OVERFLOW:
  1259. case DMA_TX_XFER_OFFSET_ERR:
  1260. case DMA_RX_DATA_OFFSET_ERR:
  1261. case DMA_RX_UNEXP_NORM_RESP_ERR:
  1262. case DMA_RX_UNEXP_RDFRAME_ERR:
  1263. case DMA_RX_UNKNOWN_FRM_ERR:
  1264. {
  1265. ts->stat = SAS_OPEN_REJECT;
  1266. ts->open_rej_reason = SAS_OREJ_UNKNOWN;
  1267. break;
  1268. }
  1269. default:
  1270. break;
  1271. }
  1272. }
  1273. break;
  1274. case SAS_PROTOCOL_SMP:
  1275. ts->stat = SAM_STAT_CHECK_CONDITION;
  1276. break;
  1277. case SAS_PROTOCOL_SATA:
  1278. case SAS_PROTOCOL_STP:
  1279. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  1280. {
  1281. switch (error) {
  1282. case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
  1283. case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
  1284. case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
  1285. {
  1286. ts->resp = SAS_TASK_UNDELIVERED;
  1287. ts->stat = SAS_DEV_NO_RESPONSE;
  1288. break;
  1289. }
  1290. case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
  1291. case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
  1292. case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
  1293. case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
  1294. case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
  1295. case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
  1296. case TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY:
  1297. {
  1298. ts->stat = SAS_OPEN_REJECT;
  1299. break;
  1300. }
  1301. case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
  1302. {
  1303. ts->stat = SAS_OPEN_TO;
  1304. break;
  1305. }
  1306. case DMA_RX_DATA_LEN_OVERFLOW:
  1307. {
  1308. ts->stat = SAS_DATA_OVERRUN;
  1309. break;
  1310. }
  1311. case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
  1312. case TRANS_TX_ERR_PHY_NOT_ENABLE:
  1313. case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
  1314. case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
  1315. case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
  1316. case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
  1317. case TRANS_TX_ERR_WITH_BREAK_REQUEST:
  1318. case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
  1319. case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
  1320. case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
  1321. case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
  1322. case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
  1323. case TRANS_TX_ERR_WITH_NAK_RECEVIED:
  1324. case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
  1325. case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
  1326. case TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT:
  1327. case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
  1328. case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
  1329. case TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR:
  1330. case TRANS_RX_ERR_WITH_RXFIS_CRC_ERR:
  1331. case TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN:
  1332. case TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP:
  1333. case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
  1334. case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
  1335. case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
  1336. case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
  1337. case TRANS_RX_ERR_WITH_DATA_LEN0:
  1338. case TRANS_RX_ERR_WITH_BAD_HASH:
  1339. case TRANS_RX_XRDY_WLEN_ZERO_ERR:
  1340. case TRANS_RX_SSP_FRM_LEN_ERR:
  1341. case SIPC_RX_FIS_STATUS_ERR_BIT_VLD:
  1342. case SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR:
  1343. case SIPC_RX_FIS_STATUS_BSY_BIT_ERR:
  1344. case SIPC_RX_WRSETUP_LEN_ODD_ERR:
  1345. case SIPC_RX_WRSETUP_LEN_ZERO_ERR:
  1346. case SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR:
  1347. case SIPC_RX_SATA_UNEXP_FIS_ERR:
  1348. case DMA_RX_SATA_FRAME_TYPE_ERR:
  1349. case DMA_RX_UNEXP_RDFRAME_ERR:
  1350. case DMA_RX_PIO_DATA_LEN_ERR:
  1351. case DMA_RX_RDSETUP_STATUS_ERR:
  1352. case DMA_RX_RDSETUP_STATUS_DRQ_ERR:
  1353. case DMA_RX_RDSETUP_STATUS_BSY_ERR:
  1354. case DMA_RX_RDSETUP_LEN_ODD_ERR:
  1355. case DMA_RX_RDSETUP_LEN_ZERO_ERR:
  1356. case DMA_RX_RDSETUP_LEN_OVER_ERR:
  1357. case DMA_RX_RDSETUP_OFFSET_ERR:
  1358. case DMA_RX_RDSETUP_ACTIVE_ERR:
  1359. case DMA_RX_RDSETUP_ESTATUS_ERR:
  1360. case DMA_RX_UNKNOWN_FRM_ERR:
  1361. {
  1362. ts->stat = SAS_OPEN_REJECT;
  1363. break;
  1364. }
  1365. default:
  1366. {
  1367. ts->stat = SAS_PROTO_RESPONSE;
  1368. break;
  1369. }
  1370. }
  1371. sata_done_v2_hw(hisi_hba, task, slot);
  1372. }
  1373. break;
  1374. default:
  1375. break;
  1376. }
  1377. }
  1378. static int
  1379. slot_complete_v2_hw(struct hisi_hba *hisi_hba, struct hisi_sas_slot *slot,
  1380. int abort)
  1381. {
  1382. struct sas_task *task = slot->task;
  1383. struct hisi_sas_device *sas_dev;
  1384. struct device *dev = &hisi_hba->pdev->dev;
  1385. struct task_status_struct *ts;
  1386. struct domain_device *device;
  1387. enum exec_status sts;
  1388. struct hisi_sas_complete_v2_hdr *complete_queue =
  1389. hisi_hba->complete_hdr[slot->cmplt_queue];
  1390. struct hisi_sas_complete_v2_hdr *complete_hdr =
  1391. &complete_queue[slot->cmplt_queue_slot];
  1392. if (unlikely(!task || !task->lldd_task || !task->dev))
  1393. return -EINVAL;
  1394. ts = &task->task_status;
  1395. device = task->dev;
  1396. sas_dev = device->lldd_dev;
  1397. task->task_state_flags &=
  1398. ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
  1399. task->task_state_flags |= SAS_TASK_STATE_DONE;
  1400. memset(ts, 0, sizeof(*ts));
  1401. ts->resp = SAS_TASK_COMPLETE;
  1402. if (unlikely(!sas_dev || abort)) {
  1403. if (!sas_dev)
  1404. dev_dbg(dev, "slot complete: port has not device\n");
  1405. ts->stat = SAS_PHY_DOWN;
  1406. goto out;
  1407. }
  1408. /* Use SAS+TMF status codes */
  1409. switch ((complete_hdr->dw0 & CMPLT_HDR_ABORT_STAT_MSK)
  1410. >> CMPLT_HDR_ABORT_STAT_OFF) {
  1411. case STAT_IO_ABORTED:
  1412. /* this io has been aborted by abort command */
  1413. ts->stat = SAS_ABORTED_TASK;
  1414. goto out;
  1415. case STAT_IO_COMPLETE:
  1416. /* internal abort command complete */
  1417. ts->stat = TMF_RESP_FUNC_COMPLETE;
  1418. goto out;
  1419. case STAT_IO_NO_DEVICE:
  1420. ts->stat = TMF_RESP_FUNC_COMPLETE;
  1421. goto out;
  1422. case STAT_IO_NOT_VALID:
  1423. /* abort single io, controller don't find
  1424. * the io need to abort
  1425. */
  1426. ts->stat = TMF_RESP_FUNC_FAILED;
  1427. goto out;
  1428. default:
  1429. break;
  1430. }
  1431. if ((complete_hdr->dw0 & CMPLT_HDR_ERX_MSK) &&
  1432. (!(complete_hdr->dw0 & CMPLT_HDR_RSPNS_XFRD_MSK))) {
  1433. slot_err_v2_hw(hisi_hba, task, slot);
  1434. if (unlikely(slot->abort)) {
  1435. queue_work(hisi_hba->wq, &slot->abort_slot);
  1436. /* immediately return and do not complete */
  1437. return ts->stat;
  1438. }
  1439. goto out;
  1440. }
  1441. switch (task->task_proto) {
  1442. case SAS_PROTOCOL_SSP:
  1443. {
  1444. struct ssp_response_iu *iu = slot->status_buffer +
  1445. sizeof(struct hisi_sas_err_record);
  1446. sas_ssp_task_response(dev, task, iu);
  1447. break;
  1448. }
  1449. case SAS_PROTOCOL_SMP:
  1450. {
  1451. struct scatterlist *sg_resp = &task->smp_task.smp_resp;
  1452. void *to;
  1453. ts->stat = SAM_STAT_GOOD;
  1454. to = kmap_atomic(sg_page(sg_resp));
  1455. dma_unmap_sg(dev, &task->smp_task.smp_resp, 1,
  1456. DMA_FROM_DEVICE);
  1457. dma_unmap_sg(dev, &task->smp_task.smp_req, 1,
  1458. DMA_TO_DEVICE);
  1459. memcpy(to + sg_resp->offset,
  1460. slot->status_buffer +
  1461. sizeof(struct hisi_sas_err_record),
  1462. sg_dma_len(sg_resp));
  1463. kunmap_atomic(to);
  1464. break;
  1465. }
  1466. case SAS_PROTOCOL_SATA:
  1467. case SAS_PROTOCOL_STP:
  1468. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  1469. {
  1470. ts->stat = SAM_STAT_GOOD;
  1471. sata_done_v2_hw(hisi_hba, task, slot);
  1472. break;
  1473. }
  1474. default:
  1475. ts->stat = SAM_STAT_CHECK_CONDITION;
  1476. break;
  1477. }
  1478. if (!slot->port->port_attached) {
  1479. dev_err(dev, "slot complete: port %d has removed\n",
  1480. slot->port->sas_port.id);
  1481. ts->stat = SAS_PHY_DOWN;
  1482. }
  1483. out:
  1484. if (sas_dev && sas_dev->running_req)
  1485. sas_dev->running_req--;
  1486. hisi_sas_slot_task_free(hisi_hba, task, slot);
  1487. sts = ts->stat;
  1488. if (task->task_done)
  1489. task->task_done(task);
  1490. return sts;
  1491. }
  1492. static u8 get_ata_protocol(u8 cmd, int direction)
  1493. {
  1494. switch (cmd) {
  1495. case ATA_CMD_FPDMA_WRITE:
  1496. case ATA_CMD_FPDMA_READ:
  1497. case ATA_CMD_FPDMA_RECV:
  1498. case ATA_CMD_FPDMA_SEND:
  1499. case ATA_CMD_NCQ_NON_DATA:
  1500. return SATA_PROTOCOL_FPDMA;
  1501. case ATA_CMD_ID_ATA:
  1502. case ATA_CMD_PMP_READ:
  1503. case ATA_CMD_READ_LOG_EXT:
  1504. case ATA_CMD_PIO_READ:
  1505. case ATA_CMD_PIO_READ_EXT:
  1506. case ATA_CMD_PMP_WRITE:
  1507. case ATA_CMD_WRITE_LOG_EXT:
  1508. case ATA_CMD_PIO_WRITE:
  1509. case ATA_CMD_PIO_WRITE_EXT:
  1510. return SATA_PROTOCOL_PIO;
  1511. case ATA_CMD_READ:
  1512. case ATA_CMD_READ_EXT:
  1513. case ATA_CMD_READ_LOG_DMA_EXT:
  1514. case ATA_CMD_WRITE:
  1515. case ATA_CMD_WRITE_EXT:
  1516. case ATA_CMD_WRITE_QUEUED:
  1517. case ATA_CMD_WRITE_LOG_DMA_EXT:
  1518. return SATA_PROTOCOL_DMA;
  1519. case ATA_CMD_DOWNLOAD_MICRO:
  1520. case ATA_CMD_DEV_RESET:
  1521. case ATA_CMD_CHK_POWER:
  1522. case ATA_CMD_FLUSH:
  1523. case ATA_CMD_FLUSH_EXT:
  1524. case ATA_CMD_VERIFY:
  1525. case ATA_CMD_VERIFY_EXT:
  1526. case ATA_CMD_SET_FEATURES:
  1527. case ATA_CMD_STANDBY:
  1528. case ATA_CMD_STANDBYNOW1:
  1529. return SATA_PROTOCOL_NONDATA;
  1530. default:
  1531. if (direction == DMA_NONE)
  1532. return SATA_PROTOCOL_NONDATA;
  1533. return SATA_PROTOCOL_PIO;
  1534. }
  1535. }
  1536. static int get_ncq_tag_v2_hw(struct sas_task *task, u32 *tag)
  1537. {
  1538. struct ata_queued_cmd *qc = task->uldd_task;
  1539. if (qc) {
  1540. if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  1541. qc->tf.command == ATA_CMD_FPDMA_READ) {
  1542. *tag = qc->tag;
  1543. return 1;
  1544. }
  1545. }
  1546. return 0;
  1547. }
  1548. static int prep_ata_v2_hw(struct hisi_hba *hisi_hba,
  1549. struct hisi_sas_slot *slot)
  1550. {
  1551. struct sas_task *task = slot->task;
  1552. struct domain_device *device = task->dev;
  1553. struct domain_device *parent_dev = device->parent;
  1554. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1555. struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
  1556. struct hisi_sas_port *port = device->port->lldd_port;
  1557. u8 *buf_cmd;
  1558. int has_data = 0, rc = 0, hdr_tag = 0;
  1559. u32 dw1 = 0, dw2 = 0;
  1560. /* create header */
  1561. /* dw0 */
  1562. hdr->dw0 = cpu_to_le32(port->id << CMD_HDR_PORT_OFF);
  1563. if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
  1564. hdr->dw0 |= cpu_to_le32(3 << CMD_HDR_CMD_OFF);
  1565. else
  1566. hdr->dw0 |= cpu_to_le32(4 << CMD_HDR_CMD_OFF);
  1567. /* dw1 */
  1568. switch (task->data_dir) {
  1569. case DMA_TO_DEVICE:
  1570. has_data = 1;
  1571. dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
  1572. break;
  1573. case DMA_FROM_DEVICE:
  1574. has_data = 1;
  1575. dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
  1576. break;
  1577. default:
  1578. dw1 &= ~CMD_HDR_DIR_MSK;
  1579. }
  1580. if (0 == task->ata_task.fis.command)
  1581. dw1 |= 1 << CMD_HDR_RESET_OFF;
  1582. dw1 |= (get_ata_protocol(task->ata_task.fis.command, task->data_dir))
  1583. << CMD_HDR_FRAME_TYPE_OFF;
  1584. dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
  1585. hdr->dw1 = cpu_to_le32(dw1);
  1586. /* dw2 */
  1587. if (task->ata_task.use_ncq && get_ncq_tag_v2_hw(task, &hdr_tag)) {
  1588. task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
  1589. dw2 |= hdr_tag << CMD_HDR_NCQ_TAG_OFF;
  1590. }
  1591. dw2 |= (HISI_SAS_MAX_STP_RESP_SZ / 4) << CMD_HDR_CFL_OFF |
  1592. 2 << CMD_HDR_SG_MOD_OFF;
  1593. hdr->dw2 = cpu_to_le32(dw2);
  1594. /* dw3 */
  1595. hdr->transfer_tags = cpu_to_le32(slot->idx);
  1596. if (has_data) {
  1597. rc = prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
  1598. slot->n_elem);
  1599. if (rc)
  1600. return rc;
  1601. }
  1602. hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
  1603. hdr->cmd_table_addr = cpu_to_le64(slot->command_table_dma);
  1604. hdr->sts_buffer_addr = cpu_to_le64(slot->status_buffer_dma);
  1605. buf_cmd = slot->command_table;
  1606. if (likely(!task->ata_task.device_control_reg_update))
  1607. task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
  1608. /* fill in command FIS */
  1609. memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
  1610. return 0;
  1611. }
  1612. static int prep_abort_v2_hw(struct hisi_hba *hisi_hba,
  1613. struct hisi_sas_slot *slot,
  1614. int device_id, int abort_flag, int tag_to_abort)
  1615. {
  1616. struct sas_task *task = slot->task;
  1617. struct domain_device *dev = task->dev;
  1618. struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
  1619. struct hisi_sas_port *port = slot->port;
  1620. /* dw0 */
  1621. hdr->dw0 = cpu_to_le32((5 << CMD_HDR_CMD_OFF) | /*abort*/
  1622. (port->id << CMD_HDR_PORT_OFF) |
  1623. ((dev_is_sata(dev) ? 1:0) <<
  1624. CMD_HDR_ABORT_DEVICE_TYPE_OFF) |
  1625. (abort_flag << CMD_HDR_ABORT_FLAG_OFF));
  1626. /* dw1 */
  1627. hdr->dw1 = cpu_to_le32(device_id << CMD_HDR_DEV_ID_OFF);
  1628. /* dw7 */
  1629. hdr->dw7 = cpu_to_le32(tag_to_abort << CMD_HDR_ABORT_IPTT_OFF);
  1630. hdr->transfer_tags = cpu_to_le32(slot->idx);
  1631. return 0;
  1632. }
  1633. static int phy_up_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
  1634. {
  1635. int i, res = 0;
  1636. u32 context, port_id, link_rate, hard_phy_linkrate;
  1637. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  1638. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  1639. struct device *dev = &hisi_hba->pdev->dev;
  1640. u32 *frame_rcvd = (u32 *)sas_phy->frame_rcvd;
  1641. struct sas_identify_frame *id = (struct sas_identify_frame *)frame_rcvd;
  1642. hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 1);
  1643. /* Check for SATA dev */
  1644. context = hisi_sas_read32(hisi_hba, PHY_CONTEXT);
  1645. if (context & (1 << phy_no))
  1646. goto end;
  1647. if (phy_no == 8) {
  1648. u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
  1649. port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
  1650. PORT_STATE_PHY8_PORT_NUM_OFF;
  1651. link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
  1652. PORT_STATE_PHY8_CONN_RATE_OFF;
  1653. } else {
  1654. port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
  1655. port_id = (port_id >> (4 * phy_no)) & 0xf;
  1656. link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
  1657. link_rate = (link_rate >> (phy_no * 4)) & 0xf;
  1658. }
  1659. if (port_id == 0xf) {
  1660. dev_err(dev, "phyup: phy%d invalid portid\n", phy_no);
  1661. res = IRQ_NONE;
  1662. goto end;
  1663. }
  1664. for (i = 0; i < 6; i++) {
  1665. u32 idaf = hisi_sas_phy_read32(hisi_hba, phy_no,
  1666. RX_IDAF_DWORD0 + (i * 4));
  1667. frame_rcvd[i] = __swab32(idaf);
  1668. }
  1669. sas_phy->linkrate = link_rate;
  1670. hard_phy_linkrate = hisi_sas_phy_read32(hisi_hba, phy_no,
  1671. HARD_PHY_LINKRATE);
  1672. phy->maximum_linkrate = hard_phy_linkrate & 0xf;
  1673. phy->minimum_linkrate = (hard_phy_linkrate >> 4) & 0xf;
  1674. sas_phy->oob_mode = SAS_OOB_MODE;
  1675. memcpy(sas_phy->attached_sas_addr, &id->sas_addr, SAS_ADDR_SIZE);
  1676. dev_info(dev, "phyup: phy%d link_rate=%d\n", phy_no, link_rate);
  1677. phy->port_id = port_id;
  1678. phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
  1679. phy->phy_type |= PORT_TYPE_SAS;
  1680. phy->phy_attached = 1;
  1681. phy->identify.device_type = id->dev_type;
  1682. phy->frame_rcvd_size = sizeof(struct sas_identify_frame);
  1683. if (phy->identify.device_type == SAS_END_DEVICE)
  1684. phy->identify.target_port_protocols =
  1685. SAS_PROTOCOL_SSP;
  1686. else if (phy->identify.device_type != SAS_PHY_UNUSED)
  1687. phy->identify.target_port_protocols =
  1688. SAS_PROTOCOL_SMP;
  1689. queue_work(hisi_hba->wq, &phy->phyup_ws);
  1690. end:
  1691. hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
  1692. CHL_INT0_SL_PHY_ENABLE_MSK);
  1693. hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 0);
  1694. return res;
  1695. }
  1696. static int phy_down_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
  1697. {
  1698. int res = 0;
  1699. u32 phy_state, sl_ctrl, txid_auto;
  1700. hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 1);
  1701. phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
  1702. hisi_sas_phy_down(hisi_hba, phy_no, (phy_state & 1 << phy_no) ? 1 : 0);
  1703. sl_ctrl = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
  1704. hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL,
  1705. sl_ctrl & ~SL_CONTROL_CTA_MSK);
  1706. txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
  1707. hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
  1708. txid_auto | TXID_AUTO_CT3_MSK);
  1709. hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0, CHL_INT0_NOT_RDY_MSK);
  1710. hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 0);
  1711. return res;
  1712. }
  1713. static irqreturn_t int_phy_updown_v2_hw(int irq_no, void *p)
  1714. {
  1715. struct hisi_hba *hisi_hba = p;
  1716. u32 irq_msk;
  1717. int phy_no = 0;
  1718. irqreturn_t res = IRQ_HANDLED;
  1719. irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO)
  1720. >> HGC_INVLD_DQE_INFO_FB_CH0_OFF) & 0x1ff;
  1721. while (irq_msk) {
  1722. if (irq_msk & 1) {
  1723. u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no,
  1724. CHL_INT0);
  1725. if (irq_value & CHL_INT0_SL_PHY_ENABLE_MSK)
  1726. /* phy up */
  1727. if (phy_up_v2_hw(phy_no, hisi_hba)) {
  1728. res = IRQ_NONE;
  1729. goto end;
  1730. }
  1731. if (irq_value & CHL_INT0_NOT_RDY_MSK)
  1732. /* phy down */
  1733. if (phy_down_v2_hw(phy_no, hisi_hba)) {
  1734. res = IRQ_NONE;
  1735. goto end;
  1736. }
  1737. }
  1738. irq_msk >>= 1;
  1739. phy_no++;
  1740. }
  1741. end:
  1742. return res;
  1743. }
  1744. static void phy_bcast_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
  1745. {
  1746. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  1747. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  1748. struct sas_ha_struct *sas_ha = &hisi_hba->sha;
  1749. hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 1);
  1750. sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
  1751. hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
  1752. CHL_INT0_SL_RX_BCST_ACK_MSK);
  1753. hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 0);
  1754. }
  1755. static irqreturn_t int_chnl_int_v2_hw(int irq_no, void *p)
  1756. {
  1757. struct hisi_hba *hisi_hba = p;
  1758. struct device *dev = &hisi_hba->pdev->dev;
  1759. u32 ent_msk, ent_tmp, irq_msk;
  1760. int phy_no = 0;
  1761. ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
  1762. ent_tmp = ent_msk;
  1763. ent_msk |= ENT_INT_SRC_MSK3_ENT95_MSK_MSK;
  1764. hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_msk);
  1765. irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO) >>
  1766. HGC_INVLD_DQE_INFO_FB_CH3_OFF) & 0x1ff;
  1767. while (irq_msk) {
  1768. if (irq_msk & (1 << phy_no)) {
  1769. u32 irq_value0 = hisi_sas_phy_read32(hisi_hba, phy_no,
  1770. CHL_INT0);
  1771. u32 irq_value1 = hisi_sas_phy_read32(hisi_hba, phy_no,
  1772. CHL_INT1);
  1773. u32 irq_value2 = hisi_sas_phy_read32(hisi_hba, phy_no,
  1774. CHL_INT2);
  1775. if (irq_value1) {
  1776. if (irq_value1 & (CHL_INT1_DMAC_RX_ECC_ERR_MSK |
  1777. CHL_INT1_DMAC_TX_ECC_ERR_MSK))
  1778. panic("%s: DMAC RX/TX ecc bad error! (0x%x)",
  1779. dev_name(dev), irq_value1);
  1780. hisi_sas_phy_write32(hisi_hba, phy_no,
  1781. CHL_INT1, irq_value1);
  1782. }
  1783. if (irq_value2)
  1784. hisi_sas_phy_write32(hisi_hba, phy_no,
  1785. CHL_INT2, irq_value2);
  1786. if (irq_value0) {
  1787. if (irq_value0 & CHL_INT0_SL_RX_BCST_ACK_MSK)
  1788. phy_bcast_v2_hw(phy_no, hisi_hba);
  1789. hisi_sas_phy_write32(hisi_hba, phy_no,
  1790. CHL_INT0, irq_value0
  1791. & (~CHL_INT0_HOTPLUG_TOUT_MSK)
  1792. & (~CHL_INT0_SL_PHY_ENABLE_MSK)
  1793. & (~CHL_INT0_NOT_RDY_MSK));
  1794. }
  1795. }
  1796. irq_msk &= ~(1 << phy_no);
  1797. phy_no++;
  1798. }
  1799. hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_tmp);
  1800. return IRQ_HANDLED;
  1801. }
  1802. static irqreturn_t cq_interrupt_v2_hw(int irq_no, void *p)
  1803. {
  1804. struct hisi_sas_cq *cq = p;
  1805. struct hisi_hba *hisi_hba = cq->hisi_hba;
  1806. struct hisi_sas_slot *slot;
  1807. struct hisi_sas_itct *itct;
  1808. struct hisi_sas_complete_v2_hdr *complete_queue;
  1809. u32 irq_value, rd_point = cq->rd_point, wr_point, dev_id;
  1810. int queue = cq->id;
  1811. complete_queue = hisi_hba->complete_hdr[queue];
  1812. irq_value = hisi_sas_read32(hisi_hba, OQ_INT_SRC);
  1813. hisi_sas_write32(hisi_hba, OQ_INT_SRC, 1 << queue);
  1814. wr_point = hisi_sas_read32(hisi_hba, COMPL_Q_0_WR_PTR +
  1815. (0x14 * queue));
  1816. while (rd_point != wr_point) {
  1817. struct hisi_sas_complete_v2_hdr *complete_hdr;
  1818. int iptt;
  1819. complete_hdr = &complete_queue[rd_point];
  1820. /* Check for NCQ completion */
  1821. if (complete_hdr->act) {
  1822. u32 act_tmp = complete_hdr->act;
  1823. int ncq_tag_count = ffs(act_tmp);
  1824. dev_id = (complete_hdr->dw1 & CMPLT_HDR_DEV_ID_MSK) >>
  1825. CMPLT_HDR_DEV_ID_OFF;
  1826. itct = &hisi_hba->itct[dev_id];
  1827. /* The NCQ tags are held in the itct header */
  1828. while (ncq_tag_count) {
  1829. __le64 *ncq_tag = &itct->qw4_15[0];
  1830. ncq_tag_count -= 1;
  1831. iptt = (ncq_tag[ncq_tag_count / 5]
  1832. >> (ncq_tag_count % 5) * 12) & 0xfff;
  1833. slot = &hisi_hba->slot_info[iptt];
  1834. slot->cmplt_queue_slot = rd_point;
  1835. slot->cmplt_queue = queue;
  1836. slot_complete_v2_hw(hisi_hba, slot, 0);
  1837. act_tmp &= ~(1 << ncq_tag_count);
  1838. ncq_tag_count = ffs(act_tmp);
  1839. }
  1840. } else {
  1841. iptt = (complete_hdr->dw1) & CMPLT_HDR_IPTT_MSK;
  1842. slot = &hisi_hba->slot_info[iptt];
  1843. slot->cmplt_queue_slot = rd_point;
  1844. slot->cmplt_queue = queue;
  1845. slot_complete_v2_hw(hisi_hba, slot, 0);
  1846. }
  1847. if (++rd_point >= HISI_SAS_QUEUE_SLOTS)
  1848. rd_point = 0;
  1849. }
  1850. /* update rd_point */
  1851. cq->rd_point = rd_point;
  1852. hisi_sas_write32(hisi_hba, COMPL_Q_0_RD_PTR + (0x14 * queue), rd_point);
  1853. return IRQ_HANDLED;
  1854. }
  1855. static irqreturn_t sata_int_v2_hw(int irq_no, void *p)
  1856. {
  1857. struct hisi_sas_phy *phy = p;
  1858. struct hisi_hba *hisi_hba = phy->hisi_hba;
  1859. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  1860. struct device *dev = &hisi_hba->pdev->dev;
  1861. struct hisi_sas_initial_fis *initial_fis;
  1862. struct dev_to_host_fis *fis;
  1863. u32 ent_tmp, ent_msk, ent_int, port_id, link_rate, hard_phy_linkrate;
  1864. irqreturn_t res = IRQ_HANDLED;
  1865. u8 attached_sas_addr[SAS_ADDR_SIZE] = {0};
  1866. int phy_no, offset;
  1867. phy_no = sas_phy->id;
  1868. initial_fis = &hisi_hba->initial_fis[phy_no];
  1869. fis = &initial_fis->fis;
  1870. offset = 4 * (phy_no / 4);
  1871. ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK1 + offset);
  1872. hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset,
  1873. ent_msk | 1 << ((phy_no % 4) * 8));
  1874. ent_int = hisi_sas_read32(hisi_hba, ENT_INT_SRC1 + offset);
  1875. ent_tmp = ent_int & (1 << (ENT_INT_SRC1_D2H_FIS_CH1_OFF *
  1876. (phy_no % 4)));
  1877. ent_int >>= ENT_INT_SRC1_D2H_FIS_CH1_OFF * (phy_no % 4);
  1878. if ((ent_int & ENT_INT_SRC1_D2H_FIS_CH0_MSK) == 0) {
  1879. dev_warn(dev, "sata int: phy%d did not receive FIS\n", phy_no);
  1880. res = IRQ_NONE;
  1881. goto end;
  1882. }
  1883. if (unlikely(phy_no == 8)) {
  1884. u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
  1885. port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
  1886. PORT_STATE_PHY8_PORT_NUM_OFF;
  1887. link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
  1888. PORT_STATE_PHY8_CONN_RATE_OFF;
  1889. } else {
  1890. port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
  1891. port_id = (port_id >> (4 * phy_no)) & 0xf;
  1892. link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
  1893. link_rate = (link_rate >> (phy_no * 4)) & 0xf;
  1894. }
  1895. if (port_id == 0xf) {
  1896. dev_err(dev, "sata int: phy%d invalid portid\n", phy_no);
  1897. res = IRQ_NONE;
  1898. goto end;
  1899. }
  1900. sas_phy->linkrate = link_rate;
  1901. hard_phy_linkrate = hisi_sas_phy_read32(hisi_hba, phy_no,
  1902. HARD_PHY_LINKRATE);
  1903. phy->maximum_linkrate = hard_phy_linkrate & 0xf;
  1904. phy->minimum_linkrate = (hard_phy_linkrate >> 4) & 0xf;
  1905. sas_phy->oob_mode = SATA_OOB_MODE;
  1906. /* Make up some unique SAS address */
  1907. attached_sas_addr[0] = 0x50;
  1908. attached_sas_addr[7] = phy_no;
  1909. memcpy(sas_phy->attached_sas_addr, attached_sas_addr, SAS_ADDR_SIZE);
  1910. memcpy(sas_phy->frame_rcvd, fis, sizeof(struct dev_to_host_fis));
  1911. dev_info(dev, "sata int phyup: phy%d link_rate=%d\n", phy_no, link_rate);
  1912. phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
  1913. phy->port_id = port_id;
  1914. phy->phy_type |= PORT_TYPE_SATA;
  1915. phy->phy_attached = 1;
  1916. phy->identify.device_type = SAS_SATA_DEV;
  1917. phy->frame_rcvd_size = sizeof(struct dev_to_host_fis);
  1918. phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
  1919. queue_work(hisi_hba->wq, &phy->phyup_ws);
  1920. end:
  1921. hisi_sas_write32(hisi_hba, ENT_INT_SRC1 + offset, ent_tmp);
  1922. hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset, ent_msk);
  1923. return res;
  1924. }
  1925. static irq_handler_t phy_interrupts[HISI_SAS_PHY_INT_NR] = {
  1926. int_phy_updown_v2_hw,
  1927. int_chnl_int_v2_hw,
  1928. };
  1929. /**
  1930. * There is a limitation in the hip06 chipset that we need
  1931. * to map in all mbigen interrupts, even if they are not used.
  1932. */
  1933. static int interrupt_init_v2_hw(struct hisi_hba *hisi_hba)
  1934. {
  1935. struct platform_device *pdev = hisi_hba->pdev;
  1936. struct device *dev = &pdev->dev;
  1937. int i, irq, rc, irq_map[128];
  1938. for (i = 0; i < 128; i++)
  1939. irq_map[i] = platform_get_irq(pdev, i);
  1940. for (i = 0; i < HISI_SAS_PHY_INT_NR; i++) {
  1941. int idx = i;
  1942. irq = irq_map[idx + 1]; /* Phy up/down is irq1 */
  1943. if (!irq) {
  1944. dev_err(dev, "irq init: fail map phy interrupt %d\n",
  1945. idx);
  1946. return -ENOENT;
  1947. }
  1948. rc = devm_request_irq(dev, irq, phy_interrupts[i], 0,
  1949. DRV_NAME " phy", hisi_hba);
  1950. if (rc) {
  1951. dev_err(dev, "irq init: could not request "
  1952. "phy interrupt %d, rc=%d\n",
  1953. irq, rc);
  1954. return -ENOENT;
  1955. }
  1956. }
  1957. for (i = 0; i < hisi_hba->n_phy; i++) {
  1958. struct hisi_sas_phy *phy = &hisi_hba->phy[i];
  1959. int idx = i + 72; /* First SATA interrupt is irq72 */
  1960. irq = irq_map[idx];
  1961. if (!irq) {
  1962. dev_err(dev, "irq init: fail map phy interrupt %d\n",
  1963. idx);
  1964. return -ENOENT;
  1965. }
  1966. rc = devm_request_irq(dev, irq, sata_int_v2_hw, 0,
  1967. DRV_NAME " sata", phy);
  1968. if (rc) {
  1969. dev_err(dev, "irq init: could not request "
  1970. "sata interrupt %d, rc=%d\n",
  1971. irq, rc);
  1972. return -ENOENT;
  1973. }
  1974. }
  1975. for (i = 0; i < hisi_hba->queue_count; i++) {
  1976. int idx = i + 96; /* First cq interrupt is irq96 */
  1977. irq = irq_map[idx];
  1978. if (!irq) {
  1979. dev_err(dev,
  1980. "irq init: could not map cq interrupt %d\n",
  1981. idx);
  1982. return -ENOENT;
  1983. }
  1984. rc = devm_request_irq(dev, irq, cq_interrupt_v2_hw, 0,
  1985. DRV_NAME " cq", &hisi_hba->cq[i]);
  1986. if (rc) {
  1987. dev_err(dev,
  1988. "irq init: could not request cq interrupt %d, rc=%d\n",
  1989. irq, rc);
  1990. return -ENOENT;
  1991. }
  1992. }
  1993. return 0;
  1994. }
  1995. static int hisi_sas_v2_init(struct hisi_hba *hisi_hba)
  1996. {
  1997. int rc;
  1998. rc = hw_init_v2_hw(hisi_hba);
  1999. if (rc)
  2000. return rc;
  2001. rc = interrupt_init_v2_hw(hisi_hba);
  2002. if (rc)
  2003. return rc;
  2004. phys_init_v2_hw(hisi_hba);
  2005. return 0;
  2006. }
  2007. static const struct hisi_sas_hw hisi_sas_v2_hw = {
  2008. .hw_init = hisi_sas_v2_init,
  2009. .setup_itct = setup_itct_v2_hw,
  2010. .slot_index_alloc = slot_index_alloc_quirk_v2_hw,
  2011. .alloc_dev = alloc_dev_quirk_v2_hw,
  2012. .sl_notify = sl_notify_v2_hw,
  2013. .get_wideport_bitmap = get_wideport_bitmap_v2_hw,
  2014. .free_device = free_device_v2_hw,
  2015. .prep_smp = prep_smp_v2_hw,
  2016. .prep_ssp = prep_ssp_v2_hw,
  2017. .prep_stp = prep_ata_v2_hw,
  2018. .prep_abort = prep_abort_v2_hw,
  2019. .get_free_slot = get_free_slot_v2_hw,
  2020. .start_delivery = start_delivery_v2_hw,
  2021. .slot_complete = slot_complete_v2_hw,
  2022. .phy_enable = enable_phy_v2_hw,
  2023. .phy_disable = disable_phy_v2_hw,
  2024. .phy_hard_reset = phy_hard_reset_v2_hw,
  2025. .max_command_entries = HISI_SAS_COMMAND_ENTRIES_V2_HW,
  2026. .complete_hdr_size = sizeof(struct hisi_sas_complete_v2_hdr),
  2027. };
  2028. static int hisi_sas_v2_probe(struct platform_device *pdev)
  2029. {
  2030. return hisi_sas_probe(pdev, &hisi_sas_v2_hw);
  2031. }
  2032. static int hisi_sas_v2_remove(struct platform_device *pdev)
  2033. {
  2034. return hisi_sas_remove(pdev);
  2035. }
  2036. static const struct of_device_id sas_v2_of_match[] = {
  2037. { .compatible = "hisilicon,hip06-sas-v2",},
  2038. {},
  2039. };
  2040. MODULE_DEVICE_TABLE(of, sas_v2_of_match);
  2041. static const struct acpi_device_id sas_v2_acpi_match[] = {
  2042. { "HISI0162", 0 },
  2043. { }
  2044. };
  2045. MODULE_DEVICE_TABLE(acpi, sas_v2_acpi_match);
  2046. static struct platform_driver hisi_sas_v2_driver = {
  2047. .probe = hisi_sas_v2_probe,
  2048. .remove = hisi_sas_v2_remove,
  2049. .driver = {
  2050. .name = DRV_NAME,
  2051. .of_match_table = sas_v2_of_match,
  2052. .acpi_match_table = ACPI_PTR(sas_v2_acpi_match),
  2053. },
  2054. };
  2055. module_platform_driver(hisi_sas_v2_driver);
  2056. MODULE_LICENSE("GPL");
  2057. MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
  2058. MODULE_DESCRIPTION("HISILICON SAS controller v2 hw driver");
  2059. MODULE_ALIAS("platform:" DRV_NAME);