request.c 99 KB

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