request.c 99 KB

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