request.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466
  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 "isci.h"
  56. #include "task.h"
  57. #include "request.h"
  58. #include "sata.h"
  59. #include "scu_completion_codes.h"
  60. #include "scu_event_codes.h"
  61. #include "sas.h"
  62. static struct scu_sgl_element_pair *to_sgl_element_pair(struct isci_request *ireq,
  63. int idx)
  64. {
  65. if (idx == 0)
  66. return &ireq->tc->sgl_pair_ab;
  67. else if (idx == 1)
  68. return &ireq->tc->sgl_pair_cd;
  69. else if (idx < 0)
  70. return NULL;
  71. else
  72. return &ireq->sg_table[idx - 2];
  73. }
  74. static dma_addr_t to_sgl_element_pair_dma(struct scic_sds_controller *scic,
  75. struct isci_request *ireq, u32 idx)
  76. {
  77. u32 offset;
  78. if (idx == 0) {
  79. offset = (void *) &ireq->tc->sgl_pair_ab -
  80. (void *) &scic->task_context_table[0];
  81. return scic->task_context_dma + offset;
  82. } else if (idx == 1) {
  83. offset = (void *) &ireq->tc->sgl_pair_cd -
  84. (void *) &scic->task_context_table[0];
  85. return scic->task_context_dma + offset;
  86. }
  87. return scic_io_request_get_dma_addr(ireq, &ireq->sg_table[idx - 2]);
  88. }
  89. static void init_sgl_element(struct scu_sgl_element *e, struct scatterlist *sg)
  90. {
  91. e->length = sg_dma_len(sg);
  92. e->address_upper = upper_32_bits(sg_dma_address(sg));
  93. e->address_lower = lower_32_bits(sg_dma_address(sg));
  94. e->address_modifier = 0;
  95. }
  96. static void scic_sds_request_build_sgl(struct isci_request *ireq)
  97. {
  98. struct isci_host *isci_host = ireq->isci_host;
  99. struct scic_sds_controller *scic = &isci_host->sci;
  100. struct sas_task *task = isci_request_access_task(ireq);
  101. struct scatterlist *sg = NULL;
  102. dma_addr_t dma_addr;
  103. u32 sg_idx = 0;
  104. struct scu_sgl_element_pair *scu_sg = NULL;
  105. struct scu_sgl_element_pair *prev_sg = NULL;
  106. if (task->num_scatter > 0) {
  107. sg = task->scatter;
  108. while (sg) {
  109. scu_sg = to_sgl_element_pair(ireq, sg_idx);
  110. init_sgl_element(&scu_sg->A, sg);
  111. sg = sg_next(sg);
  112. if (sg) {
  113. init_sgl_element(&scu_sg->B, sg);
  114. sg = sg_next(sg);
  115. } else
  116. memset(&scu_sg->B, 0, sizeof(scu_sg->B));
  117. if (prev_sg) {
  118. dma_addr = to_sgl_element_pair_dma(scic,
  119. ireq,
  120. sg_idx);
  121. prev_sg->next_pair_upper =
  122. upper_32_bits(dma_addr);
  123. prev_sg->next_pair_lower =
  124. lower_32_bits(dma_addr);
  125. }
  126. prev_sg = scu_sg;
  127. sg_idx++;
  128. }
  129. } else { /* handle when no sg */
  130. scu_sg = to_sgl_element_pair(ireq, sg_idx);
  131. dma_addr = dma_map_single(&isci_host->pdev->dev,
  132. task->scatter,
  133. task->total_xfer_len,
  134. task->data_dir);
  135. ireq->zero_scatter_daddr = dma_addr;
  136. scu_sg->A.length = task->total_xfer_len;
  137. scu_sg->A.address_upper = upper_32_bits(dma_addr);
  138. scu_sg->A.address_lower = lower_32_bits(dma_addr);
  139. }
  140. if (scu_sg) {
  141. scu_sg->next_pair_upper = 0;
  142. scu_sg->next_pair_lower = 0;
  143. }
  144. }
  145. static void scic_sds_io_request_build_ssp_command_iu(struct isci_request *ireq)
  146. {
  147. struct ssp_cmd_iu *cmd_iu;
  148. struct sas_task *task = isci_request_access_task(ireq);
  149. cmd_iu = &ireq->ssp.cmd;
  150. memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
  151. cmd_iu->add_cdb_len = 0;
  152. cmd_iu->_r_a = 0;
  153. cmd_iu->_r_b = 0;
  154. cmd_iu->en_fburst = 0; /* unsupported */
  155. cmd_iu->task_prio = task->ssp_task.task_prio;
  156. cmd_iu->task_attr = task->ssp_task.task_attr;
  157. cmd_iu->_r_c = 0;
  158. sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
  159. sizeof(task->ssp_task.cdb) / sizeof(u32));
  160. }
  161. static void scic_sds_task_request_build_ssp_task_iu(struct isci_request *ireq)
  162. {
  163. struct ssp_task_iu *task_iu;
  164. struct sas_task *task = isci_request_access_task(ireq);
  165. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  166. task_iu = &ireq->ssp.tmf;
  167. memset(task_iu, 0, sizeof(struct ssp_task_iu));
  168. memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
  169. task_iu->task_func = isci_tmf->tmf_code;
  170. task_iu->task_tag =
  171. (ireq->ttype == tmf_task) ?
  172. isci_tmf->io_tag :
  173. SCI_CONTROLLER_INVALID_IO_TAG;
  174. }
  175. /**
  176. * This method is will fill in the SCU Task Context for any type of SSP request.
  177. * @sci_req:
  178. * @task_context:
  179. *
  180. */
  181. static void scu_ssp_reqeust_construct_task_context(
  182. struct isci_request *ireq,
  183. struct scu_task_context *task_context)
  184. {
  185. dma_addr_t dma_addr;
  186. struct scic_sds_remote_device *target_device;
  187. struct isci_port *iport;
  188. target_device = scic_sds_request_get_device(ireq);
  189. iport = scic_sds_request_get_port(ireq);
  190. /* Fill in the TC with the its required data */
  191. task_context->abort = 0;
  192. task_context->priority = 0;
  193. task_context->initiator_request = 1;
  194. task_context->connection_rate = target_device->connection_rate;
  195. task_context->protocol_engine_index =
  196. scic_sds_controller_get_protocol_engine_group(controller);
  197. task_context->logical_port_index = scic_sds_port_get_index(iport);
  198. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
  199. task_context->valid = SCU_TASK_CONTEXT_VALID;
  200. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  201. task_context->remote_node_index =
  202. scic_sds_remote_device_get_index(ireq->target_device);
  203. task_context->command_code = 0;
  204. task_context->link_layer_control = 0;
  205. task_context->do_not_dma_ssp_good_response = 1;
  206. task_context->strict_ordering = 0;
  207. task_context->control_frame = 0;
  208. task_context->timeout_enable = 0;
  209. task_context->block_guard_enable = 0;
  210. task_context->address_modifier = 0;
  211. /* task_context->type.ssp.tag = ireq->io_tag; */
  212. task_context->task_phase = 0x01;
  213. ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  214. (scic_sds_controller_get_protocol_engine_group(controller) <<
  215. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  216. (scic_sds_port_get_index(iport) <<
  217. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  218. ISCI_TAG_TCI(ireq->io_tag));
  219. /*
  220. * Copy the physical address for the command buffer to the
  221. * SCU Task Context
  222. */
  223. dma_addr = scic_io_request_get_dma_addr(ireq, &ireq->ssp.cmd);
  224. task_context->command_iu_upper = upper_32_bits(dma_addr);
  225. task_context->command_iu_lower = lower_32_bits(dma_addr);
  226. /*
  227. * Copy the physical address for the response buffer to the
  228. * SCU Task Context
  229. */
  230. dma_addr = scic_io_request_get_dma_addr(ireq, &ireq->ssp.rsp);
  231. task_context->response_iu_upper = upper_32_bits(dma_addr);
  232. task_context->response_iu_lower = lower_32_bits(dma_addr);
  233. }
  234. /**
  235. * This method is will fill in the SCU Task Context for a SSP IO request.
  236. * @sci_req:
  237. *
  238. */
  239. static void scu_ssp_io_request_construct_task_context(struct isci_request *ireq,
  240. enum dma_data_direction dir,
  241. u32 len)
  242. {
  243. struct scu_task_context *task_context = ireq->tc;
  244. scu_ssp_reqeust_construct_task_context(ireq, task_context);
  245. task_context->ssp_command_iu_length =
  246. sizeof(struct ssp_cmd_iu) / sizeof(u32);
  247. task_context->type.ssp.frame_type = SSP_COMMAND;
  248. switch (dir) {
  249. case DMA_FROM_DEVICE:
  250. case DMA_NONE:
  251. default:
  252. task_context->task_type = SCU_TASK_TYPE_IOREAD;
  253. break;
  254. case DMA_TO_DEVICE:
  255. task_context->task_type = SCU_TASK_TYPE_IOWRITE;
  256. break;
  257. }
  258. task_context->transfer_length_bytes = len;
  259. if (task_context->transfer_length_bytes > 0)
  260. scic_sds_request_build_sgl(ireq);
  261. }
  262. /**
  263. * This method will fill in the SCU Task Context for a SSP Task request. The
  264. * following important settings are utilized: -# priority ==
  265. * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
  266. * ahead of other task destined for the same Remote Node. -# task_type ==
  267. * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
  268. * (i.e. non-raw frame) is being utilized to perform task management. -#
  269. * control_frame == 1. This ensures that the proper endianess is set so
  270. * that the bytes are transmitted in the right order for a task frame.
  271. * @sci_req: This parameter specifies the task request object being
  272. * constructed.
  273. *
  274. */
  275. static void scu_ssp_task_request_construct_task_context(struct isci_request *ireq)
  276. {
  277. struct scu_task_context *task_context = ireq->tc;
  278. scu_ssp_reqeust_construct_task_context(ireq, task_context);
  279. task_context->control_frame = 1;
  280. task_context->priority = SCU_TASK_PRIORITY_HIGH;
  281. task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
  282. task_context->transfer_length_bytes = 0;
  283. task_context->type.ssp.frame_type = SSP_TASK;
  284. task_context->ssp_command_iu_length =
  285. sizeof(struct ssp_task_iu) / sizeof(u32);
  286. }
  287. /**
  288. * This method is will fill in the SCU Task Context for any type of SATA
  289. * request. This is called from the various SATA constructors.
  290. * @sci_req: The general IO request object which is to be used in
  291. * constructing the SCU task context.
  292. * @task_context: The buffer pointer for the SCU task context which is being
  293. * constructed.
  294. *
  295. * The general io request construction is complete. The buffer assignment for
  296. * the command buffer is complete. none Revisit task context construction to
  297. * determine what is common for SSP/SMP/STP task context structures.
  298. */
  299. static void scu_sata_reqeust_construct_task_context(
  300. struct isci_request *ireq,
  301. struct scu_task_context *task_context)
  302. {
  303. dma_addr_t dma_addr;
  304. struct scic_sds_remote_device *target_device;
  305. struct isci_port *iport;
  306. target_device = scic_sds_request_get_device(ireq);
  307. iport = scic_sds_request_get_port(ireq);
  308. /* Fill in the TC with the its required data */
  309. task_context->abort = 0;
  310. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  311. task_context->initiator_request = 1;
  312. task_context->connection_rate = target_device->connection_rate;
  313. task_context->protocol_engine_index =
  314. scic_sds_controller_get_protocol_engine_group(controller);
  315. task_context->logical_port_index =
  316. scic_sds_port_get_index(iport);
  317. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
  318. task_context->valid = SCU_TASK_CONTEXT_VALID;
  319. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  320. task_context->remote_node_index =
  321. scic_sds_remote_device_get_index(ireq->target_device);
  322. task_context->command_code = 0;
  323. task_context->link_layer_control = 0;
  324. task_context->do_not_dma_ssp_good_response = 1;
  325. task_context->strict_ordering = 0;
  326. task_context->control_frame = 0;
  327. task_context->timeout_enable = 0;
  328. task_context->block_guard_enable = 0;
  329. task_context->address_modifier = 0;
  330. task_context->task_phase = 0x01;
  331. task_context->ssp_command_iu_length =
  332. (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
  333. /* Set the first word of the H2D REG FIS */
  334. task_context->type.words[0] = *(u32 *)&ireq->stp.cmd;
  335. ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  336. (scic_sds_controller_get_protocol_engine_group(controller) <<
  337. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  338. (scic_sds_port_get_index(iport) <<
  339. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  340. ISCI_TAG_TCI(ireq->io_tag));
  341. /*
  342. * Copy the physical address for the command buffer to the SCU Task
  343. * Context. We must offset the command buffer by 4 bytes because the
  344. * first 4 bytes are transfered in the body of the TC.
  345. */
  346. dma_addr = scic_io_request_get_dma_addr(ireq,
  347. ((char *) &ireq->stp.cmd) +
  348. sizeof(u32));
  349. task_context->command_iu_upper = upper_32_bits(dma_addr);
  350. task_context->command_iu_lower = lower_32_bits(dma_addr);
  351. /* SATA Requests do not have a response buffer */
  352. task_context->response_iu_upper = 0;
  353. task_context->response_iu_lower = 0;
  354. }
  355. static void scu_stp_raw_request_construct_task_context(struct isci_request *ireq)
  356. {
  357. struct scu_task_context *task_context = ireq->tc;
  358. scu_sata_reqeust_construct_task_context(ireq, task_context);
  359. task_context->control_frame = 0;
  360. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  361. task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
  362. task_context->type.stp.fis_type = FIS_REGH2D;
  363. task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
  364. }
  365. static enum sci_status scic_sds_stp_pio_request_construct(struct isci_request *ireq,
  366. bool copy_rx_frame)
  367. {
  368. struct isci_stp_request *stp_req = &ireq->stp.req;
  369. scu_stp_raw_request_construct_task_context(ireq);
  370. stp_req->status = 0;
  371. stp_req->sgl.offset = 0;
  372. stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
  373. if (copy_rx_frame) {
  374. scic_sds_request_build_sgl(ireq);
  375. stp_req->sgl.index = 0;
  376. } else {
  377. /* The user does not want the data copied to the SGL buffer location */
  378. stp_req->sgl.index = -1;
  379. }
  380. return SCI_SUCCESS;
  381. }
  382. /**
  383. *
  384. * @sci_req: This parameter specifies the request to be constructed as an
  385. * optimized request.
  386. * @optimized_task_type: This parameter specifies whether the request is to be
  387. * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
  388. * value of 1 indicates NCQ.
  389. *
  390. * This method will perform request construction common to all types of STP
  391. * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
  392. * returns an indication as to whether the construction was successful.
  393. */
  394. static void scic_sds_stp_optimized_request_construct(struct isci_request *ireq,
  395. u8 optimized_task_type,
  396. u32 len,
  397. enum dma_data_direction dir)
  398. {
  399. struct scu_task_context *task_context = ireq->tc;
  400. /* Build the STP task context structure */
  401. scu_sata_reqeust_construct_task_context(ireq, task_context);
  402. /* Copy over the SGL elements */
  403. scic_sds_request_build_sgl(ireq);
  404. /* Copy over the number of bytes to be transfered */
  405. task_context->transfer_length_bytes = len;
  406. if (dir == DMA_TO_DEVICE) {
  407. /*
  408. * The difference between the DMA IN and DMA OUT request task type
  409. * values are consistent with the difference between FPDMA READ
  410. * and FPDMA WRITE values. Add the supplied task type parameter
  411. * to this difference to set the task type properly for this
  412. * DATA OUT (WRITE) case. */
  413. task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
  414. - SCU_TASK_TYPE_DMA_IN);
  415. } else {
  416. /*
  417. * For the DATA IN (READ) case, simply save the supplied
  418. * optimized task type. */
  419. task_context->task_type = optimized_task_type;
  420. }
  421. }
  422. static enum sci_status
  423. scic_io_request_construct_sata(struct isci_request *ireq,
  424. u32 len,
  425. enum dma_data_direction dir,
  426. bool copy)
  427. {
  428. enum sci_status status = SCI_SUCCESS;
  429. struct sas_task *task = isci_request_access_task(ireq);
  430. /* check for management protocols */
  431. if (ireq->ttype == tmf_task) {
  432. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  433. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  434. tmf->tmf_code == isci_tmf_sata_srst_low) {
  435. scu_stp_raw_request_construct_task_context(ireq);
  436. return SCI_SUCCESS;
  437. } else {
  438. dev_err(scic_to_dev(ireq->owning_controller),
  439. "%s: Request 0x%p received un-handled SAT "
  440. "management protocol 0x%x.\n",
  441. __func__, ireq, tmf->tmf_code);
  442. return SCI_FAILURE;
  443. }
  444. }
  445. if (!sas_protocol_ata(task->task_proto)) {
  446. dev_err(scic_to_dev(ireq->owning_controller),
  447. "%s: Non-ATA protocol in SATA path: 0x%x\n",
  448. __func__,
  449. task->task_proto);
  450. return SCI_FAILURE;
  451. }
  452. /* non data */
  453. if (task->data_dir == DMA_NONE) {
  454. scu_stp_raw_request_construct_task_context(ireq);
  455. return SCI_SUCCESS;
  456. }
  457. /* NCQ */
  458. if (task->ata_task.use_ncq) {
  459. scic_sds_stp_optimized_request_construct(ireq,
  460. SCU_TASK_TYPE_FPDMAQ_READ,
  461. len, dir);
  462. return SCI_SUCCESS;
  463. }
  464. /* DMA */
  465. if (task->ata_task.dma_xfer) {
  466. scic_sds_stp_optimized_request_construct(ireq,
  467. SCU_TASK_TYPE_DMA_IN,
  468. len, dir);
  469. return SCI_SUCCESS;
  470. } else /* PIO */
  471. return scic_sds_stp_pio_request_construct(ireq, copy);
  472. return status;
  473. }
  474. static enum sci_status scic_io_request_construct_basic_ssp(struct isci_request *ireq)
  475. {
  476. struct sas_task *task = isci_request_access_task(ireq);
  477. ireq->protocol = SCIC_SSP_PROTOCOL;
  478. scu_ssp_io_request_construct_task_context(ireq,
  479. task->data_dir,
  480. task->total_xfer_len);
  481. scic_sds_io_request_build_ssp_command_iu(ireq);
  482. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  483. return SCI_SUCCESS;
  484. }
  485. enum sci_status scic_task_request_construct_ssp(
  486. struct isci_request *ireq)
  487. {
  488. /* Construct the SSP Task SCU Task Context */
  489. scu_ssp_task_request_construct_task_context(ireq);
  490. /* Fill in the SSP Task IU */
  491. scic_sds_task_request_build_ssp_task_iu(ireq);
  492. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  493. return SCI_SUCCESS;
  494. }
  495. static enum sci_status scic_io_request_construct_basic_sata(struct isci_request *ireq)
  496. {
  497. enum sci_status status;
  498. bool copy = false;
  499. struct sas_task *task = isci_request_access_task(ireq);
  500. ireq->protocol = SCIC_STP_PROTOCOL;
  501. copy = (task->data_dir == DMA_NONE) ? false : true;
  502. status = scic_io_request_construct_sata(ireq,
  503. task->total_xfer_len,
  504. task->data_dir,
  505. copy);
  506. if (status == SCI_SUCCESS)
  507. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  508. return status;
  509. }
  510. enum sci_status scic_task_request_construct_sata(struct isci_request *ireq)
  511. {
  512. enum sci_status status = SCI_SUCCESS;
  513. /* check for management protocols */
  514. if (ireq->ttype == tmf_task) {
  515. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  516. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  517. tmf->tmf_code == isci_tmf_sata_srst_low) {
  518. scu_stp_raw_request_construct_task_context(ireq);
  519. } else {
  520. dev_err(scic_to_dev(ireq->owning_controller),
  521. "%s: Request 0x%p received un-handled SAT "
  522. "Protocol 0x%x.\n",
  523. __func__, ireq, tmf->tmf_code);
  524. return SCI_FAILURE;
  525. }
  526. }
  527. if (status != SCI_SUCCESS)
  528. return status;
  529. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  530. return status;
  531. }
  532. /**
  533. * sci_req_tx_bytes - bytes transferred when reply underruns request
  534. * @sci_req: request that was terminated early
  535. */
  536. #define SCU_TASK_CONTEXT_SRAM 0x200000
  537. static u32 sci_req_tx_bytes(struct isci_request *ireq)
  538. {
  539. struct scic_sds_controller *scic = ireq->owning_controller;
  540. u32 ret_val = 0;
  541. if (readl(&scic->smu_registers->address_modifier) == 0) {
  542. void __iomem *scu_reg_base = scic->scu_registers;
  543. /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
  544. * BAR1 is the scu_registers
  545. * 0x20002C = 0x200000 + 0x2c
  546. * = start of task context SRAM + offset of (type.ssp.data_offset)
  547. * TCi is the io_tag of struct scic_sds_request
  548. */
  549. ret_val = readl(scu_reg_base +
  550. (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
  551. ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(ireq->io_tag)));
  552. }
  553. return ret_val;
  554. }
  555. enum sci_status scic_sds_request_start(struct isci_request *ireq)
  556. {
  557. enum sci_base_request_states state;
  558. struct scu_task_context *tc = ireq->tc;
  559. struct scic_sds_controller *scic = ireq->owning_controller;
  560. state = ireq->sm.current_state_id;
  561. if (state != SCI_REQ_CONSTRUCTED) {
  562. dev_warn(scic_to_dev(scic),
  563. "%s: SCIC IO Request requested to start while in wrong "
  564. "state %d\n", __func__, state);
  565. return SCI_FAILURE_INVALID_STATE;
  566. }
  567. tc->task_index = ISCI_TAG_TCI(ireq->io_tag);
  568. switch (tc->protocol_type) {
  569. case SCU_TASK_CONTEXT_PROTOCOL_SMP:
  570. case SCU_TASK_CONTEXT_PROTOCOL_SSP:
  571. /* SSP/SMP Frame */
  572. tc->type.ssp.tag = ireq->io_tag;
  573. tc->type.ssp.target_port_transfer_tag = 0xFFFF;
  574. break;
  575. case SCU_TASK_CONTEXT_PROTOCOL_STP:
  576. /* STP/SATA Frame
  577. * tc->type.stp.ncq_tag = ireq->ncq_tag;
  578. */
  579. break;
  580. case SCU_TASK_CONTEXT_PROTOCOL_NONE:
  581. /* / @todo When do we set no protocol type? */
  582. break;
  583. default:
  584. /* This should never happen since we build the IO
  585. * requests */
  586. break;
  587. }
  588. /* Add to the post_context the io tag value */
  589. ireq->post_context |= ISCI_TAG_TCI(ireq->io_tag);
  590. /* Everything is good go ahead and change state */
  591. sci_change_state(&ireq->sm, SCI_REQ_STARTED);
  592. return SCI_SUCCESS;
  593. }
  594. enum sci_status
  595. scic_sds_io_request_terminate(struct isci_request *ireq)
  596. {
  597. enum sci_base_request_states state;
  598. state = ireq->sm.current_state_id;
  599. switch (state) {
  600. case SCI_REQ_CONSTRUCTED:
  601. scic_sds_request_set_status(ireq,
  602. SCU_TASK_DONE_TASK_ABORT,
  603. SCI_FAILURE_IO_TERMINATED);
  604. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  605. return SCI_SUCCESS;
  606. case SCI_REQ_STARTED:
  607. case SCI_REQ_TASK_WAIT_TC_COMP:
  608. case SCI_REQ_SMP_WAIT_RESP:
  609. case SCI_REQ_SMP_WAIT_TC_COMP:
  610. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  611. case SCI_REQ_STP_UDMA_WAIT_D2H:
  612. case SCI_REQ_STP_NON_DATA_WAIT_H2D:
  613. case SCI_REQ_STP_NON_DATA_WAIT_D2H:
  614. case SCI_REQ_STP_PIO_WAIT_H2D:
  615. case SCI_REQ_STP_PIO_WAIT_FRAME:
  616. case SCI_REQ_STP_PIO_DATA_IN:
  617. case SCI_REQ_STP_PIO_DATA_OUT:
  618. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
  619. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
  620. case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
  621. sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
  622. return SCI_SUCCESS;
  623. case SCI_REQ_TASK_WAIT_TC_RESP:
  624. sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
  625. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  626. return SCI_SUCCESS;
  627. case SCI_REQ_ABORTING:
  628. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  629. return SCI_SUCCESS;
  630. case SCI_REQ_COMPLETED:
  631. default:
  632. dev_warn(scic_to_dev(ireq->owning_controller),
  633. "%s: SCIC IO Request requested to abort while in wrong "
  634. "state %d\n",
  635. __func__,
  636. ireq->sm.current_state_id);
  637. break;
  638. }
  639. return SCI_FAILURE_INVALID_STATE;
  640. }
  641. enum sci_status scic_sds_request_complete(struct isci_request *ireq)
  642. {
  643. enum sci_base_request_states state;
  644. struct scic_sds_controller *scic = ireq->owning_controller;
  645. state = ireq->sm.current_state_id;
  646. if (WARN_ONCE(state != SCI_REQ_COMPLETED,
  647. "isci: request completion from wrong state (%d)\n", state))
  648. return SCI_FAILURE_INVALID_STATE;
  649. if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
  650. scic_sds_controller_release_frame(scic,
  651. ireq->saved_rx_frame_index);
  652. /* XXX can we just stop the machine and remove the 'final' state? */
  653. sci_change_state(&ireq->sm, SCI_REQ_FINAL);
  654. return SCI_SUCCESS;
  655. }
  656. enum sci_status scic_sds_io_request_event_handler(struct isci_request *ireq,
  657. u32 event_code)
  658. {
  659. enum sci_base_request_states state;
  660. struct scic_sds_controller *scic = ireq->owning_controller;
  661. state = ireq->sm.current_state_id;
  662. if (state != SCI_REQ_STP_PIO_DATA_IN) {
  663. dev_warn(scic_to_dev(scic), "%s: (%x) in wrong state %d\n",
  664. __func__, event_code, state);
  665. return SCI_FAILURE_INVALID_STATE;
  666. }
  667. switch (scu_get_event_specifier(event_code)) {
  668. case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
  669. /* We are waiting for data and the SCU has R_ERR the data frame.
  670. * Go back to waiting for the D2H Register FIS
  671. */
  672. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  673. return SCI_SUCCESS;
  674. default:
  675. dev_err(scic_to_dev(scic),
  676. "%s: pio request unexpected event %#x\n",
  677. __func__, event_code);
  678. /* TODO Should we fail the PIO request when we get an
  679. * unexpected event?
  680. */
  681. return SCI_FAILURE;
  682. }
  683. }
  684. /*
  685. * This function copies response data for requests returning response data
  686. * instead of sense data.
  687. * @sci_req: This parameter specifies the request object for which to copy
  688. * the response data.
  689. */
  690. static void scic_sds_io_request_copy_response(struct isci_request *ireq)
  691. {
  692. void *resp_buf;
  693. u32 len;
  694. struct ssp_response_iu *ssp_response;
  695. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  696. ssp_response = &ireq->ssp.rsp;
  697. resp_buf = &isci_tmf->resp.resp_iu;
  698. len = min_t(u32,
  699. SSP_RESP_IU_MAX_SIZE,
  700. be32_to_cpu(ssp_response->response_data_len));
  701. memcpy(resp_buf, ssp_response->resp_data, len);
  702. }
  703. static enum sci_status
  704. request_started_state_tc_event(struct isci_request *ireq,
  705. u32 completion_code)
  706. {
  707. struct ssp_response_iu *resp_iu;
  708. u8 datapres;
  709. /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
  710. * to determine SDMA status
  711. */
  712. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  713. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  714. scic_sds_request_set_status(ireq,
  715. SCU_TASK_DONE_GOOD,
  716. SCI_SUCCESS);
  717. break;
  718. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
  719. /* There are times when the SCU hardware will return an early
  720. * response because the io request specified more data than is
  721. * returned by the target device (mode pages, inquiry data,
  722. * etc.). We must check the response stats to see if this is
  723. * truly a failed request or a good request that just got
  724. * completed early.
  725. */
  726. struct ssp_response_iu *resp = &ireq->ssp.rsp;
  727. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  728. sci_swab32_cpy(&ireq->ssp.rsp,
  729. &ireq->ssp.rsp,
  730. word_cnt);
  731. if (resp->status == 0) {
  732. scic_sds_request_set_status(ireq,
  733. SCU_TASK_DONE_GOOD,
  734. SCI_SUCCESS_IO_DONE_EARLY);
  735. } else {
  736. scic_sds_request_set_status(ireq,
  737. SCU_TASK_DONE_CHECK_RESPONSE,
  738. SCI_FAILURE_IO_RESPONSE_VALID);
  739. }
  740. break;
  741. }
  742. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
  743. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  744. sci_swab32_cpy(&ireq->ssp.rsp,
  745. &ireq->ssp.rsp,
  746. word_cnt);
  747. scic_sds_request_set_status(ireq,
  748. SCU_TASK_DONE_CHECK_RESPONSE,
  749. SCI_FAILURE_IO_RESPONSE_VALID);
  750. break;
  751. }
  752. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
  753. /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
  754. * guaranteed to be received before this completion status is
  755. * posted?
  756. */
  757. resp_iu = &ireq->ssp.rsp;
  758. datapres = resp_iu->datapres;
  759. if (datapres == 1 || datapres == 2) {
  760. scic_sds_request_set_status(ireq,
  761. SCU_TASK_DONE_CHECK_RESPONSE,
  762. SCI_FAILURE_IO_RESPONSE_VALID);
  763. } else
  764. scic_sds_request_set_status(ireq,
  765. SCU_TASK_DONE_GOOD,
  766. SCI_SUCCESS);
  767. break;
  768. /* only stp device gets suspended. */
  769. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  770. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
  771. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
  772. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
  773. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
  774. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
  775. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  776. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
  777. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
  778. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  779. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
  780. if (ireq->protocol == SCIC_STP_PROTOCOL) {
  781. scic_sds_request_set_status(ireq,
  782. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  783. SCU_COMPLETION_TL_STATUS_SHIFT,
  784. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  785. } else {
  786. scic_sds_request_set_status(ireq,
  787. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  788. SCU_COMPLETION_TL_STATUS_SHIFT,
  789. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  790. }
  791. break;
  792. /* both stp/ssp device gets suspended */
  793. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
  794. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
  795. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
  796. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
  797. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
  798. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
  799. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
  800. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
  801. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
  802. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
  803. scic_sds_request_set_status(ireq,
  804. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  805. SCU_COMPLETION_TL_STATUS_SHIFT,
  806. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  807. break;
  808. /* neither ssp nor stp gets suspended. */
  809. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
  810. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
  811. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
  812. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
  813. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
  814. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
  815. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  816. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  817. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  818. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  819. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
  820. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
  821. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
  822. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
  823. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
  824. default:
  825. scic_sds_request_set_status(
  826. ireq,
  827. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  828. SCU_COMPLETION_TL_STATUS_SHIFT,
  829. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  830. break;
  831. }
  832. /*
  833. * TODO: This is probably wrong for ACK/NAK timeout conditions
  834. */
  835. /* In all cases we will treat this as the completion of the IO req. */
  836. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  837. return SCI_SUCCESS;
  838. }
  839. static enum sci_status
  840. request_aborting_state_tc_event(struct isci_request *ireq,
  841. u32 completion_code)
  842. {
  843. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  844. case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
  845. case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
  846. scic_sds_request_set_status(ireq, SCU_TASK_DONE_TASK_ABORT,
  847. SCI_FAILURE_IO_TERMINATED);
  848. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  849. break;
  850. default:
  851. /* Unless we get some strange error wait for the task abort to complete
  852. * TODO: Should there be a state change for this completion?
  853. */
  854. break;
  855. }
  856. return SCI_SUCCESS;
  857. }
  858. static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
  859. u32 completion_code)
  860. {
  861. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  862. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  863. scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
  864. SCI_SUCCESS);
  865. sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
  866. break;
  867. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  868. /* Currently, the decision is to simply allow the task request
  869. * to timeout if the task IU wasn't received successfully.
  870. * There is a potential for receiving multiple task responses if
  871. * we decide to send the task IU again.
  872. */
  873. dev_warn(scic_to_dev(ireq->owning_controller),
  874. "%s: TaskRequest:0x%p CompletionCode:%x - "
  875. "ACK/NAK timeout\n", __func__, ireq,
  876. completion_code);
  877. sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
  878. break;
  879. default:
  880. /*
  881. * All other completion status cause the IO to be complete.
  882. * If a NAK was received, then it is up to the user to retry
  883. * the request.
  884. */
  885. scic_sds_request_set_status(ireq,
  886. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  887. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  888. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  889. break;
  890. }
  891. return SCI_SUCCESS;
  892. }
  893. static enum sci_status
  894. smp_request_await_response_tc_event(struct isci_request *ireq,
  895. u32 completion_code)
  896. {
  897. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  898. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  899. /* In the AWAIT RESPONSE state, any TC completion is
  900. * unexpected. but if the TC has success status, we
  901. * complete the IO anyway.
  902. */
  903. scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
  904. SCI_SUCCESS);
  905. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  906. break;
  907. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  908. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  909. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  910. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  911. /* These status has been seen in a specific LSI
  912. * expander, which sometimes is not able to send smp
  913. * response within 2 ms. This causes our hardware break
  914. * the connection and set TC completion with one of
  915. * these SMP_XXX_XX_ERR status. For these type of error,
  916. * we ask scic user to retry the request.
  917. */
  918. scic_sds_request_set_status(ireq, SCU_TASK_DONE_SMP_RESP_TO_ERR,
  919. SCI_FAILURE_RETRY_REQUIRED);
  920. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  921. break;
  922. default:
  923. /* All other completion status cause the IO to be complete. If a NAK
  924. * was received, then it is up to the user to retry the request
  925. */
  926. scic_sds_request_set_status(ireq,
  927. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  928. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  929. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  930. break;
  931. }
  932. return SCI_SUCCESS;
  933. }
  934. static enum sci_status
  935. smp_request_await_tc_event(struct isci_request *ireq,
  936. u32 completion_code)
  937. {
  938. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  939. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  940. scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
  941. SCI_SUCCESS);
  942. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  943. break;
  944. default:
  945. /* All other completion status cause the IO to be
  946. * complete. If a NAK was received, then it is up to
  947. * the user to retry the request.
  948. */
  949. scic_sds_request_set_status(ireq,
  950. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  951. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  952. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  953. break;
  954. }
  955. return SCI_SUCCESS;
  956. }
  957. void scic_stp_io_request_set_ncq_tag(struct isci_request *ireq,
  958. u16 ncq_tag)
  959. {
  960. /**
  961. * @note This could be made to return an error to the user if the user
  962. * attempts to set the NCQ tag in the wrong state.
  963. */
  964. ireq->tc->type.stp.ncq_tag = ncq_tag;
  965. }
  966. static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
  967. {
  968. struct scu_sgl_element *sgl;
  969. struct scu_sgl_element_pair *sgl_pair;
  970. struct isci_request *ireq = to_ireq(stp_req);
  971. struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
  972. sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
  973. if (!sgl_pair)
  974. sgl = NULL;
  975. else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
  976. if (sgl_pair->B.address_lower == 0 &&
  977. sgl_pair->B.address_upper == 0) {
  978. sgl = NULL;
  979. } else {
  980. pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
  981. sgl = &sgl_pair->B;
  982. }
  983. } else {
  984. if (sgl_pair->next_pair_lower == 0 &&
  985. sgl_pair->next_pair_upper == 0) {
  986. sgl = NULL;
  987. } else {
  988. pio_sgl->index++;
  989. pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
  990. sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
  991. sgl = &sgl_pair->A;
  992. }
  993. }
  994. return sgl;
  995. }
  996. static enum sci_status
  997. stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
  998. u32 completion_code)
  999. {
  1000. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1001. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1002. scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
  1003. SCI_SUCCESS);
  1004. sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
  1005. break;
  1006. default:
  1007. /* All other completion status cause the IO to be
  1008. * complete. If a NAK was received, then it is up to
  1009. * the user to retry the request.
  1010. */
  1011. scic_sds_request_set_status(ireq,
  1012. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1013. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1014. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1015. break;
  1016. }
  1017. return SCI_SUCCESS;
  1018. }
  1019. #define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
  1020. /* transmit DATA_FIS from (current sgl + offset) for input
  1021. * parameter length. current sgl and offset is alreay stored in the IO request
  1022. */
  1023. static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
  1024. struct isci_request *ireq,
  1025. u32 length)
  1026. {
  1027. struct isci_stp_request *stp_req = &ireq->stp.req;
  1028. struct scu_task_context *task_context = ireq->tc;
  1029. struct scu_sgl_element_pair *sgl_pair;
  1030. struct scu_sgl_element *current_sgl;
  1031. /* Recycle the TC and reconstruct it for sending out DATA FIS containing
  1032. * for the data from current_sgl+offset for the input length
  1033. */
  1034. sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
  1035. if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
  1036. current_sgl = &sgl_pair->A;
  1037. else
  1038. current_sgl = &sgl_pair->B;
  1039. /* update the TC */
  1040. task_context->command_iu_upper = current_sgl->address_upper;
  1041. task_context->command_iu_lower = current_sgl->address_lower;
  1042. task_context->transfer_length_bytes = length;
  1043. task_context->type.stp.fis_type = FIS_DATA;
  1044. /* send the new TC out. */
  1045. return scic_controller_continue_io(ireq);
  1046. }
  1047. static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
  1048. {
  1049. struct isci_stp_request *stp_req = &ireq->stp.req;
  1050. struct scu_sgl_element_pair *sgl_pair;
  1051. struct scu_sgl_element *sgl;
  1052. enum sci_status status;
  1053. u32 offset;
  1054. u32 len = 0;
  1055. offset = stp_req->sgl.offset;
  1056. sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
  1057. if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
  1058. return SCI_FAILURE;
  1059. if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
  1060. sgl = &sgl_pair->A;
  1061. len = sgl_pair->A.length - offset;
  1062. } else {
  1063. sgl = &sgl_pair->B;
  1064. len = sgl_pair->B.length - offset;
  1065. }
  1066. if (stp_req->pio_len == 0)
  1067. return SCI_SUCCESS;
  1068. if (stp_req->pio_len >= len) {
  1069. status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
  1070. if (status != SCI_SUCCESS)
  1071. return status;
  1072. stp_req->pio_len -= len;
  1073. /* update the current sgl, offset and save for future */
  1074. sgl = pio_sgl_next(stp_req);
  1075. offset = 0;
  1076. } else if (stp_req->pio_len < len) {
  1077. scic_sds_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
  1078. /* Sgl offset will be adjusted and saved for future */
  1079. offset += stp_req->pio_len;
  1080. sgl->address_lower += stp_req->pio_len;
  1081. stp_req->pio_len = 0;
  1082. }
  1083. stp_req->sgl.offset = offset;
  1084. return status;
  1085. }
  1086. /**
  1087. *
  1088. * @stp_request: The request that is used for the SGL processing.
  1089. * @data_buffer: The buffer of data to be copied.
  1090. * @length: The length of the data transfer.
  1091. *
  1092. * Copy the data from the buffer for the length specified to the IO reqeust SGL
  1093. * specified data region. enum sci_status
  1094. */
  1095. static enum sci_status
  1096. scic_sds_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
  1097. u8 *data_buf, u32 len)
  1098. {
  1099. struct isci_request *ireq;
  1100. u8 *src_addr;
  1101. int copy_len;
  1102. struct sas_task *task;
  1103. struct scatterlist *sg;
  1104. void *kaddr;
  1105. int total_len = len;
  1106. ireq = to_ireq(stp_req);
  1107. task = isci_request_access_task(ireq);
  1108. src_addr = data_buf;
  1109. if (task->num_scatter > 0) {
  1110. sg = task->scatter;
  1111. while (total_len > 0) {
  1112. struct page *page = sg_page(sg);
  1113. copy_len = min_t(int, total_len, sg_dma_len(sg));
  1114. kaddr = kmap_atomic(page, KM_IRQ0);
  1115. memcpy(kaddr + sg->offset, src_addr, copy_len);
  1116. kunmap_atomic(kaddr, KM_IRQ0);
  1117. total_len -= copy_len;
  1118. src_addr += copy_len;
  1119. sg = sg_next(sg);
  1120. }
  1121. } else {
  1122. BUG_ON(task->total_xfer_len < total_len);
  1123. memcpy(task->scatter, src_addr, total_len);
  1124. }
  1125. return SCI_SUCCESS;
  1126. }
  1127. /**
  1128. *
  1129. * @sci_req: The PIO DATA IN request that is to receive the data.
  1130. * @data_buffer: The buffer to copy from.
  1131. *
  1132. * Copy the data buffer to the io request data region. enum sci_status
  1133. */
  1134. static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
  1135. struct isci_stp_request *stp_req,
  1136. u8 *data_buffer)
  1137. {
  1138. enum sci_status status;
  1139. /*
  1140. * If there is less than 1K remaining in the transfer request
  1141. * copy just the data for the transfer */
  1142. if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
  1143. status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
  1144. stp_req, data_buffer, stp_req->pio_len);
  1145. if (status == SCI_SUCCESS)
  1146. stp_req->pio_len = 0;
  1147. } else {
  1148. /* We are transfering the whole frame so copy */
  1149. status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
  1150. stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
  1151. if (status == SCI_SUCCESS)
  1152. stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
  1153. }
  1154. return status;
  1155. }
  1156. static enum sci_status
  1157. stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
  1158. u32 completion_code)
  1159. {
  1160. enum sci_status status = SCI_SUCCESS;
  1161. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1162. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1163. scic_sds_request_set_status(ireq,
  1164. SCU_TASK_DONE_GOOD,
  1165. SCI_SUCCESS);
  1166. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1167. break;
  1168. default:
  1169. /* All other completion status cause the IO to be
  1170. * complete. If a NAK was received, then it is up to
  1171. * the user to retry the request.
  1172. */
  1173. scic_sds_request_set_status(ireq,
  1174. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1175. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1176. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1177. break;
  1178. }
  1179. return status;
  1180. }
  1181. static enum sci_status
  1182. pio_data_out_tx_done_tc_event(struct isci_request *ireq,
  1183. u32 completion_code)
  1184. {
  1185. enum sci_status status = SCI_SUCCESS;
  1186. bool all_frames_transferred = false;
  1187. struct isci_stp_request *stp_req = &ireq->stp.req;
  1188. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1189. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1190. /* Transmit data */
  1191. if (stp_req->pio_len != 0) {
  1192. status = scic_sds_stp_request_pio_data_out_transmit_data(ireq);
  1193. if (status == SCI_SUCCESS) {
  1194. if (stp_req->pio_len == 0)
  1195. all_frames_transferred = true;
  1196. }
  1197. } else if (stp_req->pio_len == 0) {
  1198. /*
  1199. * this will happen if the all data is written at the
  1200. * first time after the pio setup fis is received
  1201. */
  1202. all_frames_transferred = true;
  1203. }
  1204. /* all data transferred. */
  1205. if (all_frames_transferred) {
  1206. /*
  1207. * Change the state to SCI_REQ_STP_PIO_DATA_IN
  1208. * and wait for PIO_SETUP fis / or D2H REg fis. */
  1209. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1210. }
  1211. break;
  1212. default:
  1213. /*
  1214. * All other completion status cause the IO to be complete.
  1215. * If a NAK was received, then it is up to the user to retry
  1216. * the request.
  1217. */
  1218. scic_sds_request_set_status(
  1219. ireq,
  1220. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1221. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1222. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1223. break;
  1224. }
  1225. return status;
  1226. }
  1227. static void scic_sds_stp_request_udma_complete_request(
  1228. struct isci_request *ireq,
  1229. u32 scu_status,
  1230. enum sci_status sci_status)
  1231. {
  1232. scic_sds_request_set_status(ireq, scu_status, sci_status);
  1233. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1234. }
  1235. static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct isci_request *ireq,
  1236. u32 frame_index)
  1237. {
  1238. struct scic_sds_controller *scic = ireq->owning_controller;
  1239. struct dev_to_host_fis *frame_header;
  1240. enum sci_status status;
  1241. u32 *frame_buffer;
  1242. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1243. frame_index,
  1244. (void **)&frame_header);
  1245. if ((status == SCI_SUCCESS) &&
  1246. (frame_header->fis_type == FIS_REGD2H)) {
  1247. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1248. frame_index,
  1249. (void **)&frame_buffer);
  1250. scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
  1251. frame_header,
  1252. frame_buffer);
  1253. }
  1254. scic_sds_controller_release_frame(scic, frame_index);
  1255. return status;
  1256. }
  1257. enum sci_status
  1258. scic_sds_io_request_frame_handler(struct isci_request *ireq,
  1259. u32 frame_index)
  1260. {
  1261. struct scic_sds_controller *scic = ireq->owning_controller;
  1262. struct isci_stp_request *stp_req = &ireq->stp.req;
  1263. enum sci_base_request_states state;
  1264. enum sci_status status;
  1265. ssize_t word_cnt;
  1266. state = ireq->sm.current_state_id;
  1267. switch (state) {
  1268. case SCI_REQ_STARTED: {
  1269. struct ssp_frame_hdr ssp_hdr;
  1270. void *frame_header;
  1271. scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1272. frame_index,
  1273. &frame_header);
  1274. word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
  1275. sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
  1276. if (ssp_hdr.frame_type == SSP_RESPONSE) {
  1277. struct ssp_response_iu *resp_iu;
  1278. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  1279. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1280. frame_index,
  1281. (void **)&resp_iu);
  1282. sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
  1283. resp_iu = &ireq->ssp.rsp;
  1284. if (resp_iu->datapres == 0x01 ||
  1285. resp_iu->datapres == 0x02) {
  1286. scic_sds_request_set_status(ireq,
  1287. SCU_TASK_DONE_CHECK_RESPONSE,
  1288. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1289. } else
  1290. scic_sds_request_set_status(ireq,
  1291. SCU_TASK_DONE_GOOD,
  1292. SCI_SUCCESS);
  1293. } else {
  1294. /* not a response frame, why did it get forwarded? */
  1295. dev_err(scic_to_dev(scic),
  1296. "%s: SCIC IO Request 0x%p received unexpected "
  1297. "frame %d type 0x%02x\n", __func__, ireq,
  1298. frame_index, ssp_hdr.frame_type);
  1299. }
  1300. /*
  1301. * In any case we are done with this frame buffer return it to
  1302. * the controller
  1303. */
  1304. scic_sds_controller_release_frame(scic, frame_index);
  1305. return SCI_SUCCESS;
  1306. }
  1307. case SCI_REQ_TASK_WAIT_TC_RESP:
  1308. scic_sds_io_request_copy_response(ireq);
  1309. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1310. scic_sds_controller_release_frame(scic,frame_index);
  1311. return SCI_SUCCESS;
  1312. case SCI_REQ_SMP_WAIT_RESP: {
  1313. struct smp_resp *rsp_hdr = &ireq->smp.rsp;
  1314. void *frame_header;
  1315. scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1316. frame_index,
  1317. &frame_header);
  1318. /* byte swap the header. */
  1319. word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
  1320. sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
  1321. if (rsp_hdr->frame_type == SMP_RESPONSE) {
  1322. void *smp_resp;
  1323. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1324. frame_index,
  1325. &smp_resp);
  1326. word_cnt = (sizeof(struct smp_resp) - SMP_RESP_HDR_SZ) /
  1327. sizeof(u32);
  1328. sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
  1329. smp_resp, word_cnt);
  1330. scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
  1331. SCI_SUCCESS);
  1332. sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
  1333. } else {
  1334. /*
  1335. * This was not a response frame why did it get
  1336. * forwarded?
  1337. */
  1338. dev_err(scic_to_dev(scic),
  1339. "%s: SCIC SMP Request 0x%p received unexpected "
  1340. "frame %d type 0x%02x\n",
  1341. __func__,
  1342. ireq,
  1343. frame_index,
  1344. rsp_hdr->frame_type);
  1345. scic_sds_request_set_status(ireq,
  1346. SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
  1347. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1348. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1349. }
  1350. scic_sds_controller_release_frame(scic, frame_index);
  1351. return SCI_SUCCESS;
  1352. }
  1353. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  1354. return scic_sds_stp_request_udma_general_frame_handler(ireq,
  1355. frame_index);
  1356. case SCI_REQ_STP_UDMA_WAIT_D2H:
  1357. /* Use the general frame handler to copy the resposne data */
  1358. status = scic_sds_stp_request_udma_general_frame_handler(ireq,
  1359. frame_index);
  1360. if (status != SCI_SUCCESS)
  1361. return status;
  1362. scic_sds_stp_request_udma_complete_request(ireq,
  1363. SCU_TASK_DONE_CHECK_RESPONSE,
  1364. SCI_FAILURE_IO_RESPONSE_VALID);
  1365. return SCI_SUCCESS;
  1366. case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
  1367. struct dev_to_host_fis *frame_header;
  1368. u32 *frame_buffer;
  1369. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1370. frame_index,
  1371. (void **)&frame_header);
  1372. if (status != SCI_SUCCESS) {
  1373. dev_err(scic_to_dev(scic),
  1374. "%s: SCIC IO Request 0x%p could not get frame "
  1375. "header for frame index %d, status %x\n",
  1376. __func__,
  1377. stp_req,
  1378. frame_index,
  1379. status);
  1380. return status;
  1381. }
  1382. switch (frame_header->fis_type) {
  1383. case FIS_REGD2H:
  1384. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1385. frame_index,
  1386. (void **)&frame_buffer);
  1387. scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
  1388. frame_header,
  1389. frame_buffer);
  1390. /* The command has completed with error */
  1391. scic_sds_request_set_status(ireq, SCU_TASK_DONE_CHECK_RESPONSE,
  1392. SCI_FAILURE_IO_RESPONSE_VALID);
  1393. break;
  1394. default:
  1395. dev_warn(scic_to_dev(scic),
  1396. "%s: IO Request:0x%p Frame Id:%d protocol "
  1397. "violation occurred\n", __func__, stp_req,
  1398. frame_index);
  1399. scic_sds_request_set_status(ireq, SCU_TASK_DONE_UNEXP_FIS,
  1400. SCI_FAILURE_PROTOCOL_VIOLATION);
  1401. break;
  1402. }
  1403. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1404. /* Frame has been decoded return it to the controller */
  1405. scic_sds_controller_release_frame(scic, frame_index);
  1406. return status;
  1407. }
  1408. case SCI_REQ_STP_PIO_WAIT_FRAME: {
  1409. struct sas_task *task = isci_request_access_task(ireq);
  1410. struct dev_to_host_fis *frame_header;
  1411. u32 *frame_buffer;
  1412. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1413. frame_index,
  1414. (void **)&frame_header);
  1415. if (status != SCI_SUCCESS) {
  1416. dev_err(scic_to_dev(scic),
  1417. "%s: SCIC IO Request 0x%p could not get frame "
  1418. "header for frame index %d, status %x\n",
  1419. __func__, stp_req, frame_index, status);
  1420. return status;
  1421. }
  1422. switch (frame_header->fis_type) {
  1423. case FIS_PIO_SETUP:
  1424. /* Get from the frame buffer the PIO Setup Data */
  1425. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1426. frame_index,
  1427. (void **)&frame_buffer);
  1428. /* Get the data from the PIO Setup The SCU Hardware
  1429. * returns first word in the frame_header and the rest
  1430. * of the data is in the frame buffer so we need to
  1431. * back up one dword
  1432. */
  1433. /* transfer_count: first 16bits in the 4th dword */
  1434. stp_req->pio_len = frame_buffer[3] & 0xffff;
  1435. /* status: 4th byte in the 3rd dword */
  1436. stp_req->status = (frame_buffer[2] >> 24) & 0xff;
  1437. scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
  1438. frame_header,
  1439. frame_buffer);
  1440. ireq->stp.rsp.status = stp_req->status;
  1441. /* The next state is dependent on whether the
  1442. * request was PIO Data-in or Data out
  1443. */
  1444. if (task->data_dir == DMA_FROM_DEVICE) {
  1445. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
  1446. } else if (task->data_dir == DMA_TO_DEVICE) {
  1447. /* Transmit data */
  1448. status = scic_sds_stp_request_pio_data_out_transmit_data(ireq);
  1449. if (status != SCI_SUCCESS)
  1450. break;
  1451. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
  1452. }
  1453. break;
  1454. case FIS_SETDEVBITS:
  1455. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1456. break;
  1457. case FIS_REGD2H:
  1458. if (frame_header->status & ATA_BUSY) {
  1459. /*
  1460. * Now why is the drive sending a D2H Register
  1461. * FIS when it is still busy? Do nothing since
  1462. * we are still in the right state.
  1463. */
  1464. dev_dbg(scic_to_dev(scic),
  1465. "%s: SCIC PIO Request 0x%p received "
  1466. "D2H Register FIS with BSY status "
  1467. "0x%x\n",
  1468. __func__,
  1469. stp_req,
  1470. frame_header->status);
  1471. break;
  1472. }
  1473. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1474. frame_index,
  1475. (void **)&frame_buffer);
  1476. scic_sds_controller_copy_sata_response(&ireq->stp.req,
  1477. frame_header,
  1478. frame_buffer);
  1479. scic_sds_request_set_status(ireq,
  1480. SCU_TASK_DONE_CHECK_RESPONSE,
  1481. SCI_FAILURE_IO_RESPONSE_VALID);
  1482. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1483. break;
  1484. default:
  1485. /* FIXME: what do we do here? */
  1486. break;
  1487. }
  1488. /* Frame is decoded return it to the controller */
  1489. scic_sds_controller_release_frame(scic, frame_index);
  1490. return status;
  1491. }
  1492. case SCI_REQ_STP_PIO_DATA_IN: {
  1493. struct dev_to_host_fis *frame_header;
  1494. struct sata_fis_data *frame_buffer;
  1495. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1496. frame_index,
  1497. (void **)&frame_header);
  1498. if (status != SCI_SUCCESS) {
  1499. dev_err(scic_to_dev(scic),
  1500. "%s: SCIC IO Request 0x%p could not get frame "
  1501. "header for frame index %d, status %x\n",
  1502. __func__,
  1503. stp_req,
  1504. frame_index,
  1505. status);
  1506. return status;
  1507. }
  1508. if (frame_header->fis_type != FIS_DATA) {
  1509. dev_err(scic_to_dev(scic),
  1510. "%s: SCIC PIO Request 0x%p received frame %d "
  1511. "with fis type 0x%02x when expecting a data "
  1512. "fis.\n",
  1513. __func__,
  1514. stp_req,
  1515. frame_index,
  1516. frame_header->fis_type);
  1517. scic_sds_request_set_status(ireq,
  1518. SCU_TASK_DONE_GOOD,
  1519. SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
  1520. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1521. /* Frame is decoded return it to the controller */
  1522. scic_sds_controller_release_frame(scic, frame_index);
  1523. return status;
  1524. }
  1525. if (stp_req->sgl.index < 0) {
  1526. ireq->saved_rx_frame_index = frame_index;
  1527. stp_req->pio_len = 0;
  1528. } else {
  1529. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1530. frame_index,
  1531. (void **)&frame_buffer);
  1532. status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
  1533. (u8 *)frame_buffer);
  1534. /* Frame is decoded return it to the controller */
  1535. scic_sds_controller_release_frame(scic, frame_index);
  1536. }
  1537. /* Check for the end of the transfer, are there more
  1538. * bytes remaining for this data transfer
  1539. */
  1540. if (status != SCI_SUCCESS || stp_req->pio_len != 0)
  1541. return status;
  1542. if ((stp_req->status & ATA_BUSY) == 0) {
  1543. scic_sds_request_set_status(ireq,
  1544. SCU_TASK_DONE_CHECK_RESPONSE,
  1545. SCI_FAILURE_IO_RESPONSE_VALID);
  1546. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1547. } else {
  1548. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1549. }
  1550. return status;
  1551. }
  1552. case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
  1553. struct dev_to_host_fis *frame_header;
  1554. u32 *frame_buffer;
  1555. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1556. frame_index,
  1557. (void **)&frame_header);
  1558. if (status != SCI_SUCCESS) {
  1559. dev_err(scic_to_dev(scic),
  1560. "%s: SCIC IO Request 0x%p could not get frame "
  1561. "header for frame index %d, status %x\n",
  1562. __func__,
  1563. stp_req,
  1564. frame_index,
  1565. status);
  1566. return status;
  1567. }
  1568. switch (frame_header->fis_type) {
  1569. case FIS_REGD2H:
  1570. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1571. frame_index,
  1572. (void **)&frame_buffer);
  1573. scic_sds_controller_copy_sata_response(&ireq->stp.rsp,
  1574. frame_header,
  1575. frame_buffer);
  1576. /* The command has completed with error */
  1577. scic_sds_request_set_status(ireq,
  1578. SCU_TASK_DONE_CHECK_RESPONSE,
  1579. SCI_FAILURE_IO_RESPONSE_VALID);
  1580. break;
  1581. default:
  1582. dev_warn(scic_to_dev(scic),
  1583. "%s: IO Request:0x%p Frame Id:%d protocol "
  1584. "violation occurred\n",
  1585. __func__,
  1586. stp_req,
  1587. frame_index);
  1588. scic_sds_request_set_status(ireq,
  1589. SCU_TASK_DONE_UNEXP_FIS,
  1590. SCI_FAILURE_PROTOCOL_VIOLATION);
  1591. break;
  1592. }
  1593. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1594. /* Frame has been decoded return it to the controller */
  1595. scic_sds_controller_release_frame(scic, frame_index);
  1596. return status;
  1597. }
  1598. case SCI_REQ_ABORTING:
  1599. /*
  1600. * TODO: Is it even possible to get an unsolicited frame in the
  1601. * aborting state?
  1602. */
  1603. scic_sds_controller_release_frame(scic, frame_index);
  1604. return SCI_SUCCESS;
  1605. default:
  1606. dev_warn(scic_to_dev(scic),
  1607. "%s: SCIC IO Request given unexpected frame %x while "
  1608. "in state %d\n",
  1609. __func__,
  1610. frame_index,
  1611. state);
  1612. scic_sds_controller_release_frame(scic, frame_index);
  1613. return SCI_FAILURE_INVALID_STATE;
  1614. }
  1615. }
  1616. static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
  1617. u32 completion_code)
  1618. {
  1619. enum sci_status status = SCI_SUCCESS;
  1620. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1621. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1622. scic_sds_stp_request_udma_complete_request(ireq,
  1623. SCU_TASK_DONE_GOOD,
  1624. SCI_SUCCESS);
  1625. break;
  1626. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
  1627. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  1628. /* We must check ther response buffer to see if the D2H
  1629. * Register FIS was received before we got the TC
  1630. * completion.
  1631. */
  1632. if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
  1633. scic_sds_remote_device_suspend(ireq->target_device,
  1634. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1635. scic_sds_stp_request_udma_complete_request(ireq,
  1636. SCU_TASK_DONE_CHECK_RESPONSE,
  1637. SCI_FAILURE_IO_RESPONSE_VALID);
  1638. } else {
  1639. /* If we have an error completion status for the
  1640. * TC then we can expect a D2H register FIS from
  1641. * the device so we must change state to wait
  1642. * for it
  1643. */
  1644. sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
  1645. }
  1646. break;
  1647. /* TODO Check to see if any of these completion status need to
  1648. * wait for the device to host register fis.
  1649. */
  1650. /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
  1651. * - this comes only for B0
  1652. */
  1653. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
  1654. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  1655. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
  1656. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
  1657. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
  1658. scic_sds_remote_device_suspend(ireq->target_device,
  1659. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1660. /* Fall through to the default case */
  1661. default:
  1662. /* All other completion status cause the IO to be complete. */
  1663. scic_sds_stp_request_udma_complete_request(ireq,
  1664. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1665. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1666. break;
  1667. }
  1668. return status;
  1669. }
  1670. static enum sci_status
  1671. stp_request_soft_reset_await_h2d_asserted_tc_event(struct isci_request *ireq,
  1672. u32 completion_code)
  1673. {
  1674. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1675. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1676. scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
  1677. SCI_SUCCESS);
  1678. sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
  1679. break;
  1680. default:
  1681. /*
  1682. * All other completion status cause the IO to be complete.
  1683. * If a NAK was received, then it is up to the user to retry
  1684. * the request.
  1685. */
  1686. scic_sds_request_set_status(ireq,
  1687. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1688. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1689. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1690. break;
  1691. }
  1692. return SCI_SUCCESS;
  1693. }
  1694. static enum sci_status
  1695. stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct isci_request *ireq,
  1696. u32 completion_code)
  1697. {
  1698. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1699. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1700. scic_sds_request_set_status(ireq, SCU_TASK_DONE_GOOD,
  1701. SCI_SUCCESS);
  1702. sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
  1703. break;
  1704. default:
  1705. /* All other completion status cause the IO to be complete. If
  1706. * a NAK was received, then it is up to the user to retry the
  1707. * request.
  1708. */
  1709. scic_sds_request_set_status(ireq,
  1710. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1711. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1712. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1713. break;
  1714. }
  1715. return SCI_SUCCESS;
  1716. }
  1717. enum sci_status
  1718. scic_sds_io_request_tc_completion(struct isci_request *ireq,
  1719. u32 completion_code)
  1720. {
  1721. enum sci_base_request_states state;
  1722. struct scic_sds_controller *scic = ireq->owning_controller;
  1723. state = ireq->sm.current_state_id;
  1724. switch (state) {
  1725. case SCI_REQ_STARTED:
  1726. return request_started_state_tc_event(ireq, completion_code);
  1727. case SCI_REQ_TASK_WAIT_TC_COMP:
  1728. return ssp_task_request_await_tc_event(ireq,
  1729. completion_code);
  1730. case SCI_REQ_SMP_WAIT_RESP:
  1731. return smp_request_await_response_tc_event(ireq,
  1732. completion_code);
  1733. case SCI_REQ_SMP_WAIT_TC_COMP:
  1734. return smp_request_await_tc_event(ireq, completion_code);
  1735. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  1736. return stp_request_udma_await_tc_event(ireq,
  1737. completion_code);
  1738. case SCI_REQ_STP_NON_DATA_WAIT_H2D:
  1739. return stp_request_non_data_await_h2d_tc_event(ireq,
  1740. completion_code);
  1741. case SCI_REQ_STP_PIO_WAIT_H2D:
  1742. return stp_request_pio_await_h2d_completion_tc_event(ireq,
  1743. completion_code);
  1744. case SCI_REQ_STP_PIO_DATA_OUT:
  1745. return pio_data_out_tx_done_tc_event(ireq, completion_code);
  1746. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
  1747. return stp_request_soft_reset_await_h2d_asserted_tc_event(ireq,
  1748. completion_code);
  1749. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
  1750. return stp_request_soft_reset_await_h2d_diagnostic_tc_event(ireq,
  1751. completion_code);
  1752. case SCI_REQ_ABORTING:
  1753. return request_aborting_state_tc_event(ireq,
  1754. completion_code);
  1755. default:
  1756. dev_warn(scic_to_dev(scic),
  1757. "%s: SCIC IO Request given task completion "
  1758. "notification %x while in wrong state %d\n",
  1759. __func__,
  1760. completion_code,
  1761. state);
  1762. return SCI_FAILURE_INVALID_STATE;
  1763. }
  1764. }
  1765. /**
  1766. * isci_request_process_response_iu() - This function sets the status and
  1767. * response iu, in the task struct, from the request object for the upper
  1768. * layer driver.
  1769. * @sas_task: This parameter is the task struct from the upper layer driver.
  1770. * @resp_iu: This parameter points to the response iu of the completed request.
  1771. * @dev: This parameter specifies the linux device struct.
  1772. *
  1773. * none.
  1774. */
  1775. static void isci_request_process_response_iu(
  1776. struct sas_task *task,
  1777. struct ssp_response_iu *resp_iu,
  1778. struct device *dev)
  1779. {
  1780. dev_dbg(dev,
  1781. "%s: resp_iu = %p "
  1782. "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
  1783. "resp_iu->response_data_len = %x, "
  1784. "resp_iu->sense_data_len = %x\nrepsonse data: ",
  1785. __func__,
  1786. resp_iu,
  1787. resp_iu->status,
  1788. resp_iu->datapres,
  1789. resp_iu->response_data_len,
  1790. resp_iu->sense_data_len);
  1791. task->task_status.stat = resp_iu->status;
  1792. /* libsas updates the task status fields based on the response iu. */
  1793. sas_ssp_task_response(dev, task, resp_iu);
  1794. }
  1795. /**
  1796. * isci_request_set_open_reject_status() - This function prepares the I/O
  1797. * completion for OPEN_REJECT conditions.
  1798. * @request: This parameter is the completed isci_request object.
  1799. * @response_ptr: This parameter specifies the service response for the I/O.
  1800. * @status_ptr: This parameter specifies the exec status for the I/O.
  1801. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  1802. * the LLDD with respect to completing this request or forcing an abort
  1803. * condition on the I/O.
  1804. * @open_rej_reason: This parameter specifies the encoded reason for the
  1805. * abandon-class reject.
  1806. *
  1807. * none.
  1808. */
  1809. static void isci_request_set_open_reject_status(
  1810. struct isci_request *request,
  1811. struct sas_task *task,
  1812. enum service_response *response_ptr,
  1813. enum exec_status *status_ptr,
  1814. enum isci_completion_selection *complete_to_host_ptr,
  1815. enum sas_open_rej_reason open_rej_reason)
  1816. {
  1817. /* Task in the target is done. */
  1818. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  1819. *response_ptr = SAS_TASK_UNDELIVERED;
  1820. *status_ptr = SAS_OPEN_REJECT;
  1821. *complete_to_host_ptr = isci_perform_normal_io_completion;
  1822. task->task_status.open_rej_reason = open_rej_reason;
  1823. }
  1824. /**
  1825. * isci_request_handle_controller_specific_errors() - This function decodes
  1826. * controller-specific I/O completion error conditions.
  1827. * @request: This parameter is the completed isci_request object.
  1828. * @response_ptr: This parameter specifies the service response for the I/O.
  1829. * @status_ptr: This parameter specifies the exec status for the I/O.
  1830. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  1831. * the LLDD with respect to completing this request or forcing an abort
  1832. * condition on the I/O.
  1833. *
  1834. * none.
  1835. */
  1836. static void isci_request_handle_controller_specific_errors(
  1837. struct isci_remote_device *idev,
  1838. struct isci_request *request,
  1839. struct sas_task *task,
  1840. enum service_response *response_ptr,
  1841. enum exec_status *status_ptr,
  1842. enum isci_completion_selection *complete_to_host_ptr)
  1843. {
  1844. unsigned int cstatus;
  1845. cstatus = request->scu_status;
  1846. dev_dbg(&request->isci_host->pdev->dev,
  1847. "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
  1848. "- controller status = 0x%x\n",
  1849. __func__, request, cstatus);
  1850. /* Decode the controller-specific errors; most
  1851. * important is to recognize those conditions in which
  1852. * the target may still have a task outstanding that
  1853. * must be aborted.
  1854. *
  1855. * Note that there are SCU completion codes being
  1856. * named in the decode below for which SCIC has already
  1857. * done work to handle them in a way other than as
  1858. * a controller-specific completion code; these are left
  1859. * in the decode below for completeness sake.
  1860. */
  1861. switch (cstatus) {
  1862. case SCU_TASK_DONE_DMASETUP_DIRERR:
  1863. /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
  1864. case SCU_TASK_DONE_XFERCNT_ERR:
  1865. /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
  1866. if (task->task_proto == SAS_PROTOCOL_SMP) {
  1867. /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
  1868. *response_ptr = SAS_TASK_COMPLETE;
  1869. /* See if the device has been/is being stopped. Note
  1870. * that we ignore the quiesce state, since we are
  1871. * concerned about the actual device state.
  1872. */
  1873. if (!idev)
  1874. *status_ptr = SAS_DEVICE_UNKNOWN;
  1875. else
  1876. *status_ptr = SAS_ABORTED_TASK;
  1877. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  1878. *complete_to_host_ptr =
  1879. isci_perform_normal_io_completion;
  1880. } else {
  1881. /* Task in the target is not done. */
  1882. *response_ptr = SAS_TASK_UNDELIVERED;
  1883. if (!idev)
  1884. *status_ptr = SAS_DEVICE_UNKNOWN;
  1885. else
  1886. *status_ptr = SAM_STAT_TASK_ABORTED;
  1887. clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  1888. *complete_to_host_ptr =
  1889. isci_perform_error_io_completion;
  1890. }
  1891. break;
  1892. case SCU_TASK_DONE_CRC_ERR:
  1893. case SCU_TASK_DONE_NAK_CMD_ERR:
  1894. case SCU_TASK_DONE_EXCESS_DATA:
  1895. case SCU_TASK_DONE_UNEXP_FIS:
  1896. /* Also SCU_TASK_DONE_UNEXP_RESP: */
  1897. case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
  1898. case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
  1899. case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
  1900. /* These are conditions in which the target
  1901. * has completed the task, so that no cleanup
  1902. * is necessary.
  1903. */
  1904. *response_ptr = SAS_TASK_COMPLETE;
  1905. /* See if the device has been/is being stopped. Note
  1906. * that we ignore the quiesce state, since we are
  1907. * concerned about the actual device state.
  1908. */
  1909. if (!idev)
  1910. *status_ptr = SAS_DEVICE_UNKNOWN;
  1911. else
  1912. *status_ptr = SAS_ABORTED_TASK;
  1913. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  1914. *complete_to_host_ptr = isci_perform_normal_io_completion;
  1915. break;
  1916. /* Note that the only open reject completion codes seen here will be
  1917. * abandon-class codes; all others are automatically retried in the SCU.
  1918. */
  1919. case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
  1920. isci_request_set_open_reject_status(
  1921. request, task, response_ptr, status_ptr,
  1922. complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
  1923. break;
  1924. case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
  1925. /* Note - the return of AB0 will change when
  1926. * libsas implements detection of zone violations.
  1927. */
  1928. isci_request_set_open_reject_status(
  1929. request, task, response_ptr, status_ptr,
  1930. complete_to_host_ptr, SAS_OREJ_RESV_AB0);
  1931. break;
  1932. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
  1933. isci_request_set_open_reject_status(
  1934. request, task, response_ptr, status_ptr,
  1935. complete_to_host_ptr, SAS_OREJ_RESV_AB1);
  1936. break;
  1937. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
  1938. isci_request_set_open_reject_status(
  1939. request, task, response_ptr, status_ptr,
  1940. complete_to_host_ptr, SAS_OREJ_RESV_AB2);
  1941. break;
  1942. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
  1943. isci_request_set_open_reject_status(
  1944. request, task, response_ptr, status_ptr,
  1945. complete_to_host_ptr, SAS_OREJ_RESV_AB3);
  1946. break;
  1947. case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
  1948. isci_request_set_open_reject_status(
  1949. request, task, response_ptr, status_ptr,
  1950. complete_to_host_ptr, SAS_OREJ_BAD_DEST);
  1951. break;
  1952. case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
  1953. isci_request_set_open_reject_status(
  1954. request, task, response_ptr, status_ptr,
  1955. complete_to_host_ptr, SAS_OREJ_STP_NORES);
  1956. break;
  1957. case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
  1958. isci_request_set_open_reject_status(
  1959. request, task, response_ptr, status_ptr,
  1960. complete_to_host_ptr, SAS_OREJ_EPROTO);
  1961. break;
  1962. case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
  1963. isci_request_set_open_reject_status(
  1964. request, task, response_ptr, status_ptr,
  1965. complete_to_host_ptr, SAS_OREJ_CONN_RATE);
  1966. break;
  1967. case SCU_TASK_DONE_LL_R_ERR:
  1968. /* Also SCU_TASK_DONE_ACK_NAK_TO: */
  1969. case SCU_TASK_DONE_LL_PERR:
  1970. case SCU_TASK_DONE_LL_SY_TERM:
  1971. /* Also SCU_TASK_DONE_NAK_ERR:*/
  1972. case SCU_TASK_DONE_LL_LF_TERM:
  1973. /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
  1974. case SCU_TASK_DONE_LL_ABORT_ERR:
  1975. case SCU_TASK_DONE_SEQ_INV_TYPE:
  1976. /* Also SCU_TASK_DONE_UNEXP_XR: */
  1977. case SCU_TASK_DONE_XR_IU_LEN_ERR:
  1978. case SCU_TASK_DONE_INV_FIS_LEN:
  1979. /* Also SCU_TASK_DONE_XR_WD_LEN: */
  1980. case SCU_TASK_DONE_SDMA_ERR:
  1981. case SCU_TASK_DONE_OFFSET_ERR:
  1982. case SCU_TASK_DONE_MAX_PLD_ERR:
  1983. case SCU_TASK_DONE_LF_ERR:
  1984. case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
  1985. case SCU_TASK_DONE_SMP_LL_RX_ERR:
  1986. case SCU_TASK_DONE_UNEXP_DATA:
  1987. case SCU_TASK_DONE_UNEXP_SDBFIS:
  1988. case SCU_TASK_DONE_REG_ERR:
  1989. case SCU_TASK_DONE_SDB_ERR:
  1990. case SCU_TASK_DONE_TASK_ABORT:
  1991. default:
  1992. /* Task in the target is not done. */
  1993. *response_ptr = SAS_TASK_UNDELIVERED;
  1994. *status_ptr = SAM_STAT_TASK_ABORTED;
  1995. if (task->task_proto == SAS_PROTOCOL_SMP) {
  1996. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  1997. *complete_to_host_ptr = isci_perform_normal_io_completion;
  1998. } else {
  1999. clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2000. *complete_to_host_ptr = isci_perform_error_io_completion;
  2001. }
  2002. break;
  2003. }
  2004. }
  2005. /**
  2006. * isci_task_save_for_upper_layer_completion() - This function saves the
  2007. * request for later completion to the upper layer driver.
  2008. * @host: This parameter is a pointer to the host on which the the request
  2009. * should be queued (either as an error or success).
  2010. * @request: This parameter is the completed request.
  2011. * @response: This parameter is the response code for the completed task.
  2012. * @status: This parameter is the status code for the completed task.
  2013. *
  2014. * none.
  2015. */
  2016. static void isci_task_save_for_upper_layer_completion(
  2017. struct isci_host *host,
  2018. struct isci_request *request,
  2019. enum service_response response,
  2020. enum exec_status status,
  2021. enum isci_completion_selection task_notification_selection)
  2022. {
  2023. struct sas_task *task = isci_request_access_task(request);
  2024. task_notification_selection
  2025. = isci_task_set_completion_status(task, response, status,
  2026. task_notification_selection);
  2027. /* Tasks aborted specifically by a call to the lldd_abort_task
  2028. * function should not be completed to the host in the regular path.
  2029. */
  2030. switch (task_notification_selection) {
  2031. case isci_perform_normal_io_completion:
  2032. /* Normal notification (task_done) */
  2033. dev_dbg(&host->pdev->dev,
  2034. "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
  2035. __func__,
  2036. task,
  2037. task->task_status.resp, response,
  2038. task->task_status.stat, status);
  2039. /* Add to the completed list. */
  2040. list_add(&request->completed_node,
  2041. &host->requests_to_complete);
  2042. /* Take the request off the device's pending request list. */
  2043. list_del_init(&request->dev_node);
  2044. break;
  2045. case isci_perform_aborted_io_completion:
  2046. /* No notification to libsas because this request is
  2047. * already in the abort path.
  2048. */
  2049. dev_warn(&host->pdev->dev,
  2050. "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
  2051. __func__,
  2052. task,
  2053. task->task_status.resp, response,
  2054. task->task_status.stat, status);
  2055. /* Wake up whatever process was waiting for this
  2056. * request to complete.
  2057. */
  2058. WARN_ON(request->io_request_completion == NULL);
  2059. if (request->io_request_completion != NULL) {
  2060. /* Signal whoever is waiting that this
  2061. * request is complete.
  2062. */
  2063. complete(request->io_request_completion);
  2064. }
  2065. break;
  2066. case isci_perform_error_io_completion:
  2067. /* Use sas_task_abort */
  2068. dev_warn(&host->pdev->dev,
  2069. "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
  2070. __func__,
  2071. task,
  2072. task->task_status.resp, response,
  2073. task->task_status.stat, status);
  2074. /* Add to the aborted list. */
  2075. list_add(&request->completed_node,
  2076. &host->requests_to_errorback);
  2077. break;
  2078. default:
  2079. dev_warn(&host->pdev->dev,
  2080. "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
  2081. __func__,
  2082. task,
  2083. task->task_status.resp, response,
  2084. task->task_status.stat, status);
  2085. /* Add to the error to libsas list. */
  2086. list_add(&request->completed_node,
  2087. &host->requests_to_errorback);
  2088. break;
  2089. }
  2090. }
  2091. static void isci_request_io_request_complete(struct isci_host *isci_host,
  2092. struct isci_request *request,
  2093. enum sci_io_status completion_status)
  2094. {
  2095. struct sas_task *task = isci_request_access_task(request);
  2096. struct ssp_response_iu *resp_iu;
  2097. void *resp_buf;
  2098. unsigned long task_flags;
  2099. struct isci_remote_device *idev = isci_lookup_device(task->dev);
  2100. enum service_response response = SAS_TASK_UNDELIVERED;
  2101. enum exec_status status = SAS_ABORTED_TASK;
  2102. enum isci_request_status request_status;
  2103. enum isci_completion_selection complete_to_host
  2104. = isci_perform_normal_io_completion;
  2105. dev_dbg(&isci_host->pdev->dev,
  2106. "%s: request = %p, task = %p,\n"
  2107. "task->data_dir = %d completion_status = 0x%x\n",
  2108. __func__,
  2109. request,
  2110. task,
  2111. task->data_dir,
  2112. completion_status);
  2113. spin_lock(&request->state_lock);
  2114. request_status = isci_request_get_state(request);
  2115. /* Decode the request status. Note that if the request has been
  2116. * aborted by a task management function, we don't care
  2117. * what the status is.
  2118. */
  2119. switch (request_status) {
  2120. case aborted:
  2121. /* "aborted" indicates that the request was aborted by a task
  2122. * management function, since once a task management request is
  2123. * perfomed by the device, the request only completes because
  2124. * of the subsequent driver terminate.
  2125. *
  2126. * Aborted also means an external thread is explicitly managing
  2127. * this request, so that we do not complete it up the stack.
  2128. *
  2129. * The target is still there (since the TMF was successful).
  2130. */
  2131. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2132. response = SAS_TASK_COMPLETE;
  2133. /* See if the device has been/is being stopped. Note
  2134. * that we ignore the quiesce state, since we are
  2135. * concerned about the actual device state.
  2136. */
  2137. if (!idev)
  2138. status = SAS_DEVICE_UNKNOWN;
  2139. else
  2140. status = SAS_ABORTED_TASK;
  2141. complete_to_host = isci_perform_aborted_io_completion;
  2142. /* This was an aborted request. */
  2143. spin_unlock(&request->state_lock);
  2144. break;
  2145. case aborting:
  2146. /* aborting means that the task management function tried and
  2147. * failed to abort the request. We need to note the request
  2148. * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
  2149. * target as down.
  2150. *
  2151. * Aborting also means an external thread is explicitly managing
  2152. * this request, so that we do not complete it up the stack.
  2153. */
  2154. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2155. response = SAS_TASK_UNDELIVERED;
  2156. if (!idev)
  2157. /* The device has been /is being stopped. Note that
  2158. * we ignore the quiesce state, since we are
  2159. * concerned about the actual device state.
  2160. */
  2161. status = SAS_DEVICE_UNKNOWN;
  2162. else
  2163. status = SAS_PHY_DOWN;
  2164. complete_to_host = isci_perform_aborted_io_completion;
  2165. /* This was an aborted request. */
  2166. spin_unlock(&request->state_lock);
  2167. break;
  2168. case terminating:
  2169. /* This was an terminated request. This happens when
  2170. * the I/O is being terminated because of an action on
  2171. * the device (reset, tear down, etc.), and the I/O needs
  2172. * to be completed up the stack.
  2173. */
  2174. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2175. response = SAS_TASK_UNDELIVERED;
  2176. /* See if the device has been/is being stopped. Note
  2177. * that we ignore the quiesce state, since we are
  2178. * concerned about the actual device state.
  2179. */
  2180. if (!idev)
  2181. status = SAS_DEVICE_UNKNOWN;
  2182. else
  2183. status = SAS_ABORTED_TASK;
  2184. complete_to_host = isci_perform_aborted_io_completion;
  2185. /* This was a terminated request. */
  2186. spin_unlock(&request->state_lock);
  2187. break;
  2188. case dead:
  2189. /* This was a terminated request that timed-out during the
  2190. * termination process. There is no task to complete to
  2191. * libsas.
  2192. */
  2193. complete_to_host = isci_perform_normal_io_completion;
  2194. spin_unlock(&request->state_lock);
  2195. break;
  2196. default:
  2197. /* The request is done from an SCU HW perspective. */
  2198. request->status = completed;
  2199. spin_unlock(&request->state_lock);
  2200. /* This is an active request being completed from the core. */
  2201. switch (completion_status) {
  2202. case SCI_IO_FAILURE_RESPONSE_VALID:
  2203. dev_dbg(&isci_host->pdev->dev,
  2204. "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
  2205. __func__,
  2206. request,
  2207. task);
  2208. if (sas_protocol_ata(task->task_proto)) {
  2209. resp_buf = &request->stp.rsp;
  2210. isci_request_process_stp_response(task,
  2211. resp_buf);
  2212. } else if (SAS_PROTOCOL_SSP == task->task_proto) {
  2213. /* crack the iu response buffer. */
  2214. resp_iu = &request->ssp.rsp;
  2215. isci_request_process_response_iu(task, resp_iu,
  2216. &isci_host->pdev->dev);
  2217. } else if (SAS_PROTOCOL_SMP == task->task_proto) {
  2218. dev_err(&isci_host->pdev->dev,
  2219. "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
  2220. "SAS_PROTOCOL_SMP protocol\n",
  2221. __func__);
  2222. } else
  2223. dev_err(&isci_host->pdev->dev,
  2224. "%s: unknown protocol\n", __func__);
  2225. /* use the task status set in the task struct by the
  2226. * isci_request_process_response_iu call.
  2227. */
  2228. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2229. response = task->task_status.resp;
  2230. status = task->task_status.stat;
  2231. break;
  2232. case SCI_IO_SUCCESS:
  2233. case SCI_IO_SUCCESS_IO_DONE_EARLY:
  2234. response = SAS_TASK_COMPLETE;
  2235. status = SAM_STAT_GOOD;
  2236. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2237. if (task->task_proto == SAS_PROTOCOL_SMP) {
  2238. void *rsp = &request->smp.rsp;
  2239. dev_dbg(&isci_host->pdev->dev,
  2240. "%s: SMP protocol completion\n",
  2241. __func__);
  2242. sg_copy_from_buffer(
  2243. &task->smp_task.smp_resp, 1,
  2244. rsp, sizeof(struct smp_resp));
  2245. } else if (completion_status
  2246. == SCI_IO_SUCCESS_IO_DONE_EARLY) {
  2247. /* This was an SSP / STP / SATA transfer.
  2248. * There is a possibility that less data than
  2249. * the maximum was transferred.
  2250. */
  2251. u32 transferred_length = sci_req_tx_bytes(request);
  2252. task->task_status.residual
  2253. = task->total_xfer_len - transferred_length;
  2254. /* If there were residual bytes, call this an
  2255. * underrun.
  2256. */
  2257. if (task->task_status.residual != 0)
  2258. status = SAS_DATA_UNDERRUN;
  2259. dev_dbg(&isci_host->pdev->dev,
  2260. "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
  2261. __func__,
  2262. status);
  2263. } else
  2264. dev_dbg(&isci_host->pdev->dev,
  2265. "%s: SCI_IO_SUCCESS\n",
  2266. __func__);
  2267. break;
  2268. case SCI_IO_FAILURE_TERMINATED:
  2269. dev_dbg(&isci_host->pdev->dev,
  2270. "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
  2271. __func__,
  2272. request,
  2273. task);
  2274. /* The request was terminated explicitly. No handling
  2275. * is needed in the SCSI error handler path.
  2276. */
  2277. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2278. response = SAS_TASK_UNDELIVERED;
  2279. /* See if the device has been/is being stopped. Note
  2280. * that we ignore the quiesce state, since we are
  2281. * concerned about the actual device state.
  2282. */
  2283. if (!idev)
  2284. status = SAS_DEVICE_UNKNOWN;
  2285. else
  2286. status = SAS_ABORTED_TASK;
  2287. complete_to_host = isci_perform_normal_io_completion;
  2288. break;
  2289. case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
  2290. isci_request_handle_controller_specific_errors(
  2291. idev, request, task, &response, &status,
  2292. &complete_to_host);
  2293. break;
  2294. case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
  2295. /* This is a special case, in that the I/O completion
  2296. * is telling us that the device needs a reset.
  2297. * In order for the device reset condition to be
  2298. * noticed, the I/O has to be handled in the error
  2299. * handler. Set the reset flag and cause the
  2300. * SCSI error thread to be scheduled.
  2301. */
  2302. spin_lock_irqsave(&task->task_state_lock, task_flags);
  2303. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  2304. spin_unlock_irqrestore(&task->task_state_lock, task_flags);
  2305. /* Fail the I/O. */
  2306. response = SAS_TASK_UNDELIVERED;
  2307. status = SAM_STAT_TASK_ABORTED;
  2308. complete_to_host = isci_perform_error_io_completion;
  2309. clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2310. break;
  2311. case SCI_FAILURE_RETRY_REQUIRED:
  2312. /* Fail the I/O so it can be retried. */
  2313. response = SAS_TASK_UNDELIVERED;
  2314. if (!idev)
  2315. status = SAS_DEVICE_UNKNOWN;
  2316. else
  2317. status = SAS_ABORTED_TASK;
  2318. complete_to_host = isci_perform_normal_io_completion;
  2319. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2320. break;
  2321. default:
  2322. /* Catch any otherwise unhandled error codes here. */
  2323. dev_warn(&isci_host->pdev->dev,
  2324. "%s: invalid completion code: 0x%x - "
  2325. "isci_request = %p\n",
  2326. __func__, completion_status, request);
  2327. response = SAS_TASK_UNDELIVERED;
  2328. /* See if the device has been/is being stopped. Note
  2329. * that we ignore the quiesce state, since we are
  2330. * concerned about the actual device state.
  2331. */
  2332. if (!idev)
  2333. status = SAS_DEVICE_UNKNOWN;
  2334. else
  2335. status = SAS_ABORTED_TASK;
  2336. if (SAS_PROTOCOL_SMP == task->task_proto) {
  2337. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2338. complete_to_host = isci_perform_normal_io_completion;
  2339. } else {
  2340. clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2341. complete_to_host = isci_perform_error_io_completion;
  2342. }
  2343. break;
  2344. }
  2345. break;
  2346. }
  2347. switch (task->task_proto) {
  2348. case SAS_PROTOCOL_SSP:
  2349. if (task->data_dir == DMA_NONE)
  2350. break;
  2351. if (task->num_scatter == 0)
  2352. /* 0 indicates a single dma address */
  2353. dma_unmap_single(&isci_host->pdev->dev,
  2354. request->zero_scatter_daddr,
  2355. task->total_xfer_len, task->data_dir);
  2356. else /* unmap the sgl dma addresses */
  2357. dma_unmap_sg(&isci_host->pdev->dev, task->scatter,
  2358. request->num_sg_entries, task->data_dir);
  2359. break;
  2360. case SAS_PROTOCOL_SMP: {
  2361. struct scatterlist *sg = &task->smp_task.smp_req;
  2362. struct smp_req *smp_req;
  2363. void *kaddr;
  2364. dma_unmap_sg(&isci_host->pdev->dev, sg, 1, DMA_TO_DEVICE);
  2365. /* need to swab it back in case the command buffer is re-used */
  2366. kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
  2367. smp_req = kaddr + sg->offset;
  2368. sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
  2369. kunmap_atomic(kaddr, KM_IRQ0);
  2370. break;
  2371. }
  2372. default:
  2373. break;
  2374. }
  2375. /* Put the completed request on the correct list */
  2376. isci_task_save_for_upper_layer_completion(isci_host, request, response,
  2377. status, complete_to_host
  2378. );
  2379. /* complete the io request to the core. */
  2380. scic_controller_complete_io(&isci_host->sci,
  2381. request->target_device,
  2382. request);
  2383. isci_put_device(idev);
  2384. /* set terminated handle so it cannot be completed or
  2385. * terminated again, and to cause any calls into abort
  2386. * task to recognize the already completed case.
  2387. */
  2388. set_bit(IREQ_TERMINATED, &request->flags);
  2389. }
  2390. static void scic_sds_request_started_state_enter(struct sci_base_state_machine *sm)
  2391. {
  2392. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2393. struct domain_device *dev = sci_dev_to_domain(ireq->target_device);
  2394. struct sas_task *task;
  2395. /* XXX as hch said always creating an internal sas_task for tmf
  2396. * requests would simplify the driver
  2397. */
  2398. task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
  2399. /* all unaccelerated request types (non ssp or ncq) handled with
  2400. * substates
  2401. */
  2402. if (!task && dev->dev_type == SAS_END_DEV) {
  2403. sci_change_state(sm, SCI_REQ_TASK_WAIT_TC_COMP);
  2404. } else if (!task &&
  2405. (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
  2406. isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
  2407. sci_change_state(sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED);
  2408. } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
  2409. sci_change_state(sm, SCI_REQ_SMP_WAIT_RESP);
  2410. } else if (task && sas_protocol_ata(task->task_proto) &&
  2411. !task->ata_task.use_ncq) {
  2412. u32 state;
  2413. if (task->data_dir == DMA_NONE)
  2414. state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
  2415. else if (task->ata_task.dma_xfer)
  2416. state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
  2417. else /* PIO */
  2418. state = SCI_REQ_STP_PIO_WAIT_H2D;
  2419. sci_change_state(sm, state);
  2420. }
  2421. }
  2422. static void scic_sds_request_completed_state_enter(struct sci_base_state_machine *sm)
  2423. {
  2424. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2425. struct scic_sds_controller *scic = ireq->owning_controller;
  2426. struct isci_host *ihost = scic_to_ihost(scic);
  2427. /* Tell the SCI_USER that the IO request is complete */
  2428. if (!test_bit(IREQ_TMF, &ireq->flags))
  2429. isci_request_io_request_complete(ihost, ireq,
  2430. ireq->sci_status);
  2431. else
  2432. isci_task_request_complete(ihost, ireq, ireq->sci_status);
  2433. }
  2434. static void scic_sds_request_aborting_state_enter(struct sci_base_state_machine *sm)
  2435. {
  2436. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2437. /* Setting the abort bit in the Task Context is required by the silicon. */
  2438. ireq->tc->abort = 1;
  2439. }
  2440. static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
  2441. {
  2442. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2443. scic_sds_remote_device_set_working_request(ireq->target_device,
  2444. ireq);
  2445. }
  2446. static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
  2447. {
  2448. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2449. scic_sds_remote_device_set_working_request(ireq->target_device,
  2450. ireq);
  2451. }
  2452. static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(struct sci_base_state_machine *sm)
  2453. {
  2454. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2455. scic_sds_remote_device_set_working_request(ireq->target_device,
  2456. ireq);
  2457. }
  2458. static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(struct sci_base_state_machine *sm)
  2459. {
  2460. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2461. struct scu_task_context *tc = ireq->tc;
  2462. struct host_to_dev_fis *h2d_fis;
  2463. enum sci_status status;
  2464. /* Clear the SRST bit */
  2465. h2d_fis = &ireq->stp.cmd;
  2466. h2d_fis->control = 0;
  2467. /* Clear the TC control bit */
  2468. tc->control_frame = 0;
  2469. status = scic_controller_continue_io(ireq);
  2470. WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
  2471. }
  2472. static const struct sci_base_state scic_sds_request_state_table[] = {
  2473. [SCI_REQ_INIT] = { },
  2474. [SCI_REQ_CONSTRUCTED] = { },
  2475. [SCI_REQ_STARTED] = {
  2476. .enter_state = scic_sds_request_started_state_enter,
  2477. },
  2478. [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
  2479. .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
  2480. },
  2481. [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
  2482. [SCI_REQ_STP_PIO_WAIT_H2D] = {
  2483. .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
  2484. },
  2485. [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
  2486. [SCI_REQ_STP_PIO_DATA_IN] = { },
  2487. [SCI_REQ_STP_PIO_DATA_OUT] = { },
  2488. [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
  2489. [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
  2490. [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
  2491. .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
  2492. },
  2493. [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
  2494. .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
  2495. },
  2496. [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
  2497. [SCI_REQ_TASK_WAIT_TC_COMP] = { },
  2498. [SCI_REQ_TASK_WAIT_TC_RESP] = { },
  2499. [SCI_REQ_SMP_WAIT_RESP] = { },
  2500. [SCI_REQ_SMP_WAIT_TC_COMP] = { },
  2501. [SCI_REQ_COMPLETED] = {
  2502. .enter_state = scic_sds_request_completed_state_enter,
  2503. },
  2504. [SCI_REQ_ABORTING] = {
  2505. .enter_state = scic_sds_request_aborting_state_enter,
  2506. },
  2507. [SCI_REQ_FINAL] = { },
  2508. };
  2509. static void
  2510. scic_sds_general_request_construct(struct scic_sds_controller *scic,
  2511. struct scic_sds_remote_device *sci_dev,
  2512. struct isci_request *ireq)
  2513. {
  2514. sci_init_sm(&ireq->sm, scic_sds_request_state_table, SCI_REQ_INIT);
  2515. ireq->target_device = sci_dev;
  2516. ireq->protocol = SCIC_NO_PROTOCOL;
  2517. ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
  2518. ireq->sci_status = SCI_SUCCESS;
  2519. ireq->scu_status = 0;
  2520. ireq->post_context = 0xFFFFFFFF;
  2521. }
  2522. static enum sci_status
  2523. scic_io_request_construct(struct scic_sds_controller *scic,
  2524. struct scic_sds_remote_device *sci_dev,
  2525. struct isci_request *ireq)
  2526. {
  2527. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  2528. enum sci_status status = SCI_SUCCESS;
  2529. /* Build the common part of the request */
  2530. scic_sds_general_request_construct(scic, sci_dev, ireq);
  2531. if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
  2532. return SCI_FAILURE_INVALID_REMOTE_DEVICE;
  2533. if (dev->dev_type == SAS_END_DEV)
  2534. /* pass */;
  2535. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  2536. memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
  2537. else if (dev_is_expander(dev))
  2538. /* pass */;
  2539. else
  2540. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2541. memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
  2542. return status;
  2543. }
  2544. enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
  2545. struct scic_sds_remote_device *sci_dev,
  2546. u16 io_tag, struct isci_request *ireq)
  2547. {
  2548. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  2549. enum sci_status status = SCI_SUCCESS;
  2550. /* Build the common part of the request */
  2551. scic_sds_general_request_construct(scic, sci_dev, ireq);
  2552. if (dev->dev_type == SAS_END_DEV ||
  2553. dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  2554. set_bit(IREQ_TMF, &ireq->flags);
  2555. memset(ireq->tc, 0, sizeof(struct scu_task_context));
  2556. } else
  2557. status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2558. return status;
  2559. }
  2560. static enum sci_status isci_request_ssp_request_construct(
  2561. struct isci_request *request)
  2562. {
  2563. enum sci_status status;
  2564. dev_dbg(&request->isci_host->pdev->dev,
  2565. "%s: request = %p\n",
  2566. __func__,
  2567. request);
  2568. status = scic_io_request_construct_basic_ssp(request);
  2569. return status;
  2570. }
  2571. static enum sci_status isci_request_stp_request_construct(
  2572. struct isci_request *request)
  2573. {
  2574. struct sas_task *task = isci_request_access_task(request);
  2575. enum sci_status status;
  2576. struct host_to_dev_fis *register_fis;
  2577. dev_dbg(&request->isci_host->pdev->dev,
  2578. "%s: request = %p\n",
  2579. __func__,
  2580. request);
  2581. /* Get the host_to_dev_fis from the core and copy
  2582. * the fis from the task into it.
  2583. */
  2584. register_fis = isci_sata_task_to_fis_copy(task);
  2585. status = scic_io_request_construct_basic_sata(request);
  2586. /* Set the ncq tag in the fis, from the queue
  2587. * command in the task.
  2588. */
  2589. if (isci_sata_is_task_ncq(task)) {
  2590. isci_sata_set_ncq_tag(
  2591. register_fis,
  2592. task
  2593. );
  2594. }
  2595. return status;
  2596. }
  2597. static enum sci_status
  2598. scic_io_request_construct_smp(struct device *dev,
  2599. struct isci_request *ireq,
  2600. struct sas_task *task)
  2601. {
  2602. struct scatterlist *sg = &task->smp_task.smp_req;
  2603. struct scic_sds_remote_device *sci_dev;
  2604. struct scu_task_context *task_context;
  2605. struct isci_port *iport;
  2606. struct smp_req *smp_req;
  2607. void *kaddr;
  2608. u8 req_len;
  2609. u32 cmd;
  2610. kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
  2611. smp_req = kaddr + sg->offset;
  2612. /*
  2613. * Look at the SMP requests' header fields; for certain SAS 1.x SMP
  2614. * functions under SAS 2.0, a zero request length really indicates
  2615. * a non-zero default length.
  2616. */
  2617. if (smp_req->req_len == 0) {
  2618. switch (smp_req->func) {
  2619. case SMP_DISCOVER:
  2620. case SMP_REPORT_PHY_ERR_LOG:
  2621. case SMP_REPORT_PHY_SATA:
  2622. case SMP_REPORT_ROUTE_INFO:
  2623. smp_req->req_len = 2;
  2624. break;
  2625. case SMP_CONF_ROUTE_INFO:
  2626. case SMP_PHY_CONTROL:
  2627. case SMP_PHY_TEST_FUNCTION:
  2628. smp_req->req_len = 9;
  2629. break;
  2630. /* Default - zero is a valid default for 2.0. */
  2631. }
  2632. }
  2633. req_len = smp_req->req_len;
  2634. sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
  2635. cmd = *(u32 *) smp_req;
  2636. kunmap_atomic(kaddr, KM_IRQ0);
  2637. if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
  2638. return SCI_FAILURE;
  2639. ireq->protocol = SCIC_SMP_PROTOCOL;
  2640. /* byte swap the smp request. */
  2641. task_context = ireq->tc;
  2642. sci_dev = scic_sds_request_get_device(ireq);
  2643. iport = scic_sds_request_get_port(ireq);
  2644. /*
  2645. * Fill in the TC with the its required data
  2646. * 00h
  2647. */
  2648. task_context->priority = 0;
  2649. task_context->initiator_request = 1;
  2650. task_context->connection_rate = sci_dev->connection_rate;
  2651. task_context->protocol_engine_index =
  2652. scic_sds_controller_get_protocol_engine_group(scic);
  2653. task_context->logical_port_index = scic_sds_port_get_index(iport);
  2654. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
  2655. task_context->abort = 0;
  2656. task_context->valid = SCU_TASK_CONTEXT_VALID;
  2657. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  2658. /* 04h */
  2659. task_context->remote_node_index = sci_dev->rnc.remote_node_index;
  2660. task_context->command_code = 0;
  2661. task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
  2662. /* 08h */
  2663. task_context->link_layer_control = 0;
  2664. task_context->do_not_dma_ssp_good_response = 1;
  2665. task_context->strict_ordering = 0;
  2666. task_context->control_frame = 1;
  2667. task_context->timeout_enable = 0;
  2668. task_context->block_guard_enable = 0;
  2669. /* 0ch */
  2670. task_context->address_modifier = 0;
  2671. /* 10h */
  2672. task_context->ssp_command_iu_length = req_len;
  2673. /* 14h */
  2674. task_context->transfer_length_bytes = 0;
  2675. /*
  2676. * 18h ~ 30h, protocol specific
  2677. * since commandIU has been build by framework at this point, we just
  2678. * copy the frist DWord from command IU to this location. */
  2679. memcpy(&task_context->type.smp, &cmd, sizeof(u32));
  2680. /*
  2681. * 40h
  2682. * "For SMP you could program it to zero. We would prefer that way
  2683. * so that done code will be consistent." - Venki
  2684. */
  2685. task_context->task_phase = 0;
  2686. ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  2687. (scic_sds_controller_get_protocol_engine_group(scic) <<
  2688. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  2689. (scic_sds_port_get_index(iport) <<
  2690. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  2691. ISCI_TAG_TCI(ireq->io_tag));
  2692. /*
  2693. * Copy the physical address for the command buffer to the SCU Task
  2694. * Context command buffer should not contain command header.
  2695. */
  2696. task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
  2697. task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
  2698. /* SMP response comes as UF, so no need to set response IU address. */
  2699. task_context->response_iu_upper = 0;
  2700. task_context->response_iu_lower = 0;
  2701. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  2702. return SCI_SUCCESS;
  2703. }
  2704. /*
  2705. * isci_smp_request_build() - This function builds the smp request.
  2706. * @ireq: This parameter points to the isci_request allocated in the
  2707. * request construct function.
  2708. *
  2709. * SCI_SUCCESS on successfull completion, or specific failure code.
  2710. */
  2711. static enum sci_status isci_smp_request_build(struct isci_request *ireq)
  2712. {
  2713. struct sas_task *task = isci_request_access_task(ireq);
  2714. struct device *dev = &ireq->isci_host->pdev->dev;
  2715. enum sci_status status = SCI_FAILURE;
  2716. status = scic_io_request_construct_smp(dev, ireq, task);
  2717. if (status != SCI_SUCCESS)
  2718. dev_warn(&ireq->isci_host->pdev->dev,
  2719. "%s: failed with status = %d\n",
  2720. __func__,
  2721. status);
  2722. return status;
  2723. }
  2724. /**
  2725. * isci_io_request_build() - This function builds the io request object.
  2726. * @isci_host: This parameter specifies the ISCI host object
  2727. * @request: This parameter points to the isci_request object allocated in the
  2728. * request construct function.
  2729. * @sci_device: This parameter is the handle for the sci core's remote device
  2730. * object that is the destination for this request.
  2731. *
  2732. * SCI_SUCCESS on successfull completion, or specific failure code.
  2733. */
  2734. static enum sci_status isci_io_request_build(struct isci_host *isci_host,
  2735. struct isci_request *request,
  2736. struct isci_remote_device *isci_device)
  2737. {
  2738. enum sci_status status = SCI_SUCCESS;
  2739. struct sas_task *task = isci_request_access_task(request);
  2740. struct scic_sds_remote_device *sci_device = &isci_device->sci;
  2741. dev_dbg(&isci_host->pdev->dev,
  2742. "%s: isci_device = 0x%p; request = %p, "
  2743. "num_scatter = %d\n",
  2744. __func__,
  2745. isci_device,
  2746. request,
  2747. task->num_scatter);
  2748. /* map the sgl addresses, if present.
  2749. * libata does the mapping for sata devices
  2750. * before we get the request.
  2751. */
  2752. if (task->num_scatter &&
  2753. !sas_protocol_ata(task->task_proto) &&
  2754. !(SAS_PROTOCOL_SMP & task->task_proto)) {
  2755. request->num_sg_entries = dma_map_sg(
  2756. &isci_host->pdev->dev,
  2757. task->scatter,
  2758. task->num_scatter,
  2759. task->data_dir
  2760. );
  2761. if (request->num_sg_entries == 0)
  2762. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2763. }
  2764. status = scic_io_request_construct(&isci_host->sci, sci_device,
  2765. request);
  2766. if (status != SCI_SUCCESS) {
  2767. dev_warn(&isci_host->pdev->dev,
  2768. "%s: failed request construct\n",
  2769. __func__);
  2770. return SCI_FAILURE;
  2771. }
  2772. switch (task->task_proto) {
  2773. case SAS_PROTOCOL_SMP:
  2774. status = isci_smp_request_build(request);
  2775. break;
  2776. case SAS_PROTOCOL_SSP:
  2777. status = isci_request_ssp_request_construct(request);
  2778. break;
  2779. case SAS_PROTOCOL_SATA:
  2780. case SAS_PROTOCOL_STP:
  2781. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  2782. status = isci_request_stp_request_construct(request);
  2783. break;
  2784. default:
  2785. dev_warn(&isci_host->pdev->dev,
  2786. "%s: unknown protocol\n", __func__);
  2787. return SCI_FAILURE;
  2788. }
  2789. return SCI_SUCCESS;
  2790. }
  2791. static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
  2792. {
  2793. struct isci_request *ireq;
  2794. ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
  2795. ireq->io_tag = tag;
  2796. ireq->io_request_completion = NULL;
  2797. ireq->flags = 0;
  2798. ireq->num_sg_entries = 0;
  2799. INIT_LIST_HEAD(&ireq->completed_node);
  2800. INIT_LIST_HEAD(&ireq->dev_node);
  2801. isci_request_change_state(ireq, allocated);
  2802. return ireq;
  2803. }
  2804. static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
  2805. struct sas_task *task,
  2806. u16 tag)
  2807. {
  2808. struct isci_request *ireq;
  2809. ireq = isci_request_from_tag(ihost, tag);
  2810. ireq->ttype_ptr.io_task_ptr = task;
  2811. ireq->ttype = io_task;
  2812. task->lldd_task = ireq;
  2813. return ireq;
  2814. }
  2815. struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
  2816. struct isci_tmf *isci_tmf,
  2817. u16 tag)
  2818. {
  2819. struct isci_request *ireq;
  2820. ireq = isci_request_from_tag(ihost, tag);
  2821. ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
  2822. ireq->ttype = tmf_task;
  2823. return ireq;
  2824. }
  2825. int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
  2826. struct sas_task *task, u16 tag)
  2827. {
  2828. enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2829. struct isci_request *ireq;
  2830. unsigned long flags;
  2831. int ret = 0;
  2832. /* do common allocation and init of request object. */
  2833. ireq = isci_io_request_from_tag(ihost, task, tag);
  2834. status = isci_io_request_build(ihost, ireq, idev);
  2835. if (status != SCI_SUCCESS) {
  2836. dev_warn(&ihost->pdev->dev,
  2837. "%s: request_construct failed - status = 0x%x\n",
  2838. __func__,
  2839. status);
  2840. return status;
  2841. }
  2842. spin_lock_irqsave(&ihost->scic_lock, flags);
  2843. if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
  2844. if (isci_task_is_ncq_recovery(task)) {
  2845. /* The device is in an NCQ recovery state. Issue the
  2846. * request on the task side. Note that it will
  2847. * complete on the I/O request side because the
  2848. * request was built that way (ie.
  2849. * ireq->is_task_management_request is false).
  2850. */
  2851. status = scic_controller_start_task(&ihost->sci,
  2852. &idev->sci,
  2853. ireq);
  2854. } else {
  2855. status = SCI_FAILURE;
  2856. }
  2857. } else {
  2858. /* send the request, let the core assign the IO TAG. */
  2859. status = scic_controller_start_io(&ihost->sci, &idev->sci,
  2860. ireq);
  2861. }
  2862. if (status != SCI_SUCCESS &&
  2863. status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  2864. dev_warn(&ihost->pdev->dev,
  2865. "%s: failed request start (0x%x)\n",
  2866. __func__, status);
  2867. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  2868. return status;
  2869. }
  2870. /* Either I/O started OK, or the core has signaled that
  2871. * the device needs a target reset.
  2872. *
  2873. * In either case, hold onto the I/O for later.
  2874. *
  2875. * Update it's status and add it to the list in the
  2876. * remote device object.
  2877. */
  2878. list_add(&ireq->dev_node, &idev->reqs_in_process);
  2879. if (status == SCI_SUCCESS) {
  2880. isci_request_change_state(ireq, started);
  2881. } else {
  2882. /* The request did not really start in the
  2883. * hardware, so clear the request handle
  2884. * here so no terminations will be done.
  2885. */
  2886. set_bit(IREQ_TERMINATED, &ireq->flags);
  2887. isci_request_change_state(ireq, completed);
  2888. }
  2889. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  2890. if (status ==
  2891. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  2892. /* Signal libsas that we need the SCSI error
  2893. * handler thread to work on this I/O and that
  2894. * we want a device reset.
  2895. */
  2896. spin_lock_irqsave(&task->task_state_lock, flags);
  2897. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  2898. spin_unlock_irqrestore(&task->task_state_lock, flags);
  2899. /* Cause this task to be scheduled in the SCSI error
  2900. * handler thread.
  2901. */
  2902. isci_execpath_callback(ihost, task,
  2903. sas_task_abort);
  2904. /* Change the status, since we are holding
  2905. * the I/O until it is managed by the SCSI
  2906. * error handler.
  2907. */
  2908. status = SCI_SUCCESS;
  2909. }
  2910. return ret;
  2911. }