host.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include <linux/circ_buf.h>
  56. #include <linux/device.h>
  57. #include <scsi/sas.h>
  58. #include "host.h"
  59. #include "isci.h"
  60. #include "port.h"
  61. #include "host.h"
  62. #include "probe_roms.h"
  63. #include "remote_device.h"
  64. #include "request.h"
  65. #include "scu_completion_codes.h"
  66. #include "scu_event_codes.h"
  67. #include "registers.h"
  68. #include "scu_remote_node_context.h"
  69. #include "scu_task_context.h"
  70. #include "scu_unsolicited_frame.h"
  71. #define SCU_CONTEXT_RAM_INIT_STALL_TIME 200
  72. #define smu_max_ports(dcc_value) \
  73. (\
  74. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
  75. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
  76. )
  77. #define smu_max_task_contexts(dcc_value) \
  78. (\
  79. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
  80. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
  81. )
  82. #define smu_max_rncs(dcc_value) \
  83. (\
  84. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
  85. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
  86. )
  87. #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 100
  88. /**
  89. *
  90. *
  91. * The number of milliseconds to wait while a given phy is consuming power
  92. * before allowing another set of phys to consume power. Ultimately, this will
  93. * be specified by OEM parameter.
  94. */
  95. #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
  96. /**
  97. * NORMALIZE_PUT_POINTER() -
  98. *
  99. * This macro will normalize the completion queue put pointer so its value can
  100. * be used as an array inde
  101. */
  102. #define NORMALIZE_PUT_POINTER(x) \
  103. ((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
  104. /**
  105. * NORMALIZE_EVENT_POINTER() -
  106. *
  107. * This macro will normalize the completion queue event entry so its value can
  108. * be used as an index.
  109. */
  110. #define NORMALIZE_EVENT_POINTER(x) \
  111. (\
  112. ((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
  113. >> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT \
  114. )
  115. /**
  116. * NORMALIZE_GET_POINTER() -
  117. *
  118. * This macro will normalize the completion queue get pointer so its value can
  119. * be used as an index into an array
  120. */
  121. #define NORMALIZE_GET_POINTER(x) \
  122. ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
  123. /**
  124. * NORMALIZE_GET_POINTER_CYCLE_BIT() -
  125. *
  126. * This macro will normalize the completion queue cycle pointer so it matches
  127. * the completion queue cycle bit
  128. */
  129. #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
  130. ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
  131. /**
  132. * COMPLETION_QUEUE_CYCLE_BIT() -
  133. *
  134. * This macro will return the cycle bit of the completion queue entry
  135. */
  136. #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
  137. /* Init the state machine and call the state entry function (if any) */
  138. void sci_init_sm(struct sci_base_state_machine *sm,
  139. const struct sci_base_state *state_table, u32 initial_state)
  140. {
  141. sci_state_transition_t handler;
  142. sm->initial_state_id = initial_state;
  143. sm->previous_state_id = initial_state;
  144. sm->current_state_id = initial_state;
  145. sm->state_table = state_table;
  146. handler = sm->state_table[initial_state].enter_state;
  147. if (handler)
  148. handler(sm);
  149. }
  150. /* Call the state exit fn, update the current state, call the state entry fn */
  151. void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
  152. {
  153. sci_state_transition_t handler;
  154. handler = sm->state_table[sm->current_state_id].exit_state;
  155. if (handler)
  156. handler(sm);
  157. sm->previous_state_id = sm->current_state_id;
  158. sm->current_state_id = next_state;
  159. handler = sm->state_table[sm->current_state_id].enter_state;
  160. if (handler)
  161. handler(sm);
  162. }
  163. static bool scic_sds_controller_completion_queue_has_entries(
  164. struct scic_sds_controller *scic)
  165. {
  166. u32 get_value = scic->completion_queue_get;
  167. u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
  168. if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
  169. COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index]))
  170. return true;
  171. return false;
  172. }
  173. static bool scic_sds_controller_isr(struct scic_sds_controller *scic)
  174. {
  175. if (scic_sds_controller_completion_queue_has_entries(scic)) {
  176. return true;
  177. } else {
  178. /*
  179. * we have a spurious interrupt it could be that we have already
  180. * emptied the completion queue from a previous interrupt */
  181. writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
  182. /*
  183. * There is a race in the hardware that could cause us not to be notified
  184. * of an interrupt completion if we do not take this step. We will mask
  185. * then unmask the interrupts so if there is another interrupt pending
  186. * the clearing of the interrupt source we get the next interrupt message. */
  187. writel(0xFF000000, &scic->smu_registers->interrupt_mask);
  188. writel(0, &scic->smu_registers->interrupt_mask);
  189. }
  190. return false;
  191. }
  192. irqreturn_t isci_msix_isr(int vec, void *data)
  193. {
  194. struct isci_host *ihost = data;
  195. if (scic_sds_controller_isr(&ihost->sci))
  196. tasklet_schedule(&ihost->completion_tasklet);
  197. return IRQ_HANDLED;
  198. }
  199. static bool scic_sds_controller_error_isr(struct scic_sds_controller *scic)
  200. {
  201. u32 interrupt_status;
  202. interrupt_status =
  203. readl(&scic->smu_registers->interrupt_status);
  204. interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
  205. if (interrupt_status != 0) {
  206. /*
  207. * There is an error interrupt pending so let it through and handle
  208. * in the callback */
  209. return true;
  210. }
  211. /*
  212. * There is a race in the hardware that could cause us not to be notified
  213. * of an interrupt completion if we do not take this step. We will mask
  214. * then unmask the error interrupts so if there was another interrupt
  215. * pending we will be notified.
  216. * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
  217. writel(0xff, &scic->smu_registers->interrupt_mask);
  218. writel(0, &scic->smu_registers->interrupt_mask);
  219. return false;
  220. }
  221. static void scic_sds_controller_task_completion(struct scic_sds_controller *scic,
  222. u32 completion_entry)
  223. {
  224. u32 index = SCU_GET_COMPLETION_INDEX(completion_entry);
  225. struct isci_host *ihost = scic_to_ihost(scic);
  226. struct isci_request *ireq = ihost->reqs[index];
  227. struct scic_sds_request *sci_req = &ireq->sci;
  228. /* Make sure that we really want to process this IO request */
  229. if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
  230. sci_req->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
  231. ISCI_TAG_SEQ(sci_req->io_tag) == scic->io_request_sequence[index])
  232. /* Yep this is a valid io request pass it along to the io request handler */
  233. scic_sds_io_request_tc_completion(sci_req, completion_entry);
  234. }
  235. static void scic_sds_controller_sdma_completion(struct scic_sds_controller *scic,
  236. u32 completion_entry)
  237. {
  238. u32 index;
  239. struct scic_sds_request *io_request;
  240. struct scic_sds_remote_device *device;
  241. index = SCU_GET_COMPLETION_INDEX(completion_entry);
  242. switch (scu_get_command_request_type(completion_entry)) {
  243. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
  244. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
  245. io_request = &scic_to_ihost(scic)->reqs[index]->sci;
  246. dev_warn(scic_to_dev(scic),
  247. "%s: SCIC SDS Completion type SDMA %x for io request "
  248. "%p\n",
  249. __func__,
  250. completion_entry,
  251. io_request);
  252. /* @todo For a post TC operation we need to fail the IO
  253. * request
  254. */
  255. break;
  256. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
  257. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
  258. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
  259. device = scic->device_table[index];
  260. dev_warn(scic_to_dev(scic),
  261. "%s: SCIC SDS Completion type SDMA %x for remote "
  262. "device %p\n",
  263. __func__,
  264. completion_entry,
  265. device);
  266. /* @todo For a port RNC operation we need to fail the
  267. * device
  268. */
  269. break;
  270. default:
  271. dev_warn(scic_to_dev(scic),
  272. "%s: SCIC SDS Completion unknown SDMA completion "
  273. "type %x\n",
  274. __func__,
  275. completion_entry);
  276. break;
  277. }
  278. }
  279. static void scic_sds_controller_unsolicited_frame(struct scic_sds_controller *scic,
  280. u32 completion_entry)
  281. {
  282. u32 index;
  283. u32 frame_index;
  284. struct isci_host *ihost = scic_to_ihost(scic);
  285. struct scu_unsolicited_frame_header *frame_header;
  286. struct scic_sds_phy *phy;
  287. struct scic_sds_remote_device *device;
  288. enum sci_status result = SCI_FAILURE;
  289. frame_index = SCU_GET_FRAME_INDEX(completion_entry);
  290. frame_header = scic->uf_control.buffers.array[frame_index].header;
  291. scic->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
  292. if (SCU_GET_FRAME_ERROR(completion_entry)) {
  293. /*
  294. * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
  295. * / this cause a problem? We expect the phy initialization will
  296. * / fail if there is an error in the frame. */
  297. scic_sds_controller_release_frame(scic, frame_index);
  298. return;
  299. }
  300. if (frame_header->is_address_frame) {
  301. index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
  302. phy = &ihost->phys[index].sci;
  303. result = scic_sds_phy_frame_handler(phy, frame_index);
  304. } else {
  305. index = SCU_GET_COMPLETION_INDEX(completion_entry);
  306. if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  307. /*
  308. * This is a signature fis or a frame from a direct attached SATA
  309. * device that has not yet been created. In either case forwared
  310. * the frame to the PE and let it take care of the frame data. */
  311. index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
  312. phy = &ihost->phys[index].sci;
  313. result = scic_sds_phy_frame_handler(phy, frame_index);
  314. } else {
  315. if (index < scic->remote_node_entries)
  316. device = scic->device_table[index];
  317. else
  318. device = NULL;
  319. if (device != NULL)
  320. result = scic_sds_remote_device_frame_handler(device, frame_index);
  321. else
  322. scic_sds_controller_release_frame(scic, frame_index);
  323. }
  324. }
  325. if (result != SCI_SUCCESS) {
  326. /*
  327. * / @todo Is there any reason to report some additional error message
  328. * / when we get this failure notifiction? */
  329. }
  330. }
  331. static void scic_sds_controller_event_completion(struct scic_sds_controller *scic,
  332. u32 completion_entry)
  333. {
  334. struct isci_host *ihost = scic_to_ihost(scic);
  335. struct scic_sds_request *io_request;
  336. struct scic_sds_remote_device *device;
  337. struct scic_sds_phy *phy;
  338. u32 index;
  339. index = SCU_GET_COMPLETION_INDEX(completion_entry);
  340. switch (scu_get_event_type(completion_entry)) {
  341. case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
  342. /* / @todo The driver did something wrong and we need to fix the condtion. */
  343. dev_err(scic_to_dev(scic),
  344. "%s: SCIC Controller 0x%p received SMU command error "
  345. "0x%x\n",
  346. __func__,
  347. scic,
  348. completion_entry);
  349. break;
  350. case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
  351. case SCU_EVENT_TYPE_SMU_ERROR:
  352. case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
  353. /*
  354. * / @todo This is a hardware failure and its likely that we want to
  355. * / reset the controller. */
  356. dev_err(scic_to_dev(scic),
  357. "%s: SCIC Controller 0x%p received fatal controller "
  358. "event 0x%x\n",
  359. __func__,
  360. scic,
  361. completion_entry);
  362. break;
  363. case SCU_EVENT_TYPE_TRANSPORT_ERROR:
  364. io_request = &ihost->reqs[index]->sci;
  365. scic_sds_io_request_event_handler(io_request, completion_entry);
  366. break;
  367. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  368. switch (scu_get_event_specifier(completion_entry)) {
  369. case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
  370. case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
  371. io_request = &ihost->reqs[index]->sci;
  372. if (io_request != NULL)
  373. scic_sds_io_request_event_handler(io_request, completion_entry);
  374. else
  375. dev_warn(scic_to_dev(scic),
  376. "%s: SCIC Controller 0x%p received "
  377. "event 0x%x for io request object "
  378. "that doesnt exist.\n",
  379. __func__,
  380. scic,
  381. completion_entry);
  382. break;
  383. case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
  384. device = scic->device_table[index];
  385. if (device != NULL)
  386. scic_sds_remote_device_event_handler(device, completion_entry);
  387. else
  388. dev_warn(scic_to_dev(scic),
  389. "%s: SCIC Controller 0x%p received "
  390. "event 0x%x for remote device object "
  391. "that doesnt exist.\n",
  392. __func__,
  393. scic,
  394. completion_entry);
  395. break;
  396. }
  397. break;
  398. case SCU_EVENT_TYPE_BROADCAST_CHANGE:
  399. /*
  400. * direct the broadcast change event to the phy first and then let
  401. * the phy redirect the broadcast change to the port object */
  402. case SCU_EVENT_TYPE_ERR_CNT_EVENT:
  403. /*
  404. * direct error counter event to the phy object since that is where
  405. * we get the event notification. This is a type 4 event. */
  406. case SCU_EVENT_TYPE_OSSP_EVENT:
  407. index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
  408. phy = &ihost->phys[index].sci;
  409. scic_sds_phy_event_handler(phy, completion_entry);
  410. break;
  411. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  412. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  413. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  414. if (index < scic->remote_node_entries) {
  415. device = scic->device_table[index];
  416. if (device != NULL)
  417. scic_sds_remote_device_event_handler(device, completion_entry);
  418. } else
  419. dev_err(scic_to_dev(scic),
  420. "%s: SCIC Controller 0x%p received event 0x%x "
  421. "for remote device object 0x%0x that doesnt "
  422. "exist.\n",
  423. __func__,
  424. scic,
  425. completion_entry,
  426. index);
  427. break;
  428. default:
  429. dev_warn(scic_to_dev(scic),
  430. "%s: SCIC Controller received unknown event code %x\n",
  431. __func__,
  432. completion_entry);
  433. break;
  434. }
  435. }
  436. static void scic_sds_controller_process_completions(struct scic_sds_controller *scic)
  437. {
  438. u32 completion_count = 0;
  439. u32 completion_entry;
  440. u32 get_index;
  441. u32 get_cycle;
  442. u32 event_get;
  443. u32 event_cycle;
  444. dev_dbg(scic_to_dev(scic),
  445. "%s: completion queue begining get:0x%08x\n",
  446. __func__,
  447. scic->completion_queue_get);
  448. /* Get the component parts of the completion queue */
  449. get_index = NORMALIZE_GET_POINTER(scic->completion_queue_get);
  450. get_cycle = SMU_CQGR_CYCLE_BIT & scic->completion_queue_get;
  451. event_get = NORMALIZE_EVENT_POINTER(scic->completion_queue_get);
  452. event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & scic->completion_queue_get;
  453. while (
  454. NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
  455. == COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index])
  456. ) {
  457. completion_count++;
  458. completion_entry = scic->completion_queue[get_index];
  459. /* increment the get pointer and check for rollover to toggle the cycle bit */
  460. get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
  461. (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
  462. get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
  463. dev_dbg(scic_to_dev(scic),
  464. "%s: completion queue entry:0x%08x\n",
  465. __func__,
  466. completion_entry);
  467. switch (SCU_GET_COMPLETION_TYPE(completion_entry)) {
  468. case SCU_COMPLETION_TYPE_TASK:
  469. scic_sds_controller_task_completion(scic, completion_entry);
  470. break;
  471. case SCU_COMPLETION_TYPE_SDMA:
  472. scic_sds_controller_sdma_completion(scic, completion_entry);
  473. break;
  474. case SCU_COMPLETION_TYPE_UFI:
  475. scic_sds_controller_unsolicited_frame(scic, completion_entry);
  476. break;
  477. case SCU_COMPLETION_TYPE_EVENT:
  478. case SCU_COMPLETION_TYPE_NOTIFY: {
  479. event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
  480. (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
  481. event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
  482. scic_sds_controller_event_completion(scic, completion_entry);
  483. break;
  484. }
  485. default:
  486. dev_warn(scic_to_dev(scic),
  487. "%s: SCIC Controller received unknown "
  488. "completion type %x\n",
  489. __func__,
  490. completion_entry);
  491. break;
  492. }
  493. }
  494. /* Update the get register if we completed one or more entries */
  495. if (completion_count > 0) {
  496. scic->completion_queue_get =
  497. SMU_CQGR_GEN_BIT(ENABLE) |
  498. SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
  499. event_cycle |
  500. SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
  501. get_cycle |
  502. SMU_CQGR_GEN_VAL(POINTER, get_index);
  503. writel(scic->completion_queue_get,
  504. &scic->smu_registers->completion_queue_get);
  505. }
  506. dev_dbg(scic_to_dev(scic),
  507. "%s: completion queue ending get:0x%08x\n",
  508. __func__,
  509. scic->completion_queue_get);
  510. }
  511. static void scic_sds_controller_error_handler(struct scic_sds_controller *scic)
  512. {
  513. u32 interrupt_status;
  514. interrupt_status =
  515. readl(&scic->smu_registers->interrupt_status);
  516. if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
  517. scic_sds_controller_completion_queue_has_entries(scic)) {
  518. scic_sds_controller_process_completions(scic);
  519. writel(SMU_ISR_QUEUE_SUSPEND, &scic->smu_registers->interrupt_status);
  520. } else {
  521. dev_err(scic_to_dev(scic), "%s: status: %#x\n", __func__,
  522. interrupt_status);
  523. sci_change_state(&scic->sm, SCIC_FAILED);
  524. return;
  525. }
  526. /* If we dont process any completions I am not sure that we want to do this.
  527. * We are in the middle of a hardware fault and should probably be reset.
  528. */
  529. writel(0, &scic->smu_registers->interrupt_mask);
  530. }
  531. irqreturn_t isci_intx_isr(int vec, void *data)
  532. {
  533. irqreturn_t ret = IRQ_NONE;
  534. struct isci_host *ihost = data;
  535. struct scic_sds_controller *scic = &ihost->sci;
  536. if (scic_sds_controller_isr(scic)) {
  537. writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
  538. tasklet_schedule(&ihost->completion_tasklet);
  539. ret = IRQ_HANDLED;
  540. } else if (scic_sds_controller_error_isr(scic)) {
  541. spin_lock(&ihost->scic_lock);
  542. scic_sds_controller_error_handler(scic);
  543. spin_unlock(&ihost->scic_lock);
  544. ret = IRQ_HANDLED;
  545. }
  546. return ret;
  547. }
  548. irqreturn_t isci_error_isr(int vec, void *data)
  549. {
  550. struct isci_host *ihost = data;
  551. if (scic_sds_controller_error_isr(&ihost->sci))
  552. scic_sds_controller_error_handler(&ihost->sci);
  553. return IRQ_HANDLED;
  554. }
  555. /**
  556. * isci_host_start_complete() - This function is called by the core library,
  557. * through the ISCI Module, to indicate controller start status.
  558. * @isci_host: This parameter specifies the ISCI host object
  559. * @completion_status: This parameter specifies the completion status from the
  560. * core library.
  561. *
  562. */
  563. static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
  564. {
  565. if (completion_status != SCI_SUCCESS)
  566. dev_info(&ihost->pdev->dev,
  567. "controller start timed out, continuing...\n");
  568. isci_host_change_state(ihost, isci_ready);
  569. clear_bit(IHOST_START_PENDING, &ihost->flags);
  570. wake_up(&ihost->eventq);
  571. }
  572. int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
  573. {
  574. struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
  575. if (test_bit(IHOST_START_PENDING, &ihost->flags))
  576. return 0;
  577. /* todo: use sas_flush_discovery once it is upstream */
  578. scsi_flush_work(shost);
  579. scsi_flush_work(shost);
  580. dev_dbg(&ihost->pdev->dev,
  581. "%s: ihost->status = %d, time = %ld\n",
  582. __func__, isci_host_get_state(ihost), time);
  583. return 1;
  584. }
  585. /**
  586. * scic_controller_get_suggested_start_timeout() - This method returns the
  587. * suggested scic_controller_start() timeout amount. The user is free to
  588. * use any timeout value, but this method provides the suggested minimum
  589. * start timeout value. The returned value is based upon empirical
  590. * information determined as a result of interoperability testing.
  591. * @controller: the handle to the controller object for which to return the
  592. * suggested start timeout.
  593. *
  594. * This method returns the number of milliseconds for the suggested start
  595. * operation timeout.
  596. */
  597. static u32 scic_controller_get_suggested_start_timeout(
  598. struct scic_sds_controller *sc)
  599. {
  600. /* Validate the user supplied parameters. */
  601. if (sc == NULL)
  602. return 0;
  603. /*
  604. * The suggested minimum timeout value for a controller start operation:
  605. *
  606. * Signature FIS Timeout
  607. * + Phy Start Timeout
  608. * + Number of Phy Spin Up Intervals
  609. * ---------------------------------
  610. * Number of milliseconds for the controller start operation.
  611. *
  612. * NOTE: The number of phy spin up intervals will be equivalent
  613. * to the number of phys divided by the number phys allowed
  614. * per interval - 1 (once OEM parameters are supported).
  615. * Currently we assume only 1 phy per interval. */
  616. return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  617. + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
  618. + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  619. }
  620. static void scic_controller_enable_interrupts(
  621. struct scic_sds_controller *scic)
  622. {
  623. BUG_ON(scic->smu_registers == NULL);
  624. writel(0, &scic->smu_registers->interrupt_mask);
  625. }
  626. void scic_controller_disable_interrupts(
  627. struct scic_sds_controller *scic)
  628. {
  629. BUG_ON(scic->smu_registers == NULL);
  630. writel(0xffffffff, &scic->smu_registers->interrupt_mask);
  631. }
  632. static void scic_sds_controller_enable_port_task_scheduler(
  633. struct scic_sds_controller *scic)
  634. {
  635. u32 port_task_scheduler_value;
  636. port_task_scheduler_value =
  637. readl(&scic->scu_registers->peg0.ptsg.control);
  638. port_task_scheduler_value |=
  639. (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
  640. SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
  641. writel(port_task_scheduler_value,
  642. &scic->scu_registers->peg0.ptsg.control);
  643. }
  644. static void scic_sds_controller_assign_task_entries(struct scic_sds_controller *scic)
  645. {
  646. u32 task_assignment;
  647. /*
  648. * Assign all the TCs to function 0
  649. * TODO: Do we actually need to read this register to write it back?
  650. */
  651. task_assignment =
  652. readl(&scic->smu_registers->task_context_assignment[0]);
  653. task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
  654. (SMU_TCA_GEN_VAL(ENDING, scic->task_context_entries - 1)) |
  655. (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
  656. writel(task_assignment,
  657. &scic->smu_registers->task_context_assignment[0]);
  658. }
  659. static void scic_sds_controller_initialize_completion_queue(struct scic_sds_controller *scic)
  660. {
  661. u32 index;
  662. u32 completion_queue_control_value;
  663. u32 completion_queue_get_value;
  664. u32 completion_queue_put_value;
  665. scic->completion_queue_get = 0;
  666. completion_queue_control_value =
  667. (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
  668. SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
  669. writel(completion_queue_control_value,
  670. &scic->smu_registers->completion_queue_control);
  671. /* Set the completion queue get pointer and enable the queue */
  672. completion_queue_get_value = (
  673. (SMU_CQGR_GEN_VAL(POINTER, 0))
  674. | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
  675. | (SMU_CQGR_GEN_BIT(ENABLE))
  676. | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
  677. );
  678. writel(completion_queue_get_value,
  679. &scic->smu_registers->completion_queue_get);
  680. /* Set the completion queue put pointer */
  681. completion_queue_put_value = (
  682. (SMU_CQPR_GEN_VAL(POINTER, 0))
  683. | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
  684. );
  685. writel(completion_queue_put_value,
  686. &scic->smu_registers->completion_queue_put);
  687. /* Initialize the cycle bit of the completion queue entries */
  688. for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
  689. /*
  690. * If get.cycle_bit != completion_queue.cycle_bit
  691. * its not a valid completion queue entry
  692. * so at system start all entries are invalid */
  693. scic->completion_queue[index] = 0x80000000;
  694. }
  695. }
  696. static void scic_sds_controller_initialize_unsolicited_frame_queue(struct scic_sds_controller *scic)
  697. {
  698. u32 frame_queue_control_value;
  699. u32 frame_queue_get_value;
  700. u32 frame_queue_put_value;
  701. /* Write the queue size */
  702. frame_queue_control_value =
  703. SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
  704. writel(frame_queue_control_value,
  705. &scic->scu_registers->sdma.unsolicited_frame_queue_control);
  706. /* Setup the get pointer for the unsolicited frame queue */
  707. frame_queue_get_value = (
  708. SCU_UFQGP_GEN_VAL(POINTER, 0)
  709. | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
  710. );
  711. writel(frame_queue_get_value,
  712. &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
  713. /* Setup the put pointer for the unsolicited frame queue */
  714. frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
  715. writel(frame_queue_put_value,
  716. &scic->scu_registers->sdma.unsolicited_frame_put_pointer);
  717. }
  718. /**
  719. * This method will attempt to transition into the ready state for the
  720. * controller and indicate that the controller start operation has completed
  721. * if all criteria are met.
  722. * @scic: This parameter indicates the controller object for which
  723. * to transition to ready.
  724. * @status: This parameter indicates the status value to be pass into the call
  725. * to scic_cb_controller_start_complete().
  726. *
  727. * none.
  728. */
  729. static void scic_sds_controller_transition_to_ready(
  730. struct scic_sds_controller *scic,
  731. enum sci_status status)
  732. {
  733. struct isci_host *ihost = scic_to_ihost(scic);
  734. if (scic->sm.current_state_id == SCIC_STARTING) {
  735. /*
  736. * We move into the ready state, because some of the phys/ports
  737. * may be up and operational.
  738. */
  739. sci_change_state(&scic->sm, SCIC_READY);
  740. isci_host_start_complete(ihost, status);
  741. }
  742. }
  743. static bool is_phy_starting(struct scic_sds_phy *sci_phy)
  744. {
  745. enum scic_sds_phy_states state;
  746. state = sci_phy->sm.current_state_id;
  747. switch (state) {
  748. case SCI_PHY_STARTING:
  749. case SCI_PHY_SUB_INITIAL:
  750. case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
  751. case SCI_PHY_SUB_AWAIT_IAF_UF:
  752. case SCI_PHY_SUB_AWAIT_SAS_POWER:
  753. case SCI_PHY_SUB_AWAIT_SATA_POWER:
  754. case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
  755. case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
  756. case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
  757. case SCI_PHY_SUB_FINAL:
  758. return true;
  759. default:
  760. return false;
  761. }
  762. }
  763. /**
  764. * scic_sds_controller_start_next_phy - start phy
  765. * @scic: controller
  766. *
  767. * If all the phys have been started, then attempt to transition the
  768. * controller to the READY state and inform the user
  769. * (scic_cb_controller_start_complete()).
  770. */
  771. static enum sci_status scic_sds_controller_start_next_phy(struct scic_sds_controller *scic)
  772. {
  773. struct isci_host *ihost = scic_to_ihost(scic);
  774. struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
  775. struct scic_sds_phy *sci_phy;
  776. enum sci_status status;
  777. status = SCI_SUCCESS;
  778. if (scic->phy_startup_timer_pending)
  779. return status;
  780. if (scic->next_phy_to_start >= SCI_MAX_PHYS) {
  781. bool is_controller_start_complete = true;
  782. u32 state;
  783. u8 index;
  784. for (index = 0; index < SCI_MAX_PHYS; index++) {
  785. sci_phy = &ihost->phys[index].sci;
  786. state = sci_phy->sm.current_state_id;
  787. if (!phy_get_non_dummy_port(sci_phy))
  788. continue;
  789. /* The controller start operation is complete iff:
  790. * - all links have been given an opportunity to start
  791. * - have no indication of a connected device
  792. * - have an indication of a connected device and it has
  793. * finished the link training process.
  794. */
  795. if ((sci_phy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
  796. (sci_phy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
  797. (sci_phy->is_in_link_training == true && is_phy_starting(sci_phy))) {
  798. is_controller_start_complete = false;
  799. break;
  800. }
  801. }
  802. /*
  803. * The controller has successfully finished the start process.
  804. * Inform the SCI Core user and transition to the READY state. */
  805. if (is_controller_start_complete == true) {
  806. scic_sds_controller_transition_to_ready(scic, SCI_SUCCESS);
  807. sci_del_timer(&scic->phy_timer);
  808. scic->phy_startup_timer_pending = false;
  809. }
  810. } else {
  811. sci_phy = &ihost->phys[scic->next_phy_to_start].sci;
  812. if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  813. if (phy_get_non_dummy_port(sci_phy) == NULL) {
  814. scic->next_phy_to_start++;
  815. /* Caution recursion ahead be forwarned
  816. *
  817. * The PHY was never added to a PORT in MPC mode
  818. * so start the next phy in sequence This phy
  819. * will never go link up and will not draw power
  820. * the OEM parameters either configured the phy
  821. * incorrectly for the PORT or it was never
  822. * assigned to a PORT
  823. */
  824. return scic_sds_controller_start_next_phy(scic);
  825. }
  826. }
  827. status = scic_sds_phy_start(sci_phy);
  828. if (status == SCI_SUCCESS) {
  829. sci_mod_timer(&scic->phy_timer,
  830. SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
  831. scic->phy_startup_timer_pending = true;
  832. } else {
  833. dev_warn(scic_to_dev(scic),
  834. "%s: Controller stop operation failed "
  835. "to stop phy %d because of status "
  836. "%d.\n",
  837. __func__,
  838. ihost->phys[scic->next_phy_to_start].sci.phy_index,
  839. status);
  840. }
  841. scic->next_phy_to_start++;
  842. }
  843. return status;
  844. }
  845. static void phy_startup_timeout(unsigned long data)
  846. {
  847. struct sci_timer *tmr = (struct sci_timer *)data;
  848. struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), phy_timer);
  849. struct isci_host *ihost = scic_to_ihost(scic);
  850. unsigned long flags;
  851. enum sci_status status;
  852. spin_lock_irqsave(&ihost->scic_lock, flags);
  853. if (tmr->cancel)
  854. goto done;
  855. scic->phy_startup_timer_pending = false;
  856. do {
  857. status = scic_sds_controller_start_next_phy(scic);
  858. } while (status != SCI_SUCCESS);
  859. done:
  860. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  861. }
  862. static u16 isci_tci_active(struct isci_host *ihost)
  863. {
  864. return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
  865. }
  866. static enum sci_status scic_controller_start(struct scic_sds_controller *scic,
  867. u32 timeout)
  868. {
  869. struct isci_host *ihost = scic_to_ihost(scic);
  870. enum sci_status result;
  871. u16 index;
  872. if (scic->sm.current_state_id != SCIC_INITIALIZED) {
  873. dev_warn(scic_to_dev(scic),
  874. "SCIC Controller start operation requested in "
  875. "invalid state\n");
  876. return SCI_FAILURE_INVALID_STATE;
  877. }
  878. /* Build the TCi free pool */
  879. BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
  880. ihost->tci_head = 0;
  881. ihost->tci_tail = 0;
  882. for (index = 0; index < scic->task_context_entries; index++)
  883. isci_tci_free(ihost, index);
  884. /* Build the RNi free pool */
  885. scic_sds_remote_node_table_initialize(
  886. &scic->available_remote_nodes,
  887. scic->remote_node_entries);
  888. /*
  889. * Before anything else lets make sure we will not be
  890. * interrupted by the hardware.
  891. */
  892. scic_controller_disable_interrupts(scic);
  893. /* Enable the port task scheduler */
  894. scic_sds_controller_enable_port_task_scheduler(scic);
  895. /* Assign all the task entries to scic physical function */
  896. scic_sds_controller_assign_task_entries(scic);
  897. /* Now initialize the completion queue */
  898. scic_sds_controller_initialize_completion_queue(scic);
  899. /* Initialize the unsolicited frame queue for use */
  900. scic_sds_controller_initialize_unsolicited_frame_queue(scic);
  901. /* Start all of the ports on this controller */
  902. for (index = 0; index < scic->logical_port_entries; index++) {
  903. struct scic_sds_port *sci_port = &ihost->ports[index].sci;
  904. result = scic_sds_port_start(sci_port);
  905. if (result)
  906. return result;
  907. }
  908. scic_sds_controller_start_next_phy(scic);
  909. sci_mod_timer(&scic->timer, timeout);
  910. sci_change_state(&scic->sm, SCIC_STARTING);
  911. return SCI_SUCCESS;
  912. }
  913. void isci_host_scan_start(struct Scsi_Host *shost)
  914. {
  915. struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
  916. unsigned long tmo = scic_controller_get_suggested_start_timeout(&ihost->sci);
  917. set_bit(IHOST_START_PENDING, &ihost->flags);
  918. spin_lock_irq(&ihost->scic_lock);
  919. scic_controller_start(&ihost->sci, tmo);
  920. scic_controller_enable_interrupts(&ihost->sci);
  921. spin_unlock_irq(&ihost->scic_lock);
  922. }
  923. static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
  924. {
  925. isci_host_change_state(ihost, isci_stopped);
  926. scic_controller_disable_interrupts(&ihost->sci);
  927. clear_bit(IHOST_STOP_PENDING, &ihost->flags);
  928. wake_up(&ihost->eventq);
  929. }
  930. static void scic_sds_controller_completion_handler(struct scic_sds_controller *scic)
  931. {
  932. /* Empty out the completion queue */
  933. if (scic_sds_controller_completion_queue_has_entries(scic))
  934. scic_sds_controller_process_completions(scic);
  935. /* Clear the interrupt and enable all interrupts again */
  936. writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
  937. /* Could we write the value of SMU_ISR_COMPLETION? */
  938. writel(0xFF000000, &scic->smu_registers->interrupt_mask);
  939. writel(0, &scic->smu_registers->interrupt_mask);
  940. }
  941. /**
  942. * isci_host_completion_routine() - This function is the delayed service
  943. * routine that calls the sci core library's completion handler. It's
  944. * scheduled as a tasklet from the interrupt service routine when interrupts
  945. * in use, or set as the timeout function in polled mode.
  946. * @data: This parameter specifies the ISCI host object
  947. *
  948. */
  949. static void isci_host_completion_routine(unsigned long data)
  950. {
  951. struct isci_host *isci_host = (struct isci_host *)data;
  952. struct list_head completed_request_list;
  953. struct list_head errored_request_list;
  954. struct list_head *current_position;
  955. struct list_head *next_position;
  956. struct isci_request *request;
  957. struct isci_request *next_request;
  958. struct sas_task *task;
  959. INIT_LIST_HEAD(&completed_request_list);
  960. INIT_LIST_HEAD(&errored_request_list);
  961. spin_lock_irq(&isci_host->scic_lock);
  962. scic_sds_controller_completion_handler(&isci_host->sci);
  963. /* Take the lists of completed I/Os from the host. */
  964. list_splice_init(&isci_host->requests_to_complete,
  965. &completed_request_list);
  966. /* Take the list of errored I/Os from the host. */
  967. list_splice_init(&isci_host->requests_to_errorback,
  968. &errored_request_list);
  969. spin_unlock_irq(&isci_host->scic_lock);
  970. /* Process any completions in the lists. */
  971. list_for_each_safe(current_position, next_position,
  972. &completed_request_list) {
  973. request = list_entry(current_position, struct isci_request,
  974. completed_node);
  975. task = isci_request_access_task(request);
  976. /* Normal notification (task_done) */
  977. dev_dbg(&isci_host->pdev->dev,
  978. "%s: Normal - request/task = %p/%p\n",
  979. __func__,
  980. request,
  981. task);
  982. /* Return the task to libsas */
  983. if (task != NULL) {
  984. task->lldd_task = NULL;
  985. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  986. /* If the task is already in the abort path,
  987. * the task_done callback cannot be called.
  988. */
  989. task->task_done(task);
  990. }
  991. }
  992. spin_lock_irq(&isci_host->scic_lock);
  993. isci_free_tag(isci_host, request->sci.io_tag);
  994. spin_unlock_irq(&isci_host->scic_lock);
  995. }
  996. list_for_each_entry_safe(request, next_request, &errored_request_list,
  997. completed_node) {
  998. task = isci_request_access_task(request);
  999. /* Use sas_task_abort */
  1000. dev_warn(&isci_host->pdev->dev,
  1001. "%s: Error - request/task = %p/%p\n",
  1002. __func__,
  1003. request,
  1004. task);
  1005. if (task != NULL) {
  1006. /* Put the task into the abort path if it's not there
  1007. * already.
  1008. */
  1009. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
  1010. sas_task_abort(task);
  1011. } else {
  1012. /* This is a case where the request has completed with a
  1013. * status such that it needed further target servicing,
  1014. * but the sas_task reference has already been removed
  1015. * from the request. Since it was errored, it was not
  1016. * being aborted, so there is nothing to do except free
  1017. * it.
  1018. */
  1019. spin_lock_irq(&isci_host->scic_lock);
  1020. /* Remove the request from the remote device's list
  1021. * of pending requests.
  1022. */
  1023. list_del_init(&request->dev_node);
  1024. isci_free_tag(isci_host, request->sci.io_tag);
  1025. spin_unlock_irq(&isci_host->scic_lock);
  1026. }
  1027. }
  1028. }
  1029. /**
  1030. * scic_controller_stop() - This method will stop an individual controller
  1031. * object.This method will invoke the associated user callback upon
  1032. * completion. The completion callback is called when the following
  1033. * conditions are met: -# the method return status is SCI_SUCCESS. -# the
  1034. * controller has been quiesced. This method will ensure that all IO
  1035. * requests are quiesced, phys are stopped, and all additional operation by
  1036. * the hardware is halted.
  1037. * @controller: the handle to the controller object to stop.
  1038. * @timeout: This parameter specifies the number of milliseconds in which the
  1039. * stop operation should complete.
  1040. *
  1041. * The controller must be in the STARTED or STOPPED state. Indicate if the
  1042. * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
  1043. * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
  1044. * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
  1045. * controller is not either in the STARTED or STOPPED states.
  1046. */
  1047. static enum sci_status scic_controller_stop(struct scic_sds_controller *scic,
  1048. u32 timeout)
  1049. {
  1050. if (scic->sm.current_state_id != SCIC_READY) {
  1051. dev_warn(scic_to_dev(scic),
  1052. "SCIC Controller stop operation requested in "
  1053. "invalid state\n");
  1054. return SCI_FAILURE_INVALID_STATE;
  1055. }
  1056. sci_mod_timer(&scic->timer, timeout);
  1057. sci_change_state(&scic->sm, SCIC_STOPPING);
  1058. return SCI_SUCCESS;
  1059. }
  1060. /**
  1061. * scic_controller_reset() - This method will reset the supplied core
  1062. * controller regardless of the state of said controller. This operation is
  1063. * considered destructive. In other words, all current operations are wiped
  1064. * out. No IO completions for outstanding devices occur. Outstanding IO
  1065. * requests are not aborted or completed at the actual remote device.
  1066. * @controller: the handle to the controller object to reset.
  1067. *
  1068. * Indicate if the controller reset method succeeded or failed in some way.
  1069. * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
  1070. * the controller reset operation is unable to complete.
  1071. */
  1072. static enum sci_status scic_controller_reset(struct scic_sds_controller *scic)
  1073. {
  1074. switch (scic->sm.current_state_id) {
  1075. case SCIC_RESET:
  1076. case SCIC_READY:
  1077. case SCIC_STOPPED:
  1078. case SCIC_FAILED:
  1079. /*
  1080. * The reset operation is not a graceful cleanup, just
  1081. * perform the state transition.
  1082. */
  1083. sci_change_state(&scic->sm, SCIC_RESETTING);
  1084. return SCI_SUCCESS;
  1085. default:
  1086. dev_warn(scic_to_dev(scic),
  1087. "SCIC Controller reset operation requested in "
  1088. "invalid state\n");
  1089. return SCI_FAILURE_INVALID_STATE;
  1090. }
  1091. }
  1092. void isci_host_deinit(struct isci_host *ihost)
  1093. {
  1094. int i;
  1095. isci_host_change_state(ihost, isci_stopping);
  1096. for (i = 0; i < SCI_MAX_PORTS; i++) {
  1097. struct isci_port *iport = &ihost->ports[i];
  1098. struct isci_remote_device *idev, *d;
  1099. list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
  1100. if (test_bit(IDEV_ALLOCATED, &idev->flags))
  1101. isci_remote_device_stop(ihost, idev);
  1102. }
  1103. }
  1104. set_bit(IHOST_STOP_PENDING, &ihost->flags);
  1105. spin_lock_irq(&ihost->scic_lock);
  1106. scic_controller_stop(&ihost->sci, SCIC_CONTROLLER_STOP_TIMEOUT);
  1107. spin_unlock_irq(&ihost->scic_lock);
  1108. wait_for_stop(ihost);
  1109. scic_controller_reset(&ihost->sci);
  1110. /* Cancel any/all outstanding port timers */
  1111. for (i = 0; i < ihost->sci.logical_port_entries; i++) {
  1112. struct scic_sds_port *sci_port = &ihost->ports[i].sci;
  1113. del_timer_sync(&sci_port->timer.timer);
  1114. }
  1115. /* Cancel any/all outstanding phy timers */
  1116. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1117. struct scic_sds_phy *sci_phy = &ihost->phys[i].sci;
  1118. del_timer_sync(&sci_phy->sata_timer.timer);
  1119. }
  1120. del_timer_sync(&ihost->sci.port_agent.timer.timer);
  1121. del_timer_sync(&ihost->sci.power_control.timer.timer);
  1122. del_timer_sync(&ihost->sci.timer.timer);
  1123. del_timer_sync(&ihost->sci.phy_timer.timer);
  1124. }
  1125. static void __iomem *scu_base(struct isci_host *isci_host)
  1126. {
  1127. struct pci_dev *pdev = isci_host->pdev;
  1128. int id = isci_host->id;
  1129. return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
  1130. }
  1131. static void __iomem *smu_base(struct isci_host *isci_host)
  1132. {
  1133. struct pci_dev *pdev = isci_host->pdev;
  1134. int id = isci_host->id;
  1135. return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
  1136. }
  1137. static void isci_user_parameters_get(
  1138. struct isci_host *isci_host,
  1139. union scic_user_parameters *scic_user_params)
  1140. {
  1141. struct scic_sds_user_parameters *u = &scic_user_params->sds1;
  1142. int i;
  1143. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1144. struct sci_phy_user_params *u_phy = &u->phys[i];
  1145. u_phy->max_speed_generation = phy_gen;
  1146. /* we are not exporting these for now */
  1147. u_phy->align_insertion_frequency = 0x7f;
  1148. u_phy->in_connection_align_insertion_frequency = 0xff;
  1149. u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
  1150. }
  1151. u->stp_inactivity_timeout = stp_inactive_to;
  1152. u->ssp_inactivity_timeout = ssp_inactive_to;
  1153. u->stp_max_occupancy_timeout = stp_max_occ_to;
  1154. u->ssp_max_occupancy_timeout = ssp_max_occ_to;
  1155. u->no_outbound_task_timeout = no_outbound_task_to;
  1156. u->max_number_concurrent_device_spin_up = max_concurr_spinup;
  1157. }
  1158. static void scic_sds_controller_initial_state_enter(struct sci_base_state_machine *sm)
  1159. {
  1160. struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
  1161. sci_change_state(&scic->sm, SCIC_RESET);
  1162. }
  1163. static inline void scic_sds_controller_starting_state_exit(struct sci_base_state_machine *sm)
  1164. {
  1165. struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
  1166. sci_del_timer(&scic->timer);
  1167. }
  1168. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
  1169. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
  1170. #define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
  1171. #define INTERRUPT_COALESCE_NUMBER_MAX 256
  1172. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
  1173. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
  1174. /**
  1175. * scic_controller_set_interrupt_coalescence() - This method allows the user to
  1176. * configure the interrupt coalescence.
  1177. * @controller: This parameter represents the handle to the controller object
  1178. * for which its interrupt coalesce register is overridden.
  1179. * @coalesce_number: Used to control the number of entries in the Completion
  1180. * Queue before an interrupt is generated. If the number of entries exceed
  1181. * this number, an interrupt will be generated. The valid range of the input
  1182. * is [0, 256]. A setting of 0 results in coalescing being disabled.
  1183. * @coalesce_timeout: Timeout value in microseconds. The valid range of the
  1184. * input is [0, 2700000] . A setting of 0 is allowed and results in no
  1185. * interrupt coalescing timeout.
  1186. *
  1187. * Indicate if the user successfully set the interrupt coalesce parameters.
  1188. * SCI_SUCCESS The user successfully updated the interrutp coalescence.
  1189. * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
  1190. */
  1191. static enum sci_status scic_controller_set_interrupt_coalescence(
  1192. struct scic_sds_controller *scic_controller,
  1193. u32 coalesce_number,
  1194. u32 coalesce_timeout)
  1195. {
  1196. u8 timeout_encode = 0;
  1197. u32 min = 0;
  1198. u32 max = 0;
  1199. /* Check if the input parameters fall in the range. */
  1200. if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
  1201. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1202. /*
  1203. * Defined encoding for interrupt coalescing timeout:
  1204. * Value Min Max Units
  1205. * ----- --- --- -----
  1206. * 0 - - Disabled
  1207. * 1 13.3 20.0 ns
  1208. * 2 26.7 40.0
  1209. * 3 53.3 80.0
  1210. * 4 106.7 160.0
  1211. * 5 213.3 320.0
  1212. * 6 426.7 640.0
  1213. * 7 853.3 1280.0
  1214. * 8 1.7 2.6 us
  1215. * 9 3.4 5.1
  1216. * 10 6.8 10.2
  1217. * 11 13.7 20.5
  1218. * 12 27.3 41.0
  1219. * 13 54.6 81.9
  1220. * 14 109.2 163.8
  1221. * 15 218.5 327.7
  1222. * 16 436.9 655.4
  1223. * 17 873.8 1310.7
  1224. * 18 1.7 2.6 ms
  1225. * 19 3.5 5.2
  1226. * 20 7.0 10.5
  1227. * 21 14.0 21.0
  1228. * 22 28.0 41.9
  1229. * 23 55.9 83.9
  1230. * 24 111.8 167.8
  1231. * 25 223.7 335.5
  1232. * 26 447.4 671.1
  1233. * 27 894.8 1342.2
  1234. * 28 1.8 2.7 s
  1235. * Others Undefined */
  1236. /*
  1237. * Use the table above to decide the encode of interrupt coalescing timeout
  1238. * value for register writing. */
  1239. if (coalesce_timeout == 0)
  1240. timeout_encode = 0;
  1241. else{
  1242. /* make the timeout value in unit of (10 ns). */
  1243. coalesce_timeout = coalesce_timeout * 100;
  1244. min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
  1245. max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
  1246. /* get the encode of timeout for register writing. */
  1247. for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
  1248. timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
  1249. timeout_encode++) {
  1250. if (min <= coalesce_timeout && max > coalesce_timeout)
  1251. break;
  1252. else if (coalesce_timeout >= max && coalesce_timeout < min * 2
  1253. && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
  1254. if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
  1255. break;
  1256. else{
  1257. timeout_encode++;
  1258. break;
  1259. }
  1260. } else {
  1261. max = max * 2;
  1262. min = min * 2;
  1263. }
  1264. }
  1265. if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
  1266. /* the value is out of range. */
  1267. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1268. }
  1269. writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
  1270. SMU_ICC_GEN_VAL(TIMER, timeout_encode),
  1271. &scic_controller->smu_registers->interrupt_coalesce_control);
  1272. scic_controller->interrupt_coalesce_number = (u16)coalesce_number;
  1273. scic_controller->interrupt_coalesce_timeout = coalesce_timeout / 100;
  1274. return SCI_SUCCESS;
  1275. }
  1276. static void scic_sds_controller_ready_state_enter(struct sci_base_state_machine *sm)
  1277. {
  1278. struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
  1279. /* set the default interrupt coalescence number and timeout value. */
  1280. scic_controller_set_interrupt_coalescence(scic, 0x10, 250);
  1281. }
  1282. static void scic_sds_controller_ready_state_exit(struct sci_base_state_machine *sm)
  1283. {
  1284. struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
  1285. /* disable interrupt coalescence. */
  1286. scic_controller_set_interrupt_coalescence(scic, 0, 0);
  1287. }
  1288. static enum sci_status scic_sds_controller_stop_phys(struct scic_sds_controller *scic)
  1289. {
  1290. u32 index;
  1291. enum sci_status status;
  1292. enum sci_status phy_status;
  1293. struct isci_host *ihost = scic_to_ihost(scic);
  1294. status = SCI_SUCCESS;
  1295. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1296. phy_status = scic_sds_phy_stop(&ihost->phys[index].sci);
  1297. if (phy_status != SCI_SUCCESS &&
  1298. phy_status != SCI_FAILURE_INVALID_STATE) {
  1299. status = SCI_FAILURE;
  1300. dev_warn(scic_to_dev(scic),
  1301. "%s: Controller stop operation failed to stop "
  1302. "phy %d because of status %d.\n",
  1303. __func__,
  1304. ihost->phys[index].sci.phy_index, phy_status);
  1305. }
  1306. }
  1307. return status;
  1308. }
  1309. static enum sci_status scic_sds_controller_stop_ports(struct scic_sds_controller *scic)
  1310. {
  1311. u32 index;
  1312. enum sci_status port_status;
  1313. enum sci_status status = SCI_SUCCESS;
  1314. struct isci_host *ihost = scic_to_ihost(scic);
  1315. for (index = 0; index < scic->logical_port_entries; index++) {
  1316. struct scic_sds_port *sci_port = &ihost->ports[index].sci;
  1317. port_status = scic_sds_port_stop(sci_port);
  1318. if ((port_status != SCI_SUCCESS) &&
  1319. (port_status != SCI_FAILURE_INVALID_STATE)) {
  1320. status = SCI_FAILURE;
  1321. dev_warn(scic_to_dev(scic),
  1322. "%s: Controller stop operation failed to "
  1323. "stop port %d because of status %d.\n",
  1324. __func__,
  1325. sci_port->logical_port_index,
  1326. port_status);
  1327. }
  1328. }
  1329. return status;
  1330. }
  1331. static enum sci_status scic_sds_controller_stop_devices(struct scic_sds_controller *scic)
  1332. {
  1333. u32 index;
  1334. enum sci_status status;
  1335. enum sci_status device_status;
  1336. status = SCI_SUCCESS;
  1337. for (index = 0; index < scic->remote_node_entries; index++) {
  1338. if (scic->device_table[index] != NULL) {
  1339. /* / @todo What timeout value do we want to provide to this request? */
  1340. device_status = scic_remote_device_stop(scic->device_table[index], 0);
  1341. if ((device_status != SCI_SUCCESS) &&
  1342. (device_status != SCI_FAILURE_INVALID_STATE)) {
  1343. dev_warn(scic_to_dev(scic),
  1344. "%s: Controller stop operation failed "
  1345. "to stop device 0x%p because of "
  1346. "status %d.\n",
  1347. __func__,
  1348. scic->device_table[index], device_status);
  1349. }
  1350. }
  1351. }
  1352. return status;
  1353. }
  1354. static void scic_sds_controller_stopping_state_enter(struct sci_base_state_machine *sm)
  1355. {
  1356. struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
  1357. /* Stop all of the components for this controller */
  1358. scic_sds_controller_stop_phys(scic);
  1359. scic_sds_controller_stop_ports(scic);
  1360. scic_sds_controller_stop_devices(scic);
  1361. }
  1362. static void scic_sds_controller_stopping_state_exit(struct sci_base_state_machine *sm)
  1363. {
  1364. struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
  1365. sci_del_timer(&scic->timer);
  1366. }
  1367. /**
  1368. * scic_sds_controller_reset_hardware() -
  1369. *
  1370. * This method will reset the controller hardware.
  1371. */
  1372. static void scic_sds_controller_reset_hardware(struct scic_sds_controller *scic)
  1373. {
  1374. /* Disable interrupts so we dont take any spurious interrupts */
  1375. scic_controller_disable_interrupts(scic);
  1376. /* Reset the SCU */
  1377. writel(0xFFFFFFFF, &scic->smu_registers->soft_reset_control);
  1378. /* Delay for 1ms to before clearing the CQP and UFQPR. */
  1379. udelay(1000);
  1380. /* The write to the CQGR clears the CQP */
  1381. writel(0x00000000, &scic->smu_registers->completion_queue_get);
  1382. /* The write to the UFQGP clears the UFQPR */
  1383. writel(0, &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
  1384. }
  1385. static void scic_sds_controller_resetting_state_enter(struct sci_base_state_machine *sm)
  1386. {
  1387. struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
  1388. scic_sds_controller_reset_hardware(scic);
  1389. sci_change_state(&scic->sm, SCIC_RESET);
  1390. }
  1391. static const struct sci_base_state scic_sds_controller_state_table[] = {
  1392. [SCIC_INITIAL] = {
  1393. .enter_state = scic_sds_controller_initial_state_enter,
  1394. },
  1395. [SCIC_RESET] = {},
  1396. [SCIC_INITIALIZING] = {},
  1397. [SCIC_INITIALIZED] = {},
  1398. [SCIC_STARTING] = {
  1399. .exit_state = scic_sds_controller_starting_state_exit,
  1400. },
  1401. [SCIC_READY] = {
  1402. .enter_state = scic_sds_controller_ready_state_enter,
  1403. .exit_state = scic_sds_controller_ready_state_exit,
  1404. },
  1405. [SCIC_RESETTING] = {
  1406. .enter_state = scic_sds_controller_resetting_state_enter,
  1407. },
  1408. [SCIC_STOPPING] = {
  1409. .enter_state = scic_sds_controller_stopping_state_enter,
  1410. .exit_state = scic_sds_controller_stopping_state_exit,
  1411. },
  1412. [SCIC_STOPPED] = {},
  1413. [SCIC_FAILED] = {}
  1414. };
  1415. static void scic_sds_controller_set_default_config_parameters(struct scic_sds_controller *scic)
  1416. {
  1417. /* these defaults are overridden by the platform / firmware */
  1418. struct isci_host *ihost = scic_to_ihost(scic);
  1419. u16 index;
  1420. /* Default to APC mode. */
  1421. scic->oem_parameters.sds1.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
  1422. /* Default to APC mode. */
  1423. scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up = 1;
  1424. /* Default to no SSC operation. */
  1425. scic->oem_parameters.sds1.controller.do_enable_ssc = false;
  1426. /* Initialize all of the port parameter information to narrow ports. */
  1427. for (index = 0; index < SCI_MAX_PORTS; index++) {
  1428. scic->oem_parameters.sds1.ports[index].phy_mask = 0;
  1429. }
  1430. /* Initialize all of the phy parameter information. */
  1431. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1432. /* Default to 6G (i.e. Gen 3) for now. */
  1433. scic->user_parameters.sds1.phys[index].max_speed_generation = 3;
  1434. /* the frequencies cannot be 0 */
  1435. scic->user_parameters.sds1.phys[index].align_insertion_frequency = 0x7f;
  1436. scic->user_parameters.sds1.phys[index].in_connection_align_insertion_frequency = 0xff;
  1437. scic->user_parameters.sds1.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
  1438. /*
  1439. * Previous Vitesse based expanders had a arbitration issue that
  1440. * is worked around by having the upper 32-bits of SAS address
  1441. * with a value greater then the Vitesse company identifier.
  1442. * Hence, usage of 0x5FCFFFFF. */
  1443. scic->oem_parameters.sds1.phys[index].sas_address.low = 0x1 + ihost->id;
  1444. scic->oem_parameters.sds1.phys[index].sas_address.high = 0x5FCFFFFF;
  1445. }
  1446. scic->user_parameters.sds1.stp_inactivity_timeout = 5;
  1447. scic->user_parameters.sds1.ssp_inactivity_timeout = 5;
  1448. scic->user_parameters.sds1.stp_max_occupancy_timeout = 5;
  1449. scic->user_parameters.sds1.ssp_max_occupancy_timeout = 20;
  1450. scic->user_parameters.sds1.no_outbound_task_timeout = 20;
  1451. }
  1452. static void controller_timeout(unsigned long data)
  1453. {
  1454. struct sci_timer *tmr = (struct sci_timer *)data;
  1455. struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), timer);
  1456. struct isci_host *ihost = scic_to_ihost(scic);
  1457. struct sci_base_state_machine *sm = &scic->sm;
  1458. unsigned long flags;
  1459. spin_lock_irqsave(&ihost->scic_lock, flags);
  1460. if (tmr->cancel)
  1461. goto done;
  1462. if (sm->current_state_id == SCIC_STARTING)
  1463. scic_sds_controller_transition_to_ready(scic, SCI_FAILURE_TIMEOUT);
  1464. else if (sm->current_state_id == SCIC_STOPPING) {
  1465. sci_change_state(sm, SCIC_FAILED);
  1466. isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
  1467. } else /* / @todo Now what do we want to do in this case? */
  1468. dev_err(scic_to_dev(scic),
  1469. "%s: Controller timer fired when controller was not "
  1470. "in a state being timed.\n",
  1471. __func__);
  1472. done:
  1473. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1474. }
  1475. /**
  1476. * scic_controller_construct() - This method will attempt to construct a
  1477. * controller object utilizing the supplied parameter information.
  1478. * @c: This parameter specifies the controller to be constructed.
  1479. * @scu_base: mapped base address of the scu registers
  1480. * @smu_base: mapped base address of the smu registers
  1481. *
  1482. * Indicate if the controller was successfully constructed or if it failed in
  1483. * some way. SCI_SUCCESS This value is returned if the controller was
  1484. * successfully constructed. SCI_WARNING_TIMER_CONFLICT This value is returned
  1485. * if the interrupt coalescence timer may cause SAS compliance issues for SMP
  1486. * Target mode response processing. SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE
  1487. * This value is returned if the controller does not support the supplied type.
  1488. * SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION This value is returned if the
  1489. * controller does not support the supplied initialization data version.
  1490. */
  1491. static enum sci_status scic_controller_construct(struct scic_sds_controller *scic,
  1492. void __iomem *scu_base,
  1493. void __iomem *smu_base)
  1494. {
  1495. struct isci_host *ihost = scic_to_ihost(scic);
  1496. u8 i;
  1497. sci_init_sm(&scic->sm, scic_sds_controller_state_table, SCIC_INITIAL);
  1498. scic->scu_registers = scu_base;
  1499. scic->smu_registers = smu_base;
  1500. scic_sds_port_configuration_agent_construct(&scic->port_agent);
  1501. /* Construct the ports for this controller */
  1502. for (i = 0; i < SCI_MAX_PORTS; i++)
  1503. scic_sds_port_construct(&ihost->ports[i].sci, i, scic);
  1504. scic_sds_port_construct(&ihost->ports[i].sci, SCIC_SDS_DUMMY_PORT, scic);
  1505. /* Construct the phys for this controller */
  1506. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1507. /* Add all the PHYs to the dummy port */
  1508. scic_sds_phy_construct(&ihost->phys[i].sci,
  1509. &ihost->ports[SCI_MAX_PORTS].sci, i);
  1510. }
  1511. scic->invalid_phy_mask = 0;
  1512. sci_init_timer(&scic->timer, controller_timeout);
  1513. /* Initialize the User and OEM parameters to default values. */
  1514. scic_sds_controller_set_default_config_parameters(scic);
  1515. return scic_controller_reset(scic);
  1516. }
  1517. int scic_oem_parameters_validate(struct scic_sds_oem_params *oem)
  1518. {
  1519. int i;
  1520. for (i = 0; i < SCI_MAX_PORTS; i++)
  1521. if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
  1522. return -EINVAL;
  1523. for (i = 0; i < SCI_MAX_PHYS; i++)
  1524. if (oem->phys[i].sas_address.high == 0 &&
  1525. oem->phys[i].sas_address.low == 0)
  1526. return -EINVAL;
  1527. if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
  1528. for (i = 0; i < SCI_MAX_PHYS; i++)
  1529. if (oem->ports[i].phy_mask != 0)
  1530. return -EINVAL;
  1531. } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  1532. u8 phy_mask = 0;
  1533. for (i = 0; i < SCI_MAX_PHYS; i++)
  1534. phy_mask |= oem->ports[i].phy_mask;
  1535. if (phy_mask == 0)
  1536. return -EINVAL;
  1537. } else
  1538. return -EINVAL;
  1539. if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
  1540. return -EINVAL;
  1541. return 0;
  1542. }
  1543. static enum sci_status scic_oem_parameters_set(struct scic_sds_controller *scic,
  1544. union scic_oem_parameters *scic_parms)
  1545. {
  1546. u32 state = scic->sm.current_state_id;
  1547. if (state == SCIC_RESET ||
  1548. state == SCIC_INITIALIZING ||
  1549. state == SCIC_INITIALIZED) {
  1550. if (scic_oem_parameters_validate(&scic_parms->sds1))
  1551. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1552. scic->oem_parameters.sds1 = scic_parms->sds1;
  1553. return SCI_SUCCESS;
  1554. }
  1555. return SCI_FAILURE_INVALID_STATE;
  1556. }
  1557. void scic_oem_parameters_get(
  1558. struct scic_sds_controller *scic,
  1559. union scic_oem_parameters *scic_parms)
  1560. {
  1561. memcpy(scic_parms, (&scic->oem_parameters), sizeof(*scic_parms));
  1562. }
  1563. static void power_control_timeout(unsigned long data)
  1564. {
  1565. struct sci_timer *tmr = (struct sci_timer *)data;
  1566. struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), power_control.timer);
  1567. struct isci_host *ihost = scic_to_ihost(scic);
  1568. struct scic_sds_phy *sci_phy;
  1569. unsigned long flags;
  1570. u8 i;
  1571. spin_lock_irqsave(&ihost->scic_lock, flags);
  1572. if (tmr->cancel)
  1573. goto done;
  1574. scic->power_control.phys_granted_power = 0;
  1575. if (scic->power_control.phys_waiting == 0) {
  1576. scic->power_control.timer_started = false;
  1577. goto done;
  1578. }
  1579. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1580. if (scic->power_control.phys_waiting == 0)
  1581. break;
  1582. sci_phy = scic->power_control.requesters[i];
  1583. if (sci_phy == NULL)
  1584. continue;
  1585. if (scic->power_control.phys_granted_power >=
  1586. scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up)
  1587. break;
  1588. scic->power_control.requesters[i] = NULL;
  1589. scic->power_control.phys_waiting--;
  1590. scic->power_control.phys_granted_power++;
  1591. scic_sds_phy_consume_power_handler(sci_phy);
  1592. }
  1593. /*
  1594. * It doesn't matter if the power list is empty, we need to start the
  1595. * timer in case another phy becomes ready.
  1596. */
  1597. sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  1598. scic->power_control.timer_started = true;
  1599. done:
  1600. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1601. }
  1602. /**
  1603. * This method inserts the phy in the stagger spinup control queue.
  1604. * @scic:
  1605. *
  1606. *
  1607. */
  1608. void scic_sds_controller_power_control_queue_insert(
  1609. struct scic_sds_controller *scic,
  1610. struct scic_sds_phy *sci_phy)
  1611. {
  1612. BUG_ON(sci_phy == NULL);
  1613. if (scic->power_control.phys_granted_power <
  1614. scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
  1615. scic->power_control.phys_granted_power++;
  1616. scic_sds_phy_consume_power_handler(sci_phy);
  1617. /*
  1618. * stop and start the power_control timer. When the timer fires, the
  1619. * no_of_phys_granted_power will be set to 0
  1620. */
  1621. if (scic->power_control.timer_started)
  1622. sci_del_timer(&scic->power_control.timer);
  1623. sci_mod_timer(&scic->power_control.timer,
  1624. SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  1625. scic->power_control.timer_started = true;
  1626. } else {
  1627. /* Add the phy in the waiting list */
  1628. scic->power_control.requesters[sci_phy->phy_index] = sci_phy;
  1629. scic->power_control.phys_waiting++;
  1630. }
  1631. }
  1632. /**
  1633. * This method removes the phy from the stagger spinup control queue.
  1634. * @scic:
  1635. *
  1636. *
  1637. */
  1638. void scic_sds_controller_power_control_queue_remove(
  1639. struct scic_sds_controller *scic,
  1640. struct scic_sds_phy *sci_phy)
  1641. {
  1642. BUG_ON(sci_phy == NULL);
  1643. if (scic->power_control.requesters[sci_phy->phy_index] != NULL) {
  1644. scic->power_control.phys_waiting--;
  1645. }
  1646. scic->power_control.requesters[sci_phy->phy_index] = NULL;
  1647. }
  1648. #define AFE_REGISTER_WRITE_DELAY 10
  1649. /* Initialize the AFE for this phy index. We need to read the AFE setup from
  1650. * the OEM parameters
  1651. */
  1652. static void scic_sds_controller_afe_initialization(struct scic_sds_controller *scic)
  1653. {
  1654. const struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
  1655. u32 afe_status;
  1656. u32 phy_id;
  1657. /* Clear DFX Status registers */
  1658. writel(0x0081000f, &scic->scu_registers->afe.afe_dfx_master_control0);
  1659. udelay(AFE_REGISTER_WRITE_DELAY);
  1660. if (is_b0()) {
  1661. /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
  1662. * Timer, PM Stagger Timer */
  1663. writel(0x0007BFFF, &scic->scu_registers->afe.afe_pmsn_master_control2);
  1664. udelay(AFE_REGISTER_WRITE_DELAY);
  1665. }
  1666. /* Configure bias currents to normal */
  1667. if (is_a0())
  1668. writel(0x00005500, &scic->scu_registers->afe.afe_bias_control);
  1669. else if (is_a2())
  1670. writel(0x00005A00, &scic->scu_registers->afe.afe_bias_control);
  1671. else if (is_b0() || is_c0())
  1672. writel(0x00005F00, &scic->scu_registers->afe.afe_bias_control);
  1673. udelay(AFE_REGISTER_WRITE_DELAY);
  1674. /* Enable PLL */
  1675. if (is_b0() || is_c0())
  1676. writel(0x80040A08, &scic->scu_registers->afe.afe_pll_control0);
  1677. else
  1678. writel(0x80040908, &scic->scu_registers->afe.afe_pll_control0);
  1679. udelay(AFE_REGISTER_WRITE_DELAY);
  1680. /* Wait for the PLL to lock */
  1681. do {
  1682. afe_status = readl(&scic->scu_registers->afe.afe_common_block_status);
  1683. udelay(AFE_REGISTER_WRITE_DELAY);
  1684. } while ((afe_status & 0x00001000) == 0);
  1685. if (is_a0() || is_a2()) {
  1686. /* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
  1687. writel(0x7bcc96ad, &scic->scu_registers->afe.afe_pmsn_master_control0);
  1688. udelay(AFE_REGISTER_WRITE_DELAY);
  1689. }
  1690. for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
  1691. const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
  1692. if (is_b0()) {
  1693. /* Configure transmitter SSC parameters */
  1694. writel(0x00030000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
  1695. udelay(AFE_REGISTER_WRITE_DELAY);
  1696. } else if (is_c0()) {
  1697. /* Configure transmitter SSC parameters */
  1698. writel(0x0003000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
  1699. udelay(AFE_REGISTER_WRITE_DELAY);
  1700. /*
  1701. * All defaults, except the Receive Word Alignament/Comma Detect
  1702. * Enable....(0xe800) */
  1703. writel(0x00004500, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
  1704. udelay(AFE_REGISTER_WRITE_DELAY);
  1705. } else {
  1706. /*
  1707. * All defaults, except the Receive Word Alignament/Comma Detect
  1708. * Enable....(0xe800) */
  1709. writel(0x00004512, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
  1710. udelay(AFE_REGISTER_WRITE_DELAY);
  1711. writel(0x0050100F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
  1712. udelay(AFE_REGISTER_WRITE_DELAY);
  1713. }
  1714. /*
  1715. * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
  1716. * & increase TX int & ext bias 20%....(0xe85c) */
  1717. if (is_a0())
  1718. writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1719. else if (is_a2())
  1720. writel(0x000003F0, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1721. else if (is_b0()) {
  1722. /* Power down TX and RX (PWRDNTX and PWRDNRX) */
  1723. writel(0x000003D7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1724. udelay(AFE_REGISTER_WRITE_DELAY);
  1725. /*
  1726. * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
  1727. * & increase TX int & ext bias 20%....(0xe85c) */
  1728. writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1729. } else {
  1730. writel(0x000001E7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1731. udelay(AFE_REGISTER_WRITE_DELAY);
  1732. /*
  1733. * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
  1734. * & increase TX int & ext bias 20%....(0xe85c) */
  1735. writel(0x000001E4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1736. }
  1737. udelay(AFE_REGISTER_WRITE_DELAY);
  1738. if (is_a0() || is_a2()) {
  1739. /* Enable TX equalization (0xe824) */
  1740. writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
  1741. udelay(AFE_REGISTER_WRITE_DELAY);
  1742. }
  1743. /*
  1744. * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
  1745. * RDD=0x0(RX Detect Enabled) ....(0xe800) */
  1746. writel(0x00004100, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
  1747. udelay(AFE_REGISTER_WRITE_DELAY);
  1748. /* Leave DFE/FFE on */
  1749. if (is_a0())
  1750. writel(0x3F09983F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
  1751. else if (is_a2())
  1752. writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
  1753. else if (is_b0()) {
  1754. writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
  1755. udelay(AFE_REGISTER_WRITE_DELAY);
  1756. /* Enable TX equalization (0xe824) */
  1757. writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
  1758. } else {
  1759. writel(0x0140DF0F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control1);
  1760. udelay(AFE_REGISTER_WRITE_DELAY);
  1761. writel(0x3F6F103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
  1762. udelay(AFE_REGISTER_WRITE_DELAY);
  1763. /* Enable TX equalization (0xe824) */
  1764. writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
  1765. }
  1766. udelay(AFE_REGISTER_WRITE_DELAY);
  1767. writel(oem_phy->afe_tx_amp_control0,
  1768. &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
  1769. udelay(AFE_REGISTER_WRITE_DELAY);
  1770. writel(oem_phy->afe_tx_amp_control1,
  1771. &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
  1772. udelay(AFE_REGISTER_WRITE_DELAY);
  1773. writel(oem_phy->afe_tx_amp_control2,
  1774. &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
  1775. udelay(AFE_REGISTER_WRITE_DELAY);
  1776. writel(oem_phy->afe_tx_amp_control3,
  1777. &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
  1778. udelay(AFE_REGISTER_WRITE_DELAY);
  1779. }
  1780. /* Transfer control to the PEs */
  1781. writel(0x00010f00, &scic->scu_registers->afe.afe_dfx_master_control0);
  1782. udelay(AFE_REGISTER_WRITE_DELAY);
  1783. }
  1784. static void scic_sds_controller_initialize_power_control(struct scic_sds_controller *scic)
  1785. {
  1786. sci_init_timer(&scic->power_control.timer, power_control_timeout);
  1787. memset(scic->power_control.requesters, 0,
  1788. sizeof(scic->power_control.requesters));
  1789. scic->power_control.phys_waiting = 0;
  1790. scic->power_control.phys_granted_power = 0;
  1791. }
  1792. static enum sci_status scic_controller_initialize(struct scic_sds_controller *scic)
  1793. {
  1794. struct sci_base_state_machine *sm = &scic->sm;
  1795. struct isci_host *ihost = scic_to_ihost(scic);
  1796. enum sci_status result = SCI_FAILURE;
  1797. unsigned long i, state, val;
  1798. if (scic->sm.current_state_id != SCIC_RESET) {
  1799. dev_warn(scic_to_dev(scic),
  1800. "SCIC Controller initialize operation requested "
  1801. "in invalid state\n");
  1802. return SCI_FAILURE_INVALID_STATE;
  1803. }
  1804. sci_change_state(sm, SCIC_INITIALIZING);
  1805. sci_init_timer(&scic->phy_timer, phy_startup_timeout);
  1806. scic->next_phy_to_start = 0;
  1807. scic->phy_startup_timer_pending = false;
  1808. scic_sds_controller_initialize_power_control(scic);
  1809. /*
  1810. * There is nothing to do here for B0 since we do not have to
  1811. * program the AFE registers.
  1812. * / @todo The AFE settings are supposed to be correct for the B0 but
  1813. * / presently they seem to be wrong. */
  1814. scic_sds_controller_afe_initialization(scic);
  1815. /* Take the hardware out of reset */
  1816. writel(0, &scic->smu_registers->soft_reset_control);
  1817. /*
  1818. * / @todo Provide meaningfull error code for hardware failure
  1819. * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
  1820. for (i = 100; i >= 1; i--) {
  1821. u32 status;
  1822. /* Loop until the hardware reports success */
  1823. udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
  1824. status = readl(&scic->smu_registers->control_status);
  1825. if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
  1826. break;
  1827. }
  1828. if (i == 0)
  1829. goto out;
  1830. /*
  1831. * Determine what are the actaul device capacities that the
  1832. * hardware will support */
  1833. val = readl(&scic->smu_registers->device_context_capacity);
  1834. /* Record the smaller of the two capacity values */
  1835. scic->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
  1836. scic->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
  1837. scic->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
  1838. /*
  1839. * Make all PEs that are unassigned match up with the
  1840. * logical ports
  1841. */
  1842. for (i = 0; i < scic->logical_port_entries; i++) {
  1843. struct scu_port_task_scheduler_group_registers __iomem
  1844. *ptsg = &scic->scu_registers->peg0.ptsg;
  1845. writel(i, &ptsg->protocol_engine[i]);
  1846. }
  1847. /* Initialize hardware PCI Relaxed ordering in DMA engines */
  1848. val = readl(&scic->scu_registers->sdma.pdma_configuration);
  1849. val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1850. writel(val, &scic->scu_registers->sdma.pdma_configuration);
  1851. val = readl(&scic->scu_registers->sdma.cdma_configuration);
  1852. val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1853. writel(val, &scic->scu_registers->sdma.cdma_configuration);
  1854. /*
  1855. * Initialize the PHYs before the PORTs because the PHY registers
  1856. * are accessed during the port initialization.
  1857. */
  1858. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1859. result = scic_sds_phy_initialize(&ihost->phys[i].sci,
  1860. &scic->scu_registers->peg0.pe[i].tl,
  1861. &scic->scu_registers->peg0.pe[i].ll);
  1862. if (result != SCI_SUCCESS)
  1863. goto out;
  1864. }
  1865. for (i = 0; i < scic->logical_port_entries; i++) {
  1866. result = scic_sds_port_initialize(&ihost->ports[i].sci,
  1867. &scic->scu_registers->peg0.ptsg.port[i],
  1868. &scic->scu_registers->peg0.ptsg.protocol_engine,
  1869. &scic->scu_registers->peg0.viit[i]);
  1870. if (result != SCI_SUCCESS)
  1871. goto out;
  1872. }
  1873. result = scic_sds_port_configuration_agent_initialize(scic, &scic->port_agent);
  1874. out:
  1875. /* Advance the controller state machine */
  1876. if (result == SCI_SUCCESS)
  1877. state = SCIC_INITIALIZED;
  1878. else
  1879. state = SCIC_FAILED;
  1880. sci_change_state(sm, state);
  1881. return result;
  1882. }
  1883. static enum sci_status scic_user_parameters_set(
  1884. struct scic_sds_controller *scic,
  1885. union scic_user_parameters *scic_parms)
  1886. {
  1887. u32 state = scic->sm.current_state_id;
  1888. if (state == SCIC_RESET ||
  1889. state == SCIC_INITIALIZING ||
  1890. state == SCIC_INITIALIZED) {
  1891. u16 index;
  1892. /*
  1893. * Validate the user parameters. If they are not legal, then
  1894. * return a failure.
  1895. */
  1896. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1897. struct sci_phy_user_params *user_phy;
  1898. user_phy = &scic_parms->sds1.phys[index];
  1899. if (!((user_phy->max_speed_generation <=
  1900. SCIC_SDS_PARM_MAX_SPEED) &&
  1901. (user_phy->max_speed_generation >
  1902. SCIC_SDS_PARM_NO_SPEED)))
  1903. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1904. if (user_phy->in_connection_align_insertion_frequency <
  1905. 3)
  1906. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1907. if ((user_phy->in_connection_align_insertion_frequency <
  1908. 3) ||
  1909. (user_phy->align_insertion_frequency == 0) ||
  1910. (user_phy->
  1911. notify_enable_spin_up_insertion_frequency ==
  1912. 0))
  1913. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1914. }
  1915. if ((scic_parms->sds1.stp_inactivity_timeout == 0) ||
  1916. (scic_parms->sds1.ssp_inactivity_timeout == 0) ||
  1917. (scic_parms->sds1.stp_max_occupancy_timeout == 0) ||
  1918. (scic_parms->sds1.ssp_max_occupancy_timeout == 0) ||
  1919. (scic_parms->sds1.no_outbound_task_timeout == 0))
  1920. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1921. memcpy(&scic->user_parameters, scic_parms, sizeof(*scic_parms));
  1922. return SCI_SUCCESS;
  1923. }
  1924. return SCI_FAILURE_INVALID_STATE;
  1925. }
  1926. static int scic_controller_mem_init(struct scic_sds_controller *scic)
  1927. {
  1928. struct device *dev = scic_to_dev(scic);
  1929. dma_addr_t dma;
  1930. size_t size;
  1931. int err;
  1932. size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
  1933. scic->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
  1934. if (!scic->completion_queue)
  1935. return -ENOMEM;
  1936. writel(lower_32_bits(dma), &scic->smu_registers->completion_queue_lower);
  1937. writel(upper_32_bits(dma), &scic->smu_registers->completion_queue_upper);
  1938. size = scic->remote_node_entries * sizeof(union scu_remote_node_context);
  1939. scic->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
  1940. GFP_KERNEL);
  1941. if (!scic->remote_node_context_table)
  1942. return -ENOMEM;
  1943. writel(lower_32_bits(dma), &scic->smu_registers->remote_node_context_lower);
  1944. writel(upper_32_bits(dma), &scic->smu_registers->remote_node_context_upper);
  1945. size = scic->task_context_entries * sizeof(struct scu_task_context),
  1946. scic->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
  1947. if (!scic->task_context_table)
  1948. return -ENOMEM;
  1949. scic->task_context_dma = dma;
  1950. writel(lower_32_bits(dma), &scic->smu_registers->host_task_table_lower);
  1951. writel(upper_32_bits(dma), &scic->smu_registers->host_task_table_upper);
  1952. err = scic_sds_unsolicited_frame_control_construct(scic);
  1953. if (err)
  1954. return err;
  1955. /*
  1956. * Inform the silicon as to the location of the UF headers and
  1957. * address table.
  1958. */
  1959. writel(lower_32_bits(scic->uf_control.headers.physical_address),
  1960. &scic->scu_registers->sdma.uf_header_base_address_lower);
  1961. writel(upper_32_bits(scic->uf_control.headers.physical_address),
  1962. &scic->scu_registers->sdma.uf_header_base_address_upper);
  1963. writel(lower_32_bits(scic->uf_control.address_table.physical_address),
  1964. &scic->scu_registers->sdma.uf_address_table_lower);
  1965. writel(upper_32_bits(scic->uf_control.address_table.physical_address),
  1966. &scic->scu_registers->sdma.uf_address_table_upper);
  1967. return 0;
  1968. }
  1969. int isci_host_init(struct isci_host *isci_host)
  1970. {
  1971. int err = 0, i;
  1972. enum sci_status status;
  1973. union scic_oem_parameters oem;
  1974. union scic_user_parameters scic_user_params;
  1975. struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
  1976. spin_lock_init(&isci_host->state_lock);
  1977. spin_lock_init(&isci_host->scic_lock);
  1978. init_waitqueue_head(&isci_host->eventq);
  1979. isci_host_change_state(isci_host, isci_starting);
  1980. status = scic_controller_construct(&isci_host->sci, scu_base(isci_host),
  1981. smu_base(isci_host));
  1982. if (status != SCI_SUCCESS) {
  1983. dev_err(&isci_host->pdev->dev,
  1984. "%s: scic_controller_construct failed - status = %x\n",
  1985. __func__,
  1986. status);
  1987. return -ENODEV;
  1988. }
  1989. isci_host->sas_ha.dev = &isci_host->pdev->dev;
  1990. isci_host->sas_ha.lldd_ha = isci_host;
  1991. /*
  1992. * grab initial values stored in the controller object for OEM and USER
  1993. * parameters
  1994. */
  1995. isci_user_parameters_get(isci_host, &scic_user_params);
  1996. status = scic_user_parameters_set(&isci_host->sci,
  1997. &scic_user_params);
  1998. if (status != SCI_SUCCESS) {
  1999. dev_warn(&isci_host->pdev->dev,
  2000. "%s: scic_user_parameters_set failed\n",
  2001. __func__);
  2002. return -ENODEV;
  2003. }
  2004. scic_oem_parameters_get(&isci_host->sci, &oem);
  2005. /* grab any OEM parameters specified in orom */
  2006. if (pci_info->orom) {
  2007. status = isci_parse_oem_parameters(&oem,
  2008. pci_info->orom,
  2009. isci_host->id);
  2010. if (status != SCI_SUCCESS) {
  2011. dev_warn(&isci_host->pdev->dev,
  2012. "parsing firmware oem parameters failed\n");
  2013. return -EINVAL;
  2014. }
  2015. }
  2016. status = scic_oem_parameters_set(&isci_host->sci, &oem);
  2017. if (status != SCI_SUCCESS) {
  2018. dev_warn(&isci_host->pdev->dev,
  2019. "%s: scic_oem_parameters_set failed\n",
  2020. __func__);
  2021. return -ENODEV;
  2022. }
  2023. tasklet_init(&isci_host->completion_tasklet,
  2024. isci_host_completion_routine, (unsigned long)isci_host);
  2025. INIT_LIST_HEAD(&isci_host->requests_to_complete);
  2026. INIT_LIST_HEAD(&isci_host->requests_to_errorback);
  2027. spin_lock_irq(&isci_host->scic_lock);
  2028. status = scic_controller_initialize(&isci_host->sci);
  2029. spin_unlock_irq(&isci_host->scic_lock);
  2030. if (status != SCI_SUCCESS) {
  2031. dev_warn(&isci_host->pdev->dev,
  2032. "%s: scic_controller_initialize failed -"
  2033. " status = 0x%x\n",
  2034. __func__, status);
  2035. return -ENODEV;
  2036. }
  2037. err = scic_controller_mem_init(&isci_host->sci);
  2038. if (err)
  2039. return err;
  2040. for (i = 0; i < SCI_MAX_PORTS; i++)
  2041. isci_port_init(&isci_host->ports[i], isci_host, i);
  2042. for (i = 0; i < SCI_MAX_PHYS; i++)
  2043. isci_phy_init(&isci_host->phys[i], isci_host, i);
  2044. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  2045. struct isci_remote_device *idev = &isci_host->devices[i];
  2046. INIT_LIST_HEAD(&idev->reqs_in_process);
  2047. INIT_LIST_HEAD(&idev->node);
  2048. }
  2049. for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
  2050. struct isci_request *ireq;
  2051. dma_addr_t dma;
  2052. ireq = dmam_alloc_coherent(&isci_host->pdev->dev,
  2053. sizeof(struct isci_request), &dma,
  2054. GFP_KERNEL);
  2055. if (!ireq)
  2056. return -ENOMEM;
  2057. ireq->sci.tc = &isci_host->sci.task_context_table[i];
  2058. ireq->sci.owning_controller = &isci_host->sci;
  2059. spin_lock_init(&ireq->state_lock);
  2060. ireq->request_daddr = dma;
  2061. ireq->isci_host = isci_host;
  2062. isci_host->reqs[i] = ireq;
  2063. }
  2064. return 0;
  2065. }
  2066. void scic_sds_controller_link_up(struct scic_sds_controller *scic,
  2067. struct scic_sds_port *port, struct scic_sds_phy *phy)
  2068. {
  2069. switch (scic->sm.current_state_id) {
  2070. case SCIC_STARTING:
  2071. sci_del_timer(&scic->phy_timer);
  2072. scic->phy_startup_timer_pending = false;
  2073. scic->port_agent.link_up_handler(scic, &scic->port_agent,
  2074. port, phy);
  2075. scic_sds_controller_start_next_phy(scic);
  2076. break;
  2077. case SCIC_READY:
  2078. scic->port_agent.link_up_handler(scic, &scic->port_agent,
  2079. port, phy);
  2080. break;
  2081. default:
  2082. dev_dbg(scic_to_dev(scic),
  2083. "%s: SCIC Controller linkup event from phy %d in "
  2084. "unexpected state %d\n", __func__, phy->phy_index,
  2085. scic->sm.current_state_id);
  2086. }
  2087. }
  2088. void scic_sds_controller_link_down(struct scic_sds_controller *scic,
  2089. struct scic_sds_port *port, struct scic_sds_phy *phy)
  2090. {
  2091. switch (scic->sm.current_state_id) {
  2092. case SCIC_STARTING:
  2093. case SCIC_READY:
  2094. scic->port_agent.link_down_handler(scic, &scic->port_agent,
  2095. port, phy);
  2096. break;
  2097. default:
  2098. dev_dbg(scic_to_dev(scic),
  2099. "%s: SCIC Controller linkdown event from phy %d in "
  2100. "unexpected state %d\n",
  2101. __func__,
  2102. phy->phy_index,
  2103. scic->sm.current_state_id);
  2104. }
  2105. }
  2106. /**
  2107. * This is a helper method to determine if any remote devices on this
  2108. * controller are still in the stopping state.
  2109. *
  2110. */
  2111. static bool scic_sds_controller_has_remote_devices_stopping(
  2112. struct scic_sds_controller *controller)
  2113. {
  2114. u32 index;
  2115. for (index = 0; index < controller->remote_node_entries; index++) {
  2116. if ((controller->device_table[index] != NULL) &&
  2117. (controller->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
  2118. return true;
  2119. }
  2120. return false;
  2121. }
  2122. /**
  2123. * This method is called by the remote device to inform the controller
  2124. * object that the remote device has stopped.
  2125. */
  2126. void scic_sds_controller_remote_device_stopped(struct scic_sds_controller *scic,
  2127. struct scic_sds_remote_device *sci_dev)
  2128. {
  2129. if (scic->sm.current_state_id != SCIC_STOPPING) {
  2130. dev_dbg(scic_to_dev(scic),
  2131. "SCIC Controller 0x%p remote device stopped event "
  2132. "from device 0x%p in unexpected state %d\n",
  2133. scic, sci_dev,
  2134. scic->sm.current_state_id);
  2135. return;
  2136. }
  2137. if (!scic_sds_controller_has_remote_devices_stopping(scic)) {
  2138. sci_change_state(&scic->sm, SCIC_STOPPED);
  2139. }
  2140. }
  2141. /**
  2142. * This method will write to the SCU PCP register the request value. The method
  2143. * is used to suspend/resume ports, devices, and phys.
  2144. * @scic:
  2145. *
  2146. *
  2147. */
  2148. void scic_sds_controller_post_request(
  2149. struct scic_sds_controller *scic,
  2150. u32 request)
  2151. {
  2152. dev_dbg(scic_to_dev(scic),
  2153. "%s: SCIC Controller 0x%p post request 0x%08x\n",
  2154. __func__,
  2155. scic,
  2156. request);
  2157. writel(request, &scic->smu_registers->post_context_port);
  2158. }
  2159. struct scic_sds_request *scic_request_by_tag(struct scic_sds_controller *scic, u16 io_tag)
  2160. {
  2161. u16 task_index;
  2162. u16 task_sequence;
  2163. task_index = ISCI_TAG_TCI(io_tag);
  2164. if (task_index < scic->task_context_entries) {
  2165. struct isci_request *ireq = scic_to_ihost(scic)->reqs[task_index];
  2166. if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
  2167. task_sequence = ISCI_TAG_SEQ(io_tag);
  2168. if (task_sequence == scic->io_request_sequence[task_index])
  2169. return &ireq->sci;
  2170. }
  2171. }
  2172. return NULL;
  2173. }
  2174. /**
  2175. * This method allocates remote node index and the reserves the remote node
  2176. * context space for use. This method can fail if there are no more remote
  2177. * node index available.
  2178. * @scic: This is the controller object which contains the set of
  2179. * free remote node ids
  2180. * @sci_dev: This is the device object which is requesting the a remote node
  2181. * id
  2182. * @node_id: This is the remote node id that is assinged to the device if one
  2183. * is available
  2184. *
  2185. * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
  2186. * node index available.
  2187. */
  2188. enum sci_status scic_sds_controller_allocate_remote_node_context(
  2189. struct scic_sds_controller *scic,
  2190. struct scic_sds_remote_device *sci_dev,
  2191. u16 *node_id)
  2192. {
  2193. u16 node_index;
  2194. u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
  2195. node_index = scic_sds_remote_node_table_allocate_remote_node(
  2196. &scic->available_remote_nodes, remote_node_count
  2197. );
  2198. if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  2199. scic->device_table[node_index] = sci_dev;
  2200. *node_id = node_index;
  2201. return SCI_SUCCESS;
  2202. }
  2203. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2204. }
  2205. /**
  2206. * This method frees the remote node index back to the available pool. Once
  2207. * this is done the remote node context buffer is no longer valid and can
  2208. * not be used.
  2209. * @scic:
  2210. * @sci_dev:
  2211. * @node_id:
  2212. *
  2213. */
  2214. void scic_sds_controller_free_remote_node_context(
  2215. struct scic_sds_controller *scic,
  2216. struct scic_sds_remote_device *sci_dev,
  2217. u16 node_id)
  2218. {
  2219. u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
  2220. if (scic->device_table[node_id] == sci_dev) {
  2221. scic->device_table[node_id] = NULL;
  2222. scic_sds_remote_node_table_release_remote_node_index(
  2223. &scic->available_remote_nodes, remote_node_count, node_id
  2224. );
  2225. }
  2226. }
  2227. /**
  2228. * This method returns the union scu_remote_node_context for the specified remote
  2229. * node id.
  2230. * @scic:
  2231. * @node_id:
  2232. *
  2233. * union scu_remote_node_context*
  2234. */
  2235. union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
  2236. struct scic_sds_controller *scic,
  2237. u16 node_id
  2238. ) {
  2239. if (
  2240. (node_id < scic->remote_node_entries)
  2241. && (scic->device_table[node_id] != NULL)
  2242. ) {
  2243. return &scic->remote_node_context_table[node_id];
  2244. }
  2245. return NULL;
  2246. }
  2247. /**
  2248. *
  2249. * @resposne_buffer: This is the buffer into which the D2H register FIS will be
  2250. * constructed.
  2251. * @frame_header: This is the frame header returned by the hardware.
  2252. * @frame_buffer: This is the frame buffer returned by the hardware.
  2253. *
  2254. * This method will combind the frame header and frame buffer to create a SATA
  2255. * D2H register FIS none
  2256. */
  2257. void scic_sds_controller_copy_sata_response(
  2258. void *response_buffer,
  2259. void *frame_header,
  2260. void *frame_buffer)
  2261. {
  2262. memcpy(response_buffer, frame_header, sizeof(u32));
  2263. memcpy(response_buffer + sizeof(u32),
  2264. frame_buffer,
  2265. sizeof(struct dev_to_host_fis) - sizeof(u32));
  2266. }
  2267. /**
  2268. * This method releases the frame once this is done the frame is available for
  2269. * re-use by the hardware. The data contained in the frame header and frame
  2270. * buffer is no longer valid. The UF queue get pointer is only updated if UF
  2271. * control indicates this is appropriate.
  2272. * @scic:
  2273. * @frame_index:
  2274. *
  2275. */
  2276. void scic_sds_controller_release_frame(
  2277. struct scic_sds_controller *scic,
  2278. u32 frame_index)
  2279. {
  2280. if (scic_sds_unsolicited_frame_control_release_frame(
  2281. &scic->uf_control, frame_index) == true)
  2282. writel(scic->uf_control.get,
  2283. &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
  2284. }
  2285. void isci_tci_free(struct isci_host *ihost, u16 tci)
  2286. {
  2287. u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
  2288. ihost->tci_pool[tail] = tci;
  2289. ihost->tci_tail = tail + 1;
  2290. }
  2291. static u16 isci_tci_alloc(struct isci_host *ihost)
  2292. {
  2293. u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
  2294. u16 tci = ihost->tci_pool[head];
  2295. ihost->tci_head = head + 1;
  2296. return tci;
  2297. }
  2298. static u16 isci_tci_space(struct isci_host *ihost)
  2299. {
  2300. return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
  2301. }
  2302. u16 isci_alloc_tag(struct isci_host *ihost)
  2303. {
  2304. if (isci_tci_space(ihost)) {
  2305. u16 tci = isci_tci_alloc(ihost);
  2306. u8 seq = ihost->sci.io_request_sequence[tci];
  2307. return ISCI_TAG(seq, tci);
  2308. }
  2309. return SCI_CONTROLLER_INVALID_IO_TAG;
  2310. }
  2311. enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
  2312. {
  2313. struct scic_sds_controller *scic = &ihost->sci;
  2314. u16 tci = ISCI_TAG_TCI(io_tag);
  2315. u16 seq = ISCI_TAG_SEQ(io_tag);
  2316. /* prevent tail from passing head */
  2317. if (isci_tci_active(ihost) == 0)
  2318. return SCI_FAILURE_INVALID_IO_TAG;
  2319. if (seq == scic->io_request_sequence[tci]) {
  2320. scic->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
  2321. isci_tci_free(ihost, tci);
  2322. return SCI_SUCCESS;
  2323. }
  2324. return SCI_FAILURE_INVALID_IO_TAG;
  2325. }
  2326. /**
  2327. * scic_controller_start_io() - This method is called by the SCI user to
  2328. * send/start an IO request. If the method invocation is successful, then
  2329. * the IO request has been queued to the hardware for processing.
  2330. * @controller: the handle to the controller object for which to start an IO
  2331. * request.
  2332. * @remote_device: the handle to the remote device object for which to start an
  2333. * IO request.
  2334. * @io_request: the handle to the io request object to start.
  2335. * @io_tag: This parameter specifies a previously allocated IO tag that the
  2336. * user desires to be utilized for this request.
  2337. */
  2338. enum sci_status scic_controller_start_io(struct scic_sds_controller *scic,
  2339. struct scic_sds_remote_device *rdev,
  2340. struct scic_sds_request *req)
  2341. {
  2342. enum sci_status status;
  2343. if (scic->sm.current_state_id != SCIC_READY) {
  2344. dev_warn(scic_to_dev(scic), "invalid state to start I/O");
  2345. return SCI_FAILURE_INVALID_STATE;
  2346. }
  2347. status = scic_sds_remote_device_start_io(scic, rdev, req);
  2348. if (status != SCI_SUCCESS)
  2349. return status;
  2350. set_bit(IREQ_ACTIVE, &sci_req_to_ireq(req)->flags);
  2351. scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(req));
  2352. return SCI_SUCCESS;
  2353. }
  2354. /**
  2355. * scic_controller_terminate_request() - This method is called by the SCI Core
  2356. * user to terminate an ongoing (i.e. started) core IO request. This does
  2357. * not abort the IO request at the target, but rather removes the IO request
  2358. * from the host controller.
  2359. * @controller: the handle to the controller object for which to terminate a
  2360. * request.
  2361. * @remote_device: the handle to the remote device object for which to
  2362. * terminate a request.
  2363. * @request: the handle to the io or task management request object to
  2364. * terminate.
  2365. *
  2366. * Indicate if the controller successfully began the terminate process for the
  2367. * IO request. SCI_SUCCESS if the terminate process was successfully started
  2368. * for the request. Determine the failure situations and return values.
  2369. */
  2370. enum sci_status scic_controller_terminate_request(
  2371. struct scic_sds_controller *scic,
  2372. struct scic_sds_remote_device *rdev,
  2373. struct scic_sds_request *req)
  2374. {
  2375. enum sci_status status;
  2376. if (scic->sm.current_state_id != SCIC_READY) {
  2377. dev_warn(scic_to_dev(scic),
  2378. "invalid state to terminate request\n");
  2379. return SCI_FAILURE_INVALID_STATE;
  2380. }
  2381. status = scic_sds_io_request_terminate(req);
  2382. if (status != SCI_SUCCESS)
  2383. return status;
  2384. /*
  2385. * Utilize the original post context command and or in the POST_TC_ABORT
  2386. * request sub-type.
  2387. */
  2388. scic_sds_controller_post_request(scic,
  2389. scic_sds_request_get_post_context(req) |
  2390. SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
  2391. return SCI_SUCCESS;
  2392. }
  2393. /**
  2394. * scic_controller_complete_io() - This method will perform core specific
  2395. * completion operations for an IO request. After this method is invoked,
  2396. * the user should consider the IO request as invalid until it is properly
  2397. * reused (i.e. re-constructed).
  2398. * @controller: The handle to the controller object for which to complete the
  2399. * IO request.
  2400. * @remote_device: The handle to the remote device object for which to complete
  2401. * the IO request.
  2402. * @io_request: the handle to the io request object to complete.
  2403. */
  2404. enum sci_status scic_controller_complete_io(
  2405. struct scic_sds_controller *scic,
  2406. struct scic_sds_remote_device *rdev,
  2407. struct scic_sds_request *request)
  2408. {
  2409. enum sci_status status;
  2410. u16 index;
  2411. switch (scic->sm.current_state_id) {
  2412. case SCIC_STOPPING:
  2413. /* XXX: Implement this function */
  2414. return SCI_FAILURE;
  2415. case SCIC_READY:
  2416. status = scic_sds_remote_device_complete_io(scic, rdev, request);
  2417. if (status != SCI_SUCCESS)
  2418. return status;
  2419. index = ISCI_TAG_TCI(request->io_tag);
  2420. clear_bit(IREQ_ACTIVE, &sci_req_to_ireq(request)->flags);
  2421. return SCI_SUCCESS;
  2422. default:
  2423. dev_warn(scic_to_dev(scic), "invalid state to complete I/O");
  2424. return SCI_FAILURE_INVALID_STATE;
  2425. }
  2426. }
  2427. enum sci_status scic_controller_continue_io(struct scic_sds_request *sci_req)
  2428. {
  2429. struct scic_sds_controller *scic = sci_req->owning_controller;
  2430. if (scic->sm.current_state_id != SCIC_READY) {
  2431. dev_warn(scic_to_dev(scic), "invalid state to continue I/O");
  2432. return SCI_FAILURE_INVALID_STATE;
  2433. }
  2434. set_bit(IREQ_ACTIVE, &sci_req_to_ireq(sci_req)->flags);
  2435. scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(sci_req));
  2436. return SCI_SUCCESS;
  2437. }
  2438. /**
  2439. * scic_controller_start_task() - This method is called by the SCIC user to
  2440. * send/start a framework task management request.
  2441. * @controller: the handle to the controller object for which to start the task
  2442. * management request.
  2443. * @remote_device: the handle to the remote device object for which to start
  2444. * the task management request.
  2445. * @task_request: the handle to the task request object to start.
  2446. */
  2447. enum sci_task_status scic_controller_start_task(
  2448. struct scic_sds_controller *scic,
  2449. struct scic_sds_remote_device *rdev,
  2450. struct scic_sds_request *req)
  2451. {
  2452. struct isci_request *ireq = sci_req_to_ireq(req);
  2453. enum sci_status status;
  2454. if (scic->sm.current_state_id != SCIC_READY) {
  2455. dev_warn(scic_to_dev(scic),
  2456. "%s: SCIC Controller starting task from invalid "
  2457. "state\n",
  2458. __func__);
  2459. return SCI_TASK_FAILURE_INVALID_STATE;
  2460. }
  2461. status = scic_sds_remote_device_start_task(scic, rdev, req);
  2462. switch (status) {
  2463. case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
  2464. set_bit(IREQ_ACTIVE, &ireq->flags);
  2465. /*
  2466. * We will let framework know this task request started successfully,
  2467. * although core is still woring on starting the request (to post tc when
  2468. * RNC is resumed.)
  2469. */
  2470. return SCI_SUCCESS;
  2471. case SCI_SUCCESS:
  2472. set_bit(IREQ_ACTIVE, &ireq->flags);
  2473. scic_sds_controller_post_request(scic,
  2474. scic_sds_request_get_post_context(req));
  2475. break;
  2476. default:
  2477. break;
  2478. }
  2479. return status;
  2480. }