request.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include "isci.h"
  56. #include "task.h"
  57. #include "request.h"
  58. #include "sata.h"
  59. #include "scu_completion_codes.h"
  60. #include "sas.h"
  61. /**
  62. * This method returns the sgl element pair for the specificed sgl_pair index.
  63. * @sci_req: This parameter specifies the IO request for which to retrieve
  64. * the Scatter-Gather List element pair.
  65. * @sgl_pair_index: This parameter specifies the index into the SGL element
  66. * pair to be retrieved.
  67. *
  68. * This method returns a pointer to an struct scu_sgl_element_pair.
  69. */
  70. static struct scu_sgl_element_pair *scic_sds_request_get_sgl_element_pair(
  71. struct scic_sds_request *sci_req,
  72. u32 sgl_pair_index
  73. ) {
  74. struct scu_task_context *task_context;
  75. task_context = (struct scu_task_context *)sci_req->task_context_buffer;
  76. if (sgl_pair_index == 0) {
  77. return &task_context->sgl_pair_ab;
  78. } else if (sgl_pair_index == 1) {
  79. return &task_context->sgl_pair_cd;
  80. }
  81. return &sci_req->sg_table[sgl_pair_index - 2];
  82. }
  83. /**
  84. * This function will build the SGL list for an IO request.
  85. * @sci_req: This parameter specifies the IO request for which to build
  86. * the Scatter-Gather List.
  87. *
  88. */
  89. void scic_sds_request_build_sgl(struct scic_sds_request *sds_request)
  90. {
  91. struct isci_request *isci_request = sci_req_to_ireq(sds_request);
  92. struct isci_host *isci_host = isci_request->isci_host;
  93. struct sas_task *task = isci_request_access_task(isci_request);
  94. struct scatterlist *sg = NULL;
  95. dma_addr_t dma_addr;
  96. u32 sg_idx = 0;
  97. struct scu_sgl_element_pair *scu_sg = NULL;
  98. struct scu_sgl_element_pair *prev_sg = NULL;
  99. if (task->num_scatter > 0) {
  100. sg = task->scatter;
  101. while (sg) {
  102. scu_sg = scic_sds_request_get_sgl_element_pair(
  103. sds_request,
  104. sg_idx);
  105. SCU_SGL_COPY(scu_sg->A, sg);
  106. sg = sg_next(sg);
  107. if (sg) {
  108. SCU_SGL_COPY(scu_sg->B, sg);
  109. sg = sg_next(sg);
  110. } else
  111. SCU_SGL_ZERO(scu_sg->B);
  112. if (prev_sg) {
  113. dma_addr =
  114. scic_io_request_get_dma_addr(
  115. sds_request,
  116. scu_sg);
  117. prev_sg->next_pair_upper =
  118. upper_32_bits(dma_addr);
  119. prev_sg->next_pair_lower =
  120. lower_32_bits(dma_addr);
  121. }
  122. prev_sg = scu_sg;
  123. sg_idx++;
  124. }
  125. } else { /* handle when no sg */
  126. scu_sg = scic_sds_request_get_sgl_element_pair(sds_request,
  127. sg_idx);
  128. dma_addr = dma_map_single(&isci_host->pdev->dev,
  129. task->scatter,
  130. task->total_xfer_len,
  131. task->data_dir);
  132. isci_request->zero_scatter_daddr = dma_addr;
  133. scu_sg->A.length = task->total_xfer_len;
  134. scu_sg->A.address_upper = upper_32_bits(dma_addr);
  135. scu_sg->A.address_lower = lower_32_bits(dma_addr);
  136. }
  137. if (scu_sg) {
  138. scu_sg->next_pair_upper = 0;
  139. scu_sg->next_pair_lower = 0;
  140. }
  141. }
  142. static void scic_sds_ssp_io_request_assign_buffers(struct scic_sds_request *sci_req)
  143. {
  144. if (sci_req->was_tag_assigned_by_user == false)
  145. sci_req->task_context_buffer = &sci_req->tc;
  146. }
  147. static void scic_sds_io_request_build_ssp_command_iu(struct scic_sds_request *sci_req)
  148. {
  149. struct ssp_cmd_iu *cmd_iu;
  150. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  151. struct sas_task *task = isci_request_access_task(ireq);
  152. cmd_iu = &sci_req->ssp.cmd;
  153. memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
  154. cmd_iu->add_cdb_len = 0;
  155. cmd_iu->_r_a = 0;
  156. cmd_iu->_r_b = 0;
  157. cmd_iu->en_fburst = 0; /* unsupported */
  158. cmd_iu->task_prio = task->ssp_task.task_prio;
  159. cmd_iu->task_attr = task->ssp_task.task_attr;
  160. cmd_iu->_r_c = 0;
  161. sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
  162. sizeof(task->ssp_task.cdb) / sizeof(u32));
  163. }
  164. static void scic_sds_task_request_build_ssp_task_iu(struct scic_sds_request *sci_req)
  165. {
  166. struct ssp_task_iu *task_iu;
  167. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  168. struct sas_task *task = isci_request_access_task(ireq);
  169. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  170. task_iu = &sci_req->ssp.tmf;
  171. memset(task_iu, 0, sizeof(struct ssp_task_iu));
  172. memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
  173. task_iu->task_func = isci_tmf->tmf_code;
  174. task_iu->task_tag =
  175. (ireq->ttype == tmf_task) ?
  176. isci_tmf->io_tag :
  177. SCI_CONTROLLER_INVALID_IO_TAG;
  178. }
  179. /**
  180. * This method is will fill in the SCU Task Context for any type of SSP request.
  181. * @sci_req:
  182. * @task_context:
  183. *
  184. */
  185. static void scu_ssp_reqeust_construct_task_context(
  186. struct scic_sds_request *sds_request,
  187. struct scu_task_context *task_context)
  188. {
  189. dma_addr_t dma_addr;
  190. struct scic_sds_controller *controller;
  191. struct scic_sds_remote_device *target_device;
  192. struct scic_sds_port *target_port;
  193. controller = scic_sds_request_get_controller(sds_request);
  194. target_device = scic_sds_request_get_device(sds_request);
  195. target_port = scic_sds_request_get_port(sds_request);
  196. /* Fill in the TC with the its required data */
  197. task_context->abort = 0;
  198. task_context->priority = 0;
  199. task_context->initiator_request = 1;
  200. task_context->connection_rate = target_device->connection_rate;
  201. task_context->protocol_engine_index =
  202. scic_sds_controller_get_protocol_engine_group(controller);
  203. task_context->logical_port_index =
  204. scic_sds_port_get_index(target_port);
  205. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
  206. task_context->valid = SCU_TASK_CONTEXT_VALID;
  207. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  208. task_context->remote_node_index =
  209. scic_sds_remote_device_get_index(sds_request->target_device);
  210. task_context->command_code = 0;
  211. task_context->link_layer_control = 0;
  212. task_context->do_not_dma_ssp_good_response = 1;
  213. task_context->strict_ordering = 0;
  214. task_context->control_frame = 0;
  215. task_context->timeout_enable = 0;
  216. task_context->block_guard_enable = 0;
  217. task_context->address_modifier = 0;
  218. /* task_context->type.ssp.tag = sci_req->io_tag; */
  219. task_context->task_phase = 0x01;
  220. if (sds_request->was_tag_assigned_by_user) {
  221. /*
  222. * Build the task context now since we have already read
  223. * the data
  224. */
  225. sds_request->post_context =
  226. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  227. (scic_sds_controller_get_protocol_engine_group(
  228. controller) <<
  229. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  230. (scic_sds_port_get_index(target_port) <<
  231. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  232. scic_sds_io_tag_get_index(sds_request->io_tag));
  233. } else {
  234. /*
  235. * Build the task context now since we have already read
  236. * the data
  237. *
  238. * I/O tag index is not assigned because we have to wait
  239. * until we get a TCi
  240. */
  241. sds_request->post_context =
  242. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  243. (scic_sds_controller_get_protocol_engine_group(
  244. owning_controller) <<
  245. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  246. (scic_sds_port_get_index(target_port) <<
  247. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  248. }
  249. /*
  250. * Copy the physical address for the command buffer to the
  251. * SCU Task Context
  252. */
  253. dma_addr = scic_io_request_get_dma_addr(sds_request,
  254. &sds_request->ssp.cmd);
  255. task_context->command_iu_upper = upper_32_bits(dma_addr);
  256. task_context->command_iu_lower = lower_32_bits(dma_addr);
  257. /*
  258. * Copy the physical address for the response buffer to the
  259. * SCU Task Context
  260. */
  261. dma_addr = scic_io_request_get_dma_addr(sds_request,
  262. &sds_request->ssp.rsp);
  263. task_context->response_iu_upper = upper_32_bits(dma_addr);
  264. task_context->response_iu_lower = lower_32_bits(dma_addr);
  265. }
  266. /**
  267. * This method is will fill in the SCU Task Context for a SSP IO request.
  268. * @sci_req:
  269. *
  270. */
  271. static void scu_ssp_io_request_construct_task_context(
  272. struct scic_sds_request *sci_req,
  273. enum dma_data_direction dir,
  274. u32 len)
  275. {
  276. struct scu_task_context *task_context;
  277. task_context = scic_sds_request_get_task_context(sci_req);
  278. scu_ssp_reqeust_construct_task_context(sci_req, task_context);
  279. task_context->ssp_command_iu_length =
  280. sizeof(struct ssp_cmd_iu) / sizeof(u32);
  281. task_context->type.ssp.frame_type = SSP_COMMAND;
  282. switch (dir) {
  283. case DMA_FROM_DEVICE:
  284. case DMA_NONE:
  285. default:
  286. task_context->task_type = SCU_TASK_TYPE_IOREAD;
  287. break;
  288. case DMA_TO_DEVICE:
  289. task_context->task_type = SCU_TASK_TYPE_IOWRITE;
  290. break;
  291. }
  292. task_context->transfer_length_bytes = len;
  293. if (task_context->transfer_length_bytes > 0)
  294. scic_sds_request_build_sgl(sci_req);
  295. }
  296. static void scic_sds_ssp_task_request_assign_buffers(struct scic_sds_request *sci_req)
  297. {
  298. if (sci_req->was_tag_assigned_by_user == false)
  299. sci_req->task_context_buffer = &sci_req->tc;
  300. }
  301. /**
  302. * This method will fill in the SCU Task Context for a SSP Task request. The
  303. * following important settings are utilized: -# priority ==
  304. * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
  305. * ahead of other task destined for the same Remote Node. -# task_type ==
  306. * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
  307. * (i.e. non-raw frame) is being utilized to perform task management. -#
  308. * control_frame == 1. This ensures that the proper endianess is set so
  309. * that the bytes are transmitted in the right order for a task frame.
  310. * @sci_req: This parameter specifies the task request object being
  311. * constructed.
  312. *
  313. */
  314. static void scu_ssp_task_request_construct_task_context(
  315. struct scic_sds_request *sci_req)
  316. {
  317. struct scu_task_context *task_context;
  318. task_context = scic_sds_request_get_task_context(sci_req);
  319. scu_ssp_reqeust_construct_task_context(sci_req, task_context);
  320. task_context->control_frame = 1;
  321. task_context->priority = SCU_TASK_PRIORITY_HIGH;
  322. task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
  323. task_context->transfer_length_bytes = 0;
  324. task_context->type.ssp.frame_type = SSP_TASK;
  325. task_context->ssp_command_iu_length =
  326. sizeof(struct ssp_task_iu) / sizeof(u32);
  327. }
  328. /**
  329. * This method constructs the SSP Command IU data for this ssp passthrough
  330. * comand request object.
  331. * @sci_req: This parameter specifies the request object for which the SSP
  332. * command information unit is being built.
  333. *
  334. * enum sci_status, returns invalid parameter is cdb > 16
  335. */
  336. /**
  337. * This method constructs the SATA request object.
  338. * @sci_req:
  339. * @sat_protocol:
  340. * @transfer_length:
  341. * @data_direction:
  342. * @copy_rx_frame:
  343. *
  344. * enum sci_status
  345. */
  346. static enum sci_status
  347. scic_io_request_construct_sata(struct scic_sds_request *sci_req,
  348. u32 len,
  349. enum dma_data_direction dir,
  350. bool copy)
  351. {
  352. enum sci_status status = SCI_SUCCESS;
  353. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  354. struct sas_task *task = isci_request_access_task(ireq);
  355. /* check for management protocols */
  356. if (ireq->ttype == tmf_task) {
  357. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  358. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  359. tmf->tmf_code == isci_tmf_sata_srst_low)
  360. return scic_sds_stp_soft_reset_request_construct(sci_req);
  361. else {
  362. dev_err(scic_to_dev(sci_req->owning_controller),
  363. "%s: Request 0x%p received un-handled SAT "
  364. "management protocol 0x%x.\n",
  365. __func__, sci_req, tmf->tmf_code);
  366. return SCI_FAILURE;
  367. }
  368. }
  369. if (!sas_protocol_ata(task->task_proto)) {
  370. dev_err(scic_to_dev(sci_req->owning_controller),
  371. "%s: Non-ATA protocol in SATA path: 0x%x\n",
  372. __func__,
  373. task->task_proto);
  374. return SCI_FAILURE;
  375. }
  376. /* non data */
  377. if (task->data_dir == DMA_NONE)
  378. return scic_sds_stp_non_data_request_construct(sci_req);
  379. /* NCQ */
  380. if (task->ata_task.use_ncq)
  381. return scic_sds_stp_ncq_request_construct(sci_req, len, dir);
  382. /* DMA */
  383. if (task->ata_task.dma_xfer)
  384. return scic_sds_stp_udma_request_construct(sci_req, len, dir);
  385. else /* PIO */
  386. return scic_sds_stp_pio_request_construct(sci_req, copy);
  387. return status;
  388. }
  389. static enum sci_status scic_io_request_construct_basic_ssp(struct scic_sds_request *sci_req)
  390. {
  391. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  392. struct sas_task *task = isci_request_access_task(ireq);
  393. sci_req->protocol = SCIC_SSP_PROTOCOL;
  394. scu_ssp_io_request_construct_task_context(sci_req,
  395. task->data_dir,
  396. task->total_xfer_len);
  397. scic_sds_io_request_build_ssp_command_iu(sci_req);
  398. sci_base_state_machine_change_state(
  399. &sci_req->state_machine,
  400. SCI_BASE_REQUEST_STATE_CONSTRUCTED);
  401. return SCI_SUCCESS;
  402. }
  403. enum sci_status scic_task_request_construct_ssp(
  404. struct scic_sds_request *sci_req)
  405. {
  406. /* Construct the SSP Task SCU Task Context */
  407. scu_ssp_task_request_construct_task_context(sci_req);
  408. /* Fill in the SSP Task IU */
  409. scic_sds_task_request_build_ssp_task_iu(sci_req);
  410. sci_base_state_machine_change_state(&sci_req->state_machine,
  411. SCI_BASE_REQUEST_STATE_CONSTRUCTED);
  412. return SCI_SUCCESS;
  413. }
  414. static enum sci_status scic_io_request_construct_basic_sata(struct scic_sds_request *sci_req)
  415. {
  416. enum sci_status status;
  417. struct scic_sds_stp_request *stp_req;
  418. bool copy = false;
  419. struct isci_request *isci_request = sci_req_to_ireq(sci_req);
  420. struct sas_task *task = isci_request_access_task(isci_request);
  421. stp_req = &sci_req->stp.req;
  422. sci_req->protocol = SCIC_STP_PROTOCOL;
  423. copy = (task->data_dir == DMA_NONE) ? false : true;
  424. status = scic_io_request_construct_sata(sci_req,
  425. task->total_xfer_len,
  426. task->data_dir,
  427. copy);
  428. if (status == SCI_SUCCESS)
  429. sci_base_state_machine_change_state(&sci_req->state_machine,
  430. SCI_BASE_REQUEST_STATE_CONSTRUCTED);
  431. return status;
  432. }
  433. enum sci_status scic_task_request_construct_sata(struct scic_sds_request *sci_req)
  434. {
  435. enum sci_status status = SCI_SUCCESS;
  436. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  437. /* check for management protocols */
  438. if (ireq->ttype == tmf_task) {
  439. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  440. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  441. tmf->tmf_code == isci_tmf_sata_srst_low) {
  442. status = scic_sds_stp_soft_reset_request_construct(sci_req);
  443. } else {
  444. dev_err(scic_to_dev(sci_req->owning_controller),
  445. "%s: Request 0x%p received un-handled SAT "
  446. "Protocol 0x%x.\n",
  447. __func__, sci_req, tmf->tmf_code);
  448. return SCI_FAILURE;
  449. }
  450. }
  451. if (status == SCI_SUCCESS)
  452. sci_base_state_machine_change_state(
  453. &sci_req->state_machine,
  454. SCI_BASE_REQUEST_STATE_CONSTRUCTED);
  455. return status;
  456. }
  457. /**
  458. * sci_req_tx_bytes - bytes transferred when reply underruns request
  459. * @sci_req: request that was terminated early
  460. */
  461. #define SCU_TASK_CONTEXT_SRAM 0x200000
  462. static u32 sci_req_tx_bytes(struct scic_sds_request *sci_req)
  463. {
  464. struct scic_sds_controller *scic = sci_req->owning_controller;
  465. u32 ret_val = 0;
  466. if (readl(&scic->smu_registers->address_modifier) == 0) {
  467. void __iomem *scu_reg_base = scic->scu_registers;
  468. /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
  469. * BAR1 is the scu_registers
  470. * 0x20002C = 0x200000 + 0x2c
  471. * = start of task context SRAM + offset of (type.ssp.data_offset)
  472. * TCi is the io_tag of struct scic_sds_request
  473. */
  474. ret_val = readl(scu_reg_base +
  475. (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
  476. ((sizeof(struct scu_task_context)) * scic_sds_io_tag_get_index(sci_req->io_tag)));
  477. }
  478. return ret_val;
  479. }
  480. enum sci_status
  481. scic_sds_request_start(struct scic_sds_request *request)
  482. {
  483. if (request->device_sequence !=
  484. scic_sds_remote_device_get_sequence(request->target_device))
  485. return SCI_FAILURE;
  486. if (request->state_handlers->start_handler)
  487. return request->state_handlers->start_handler(request);
  488. dev_warn(scic_to_dev(request->owning_controller),
  489. "%s: SCIC IO Request requested to start while in wrong "
  490. "state %d\n",
  491. __func__,
  492. sci_base_state_machine_get_state(&request->state_machine));
  493. return SCI_FAILURE_INVALID_STATE;
  494. }
  495. enum sci_status
  496. scic_sds_io_request_terminate(struct scic_sds_request *request)
  497. {
  498. if (request->state_handlers->abort_handler)
  499. return request->state_handlers->abort_handler(request);
  500. dev_warn(scic_to_dev(request->owning_controller),
  501. "%s: SCIC IO Request requested to abort while in wrong "
  502. "state %d\n",
  503. __func__,
  504. sci_base_state_machine_get_state(&request->state_machine));
  505. return SCI_FAILURE_INVALID_STATE;
  506. }
  507. enum sci_status scic_sds_io_request_event_handler(
  508. struct scic_sds_request *request,
  509. u32 event_code)
  510. {
  511. if (request->state_handlers->event_handler)
  512. return request->state_handlers->event_handler(request, event_code);
  513. dev_warn(scic_to_dev(request->owning_controller),
  514. "%s: SCIC IO Request given event code notification %x while "
  515. "in wrong state %d\n",
  516. __func__,
  517. event_code,
  518. sci_base_state_machine_get_state(&request->state_machine));
  519. return SCI_FAILURE_INVALID_STATE;
  520. }
  521. /**
  522. *
  523. * @sci_req: The SCIC_SDS_IO_REQUEST_T object for which the start
  524. * operation is to be executed.
  525. * @frame_index: The frame index returned by the hardware for the reqeust
  526. * object.
  527. *
  528. * This method invokes the core state frame handler for the
  529. * SCIC_SDS_IO_REQUEST_T object. enum sci_status
  530. */
  531. enum sci_status scic_sds_io_request_frame_handler(
  532. struct scic_sds_request *request,
  533. u32 frame_index)
  534. {
  535. if (request->state_handlers->frame_handler)
  536. return request->state_handlers->frame_handler(request, frame_index);
  537. dev_warn(scic_to_dev(request->owning_controller),
  538. "%s: SCIC IO Request given unexpected frame %x while in "
  539. "state %d\n",
  540. __func__,
  541. frame_index,
  542. sci_base_state_machine_get_state(&request->state_machine));
  543. scic_sds_controller_release_frame(request->owning_controller, frame_index);
  544. return SCI_FAILURE_INVALID_STATE;
  545. }
  546. /*
  547. * This function copies response data for requests returning response data
  548. * instead of sense data.
  549. * @sci_req: This parameter specifies the request object for which to copy
  550. * the response data.
  551. */
  552. static void scic_sds_io_request_copy_response(struct scic_sds_request *sci_req)
  553. {
  554. void *resp_buf;
  555. u32 len;
  556. struct ssp_response_iu *ssp_response;
  557. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  558. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  559. ssp_response = &sci_req->ssp.rsp;
  560. resp_buf = &isci_tmf->resp.resp_iu;
  561. len = min_t(u32,
  562. SSP_RESP_IU_MAX_SIZE,
  563. be32_to_cpu(ssp_response->response_data_len));
  564. memcpy(resp_buf, ssp_response->resp_data, len);
  565. }
  566. /*
  567. * This method implements the action taken when a constructed
  568. * SCIC_SDS_IO_REQUEST_T object receives a scic_sds_request_start() request.
  569. * This method will, if necessary, allocate a TCi for the io request object and
  570. * then will, if necessary, copy the constructed TC data into the actual TC
  571. * buffer. If everything is successful the post context field is updated with
  572. * the TCi so the controller can post the request to the hardware. enum sci_status
  573. * SCI_SUCCESS SCI_FAILURE_INSUFFICIENT_RESOURCES
  574. */
  575. static enum sci_status scic_sds_request_constructed_state_start_handler(
  576. struct scic_sds_request *request)
  577. {
  578. struct scu_task_context *task_context;
  579. if (request->io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  580. request->io_tag =
  581. scic_controller_allocate_io_tag(request->owning_controller);
  582. }
  583. /* Record the IO Tag in the request */
  584. if (request->io_tag != SCI_CONTROLLER_INVALID_IO_TAG) {
  585. task_context = request->task_context_buffer;
  586. task_context->task_index = scic_sds_io_tag_get_index(request->io_tag);
  587. switch (task_context->protocol_type) {
  588. case SCU_TASK_CONTEXT_PROTOCOL_SMP:
  589. case SCU_TASK_CONTEXT_PROTOCOL_SSP:
  590. /* SSP/SMP Frame */
  591. task_context->type.ssp.tag = request->io_tag;
  592. task_context->type.ssp.target_port_transfer_tag = 0xFFFF;
  593. break;
  594. case SCU_TASK_CONTEXT_PROTOCOL_STP:
  595. /*
  596. * STP/SATA Frame
  597. * task_context->type.stp.ncq_tag = request->ncq_tag; */
  598. break;
  599. case SCU_TASK_CONTEXT_PROTOCOL_NONE:
  600. /* / @todo When do we set no protocol type? */
  601. break;
  602. default:
  603. /* This should never happen since we build the IO requests */
  604. break;
  605. }
  606. /*
  607. * Check to see if we need to copy the task context buffer
  608. * or have been building into the task context buffer */
  609. if (request->was_tag_assigned_by_user == false) {
  610. scic_sds_controller_copy_task_context(
  611. request->owning_controller, request);
  612. }
  613. /* Add to the post_context the io tag value */
  614. request->post_context |= scic_sds_io_tag_get_index(request->io_tag);
  615. /* Everything is good go ahead and change state */
  616. sci_base_state_machine_change_state(&request->state_machine,
  617. SCI_BASE_REQUEST_STATE_STARTED);
  618. return SCI_SUCCESS;
  619. }
  620. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  621. }
  622. /*
  623. * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
  624. * object receives a scic_sds_request_terminate() request. Since the request
  625. * has not yet been posted to the hardware the request transitions to the
  626. * completed state. enum sci_status SCI_SUCCESS
  627. */
  628. static enum sci_status scic_sds_request_constructed_state_abort_handler(
  629. struct scic_sds_request *request)
  630. {
  631. /*
  632. * This request has been terminated by the user make sure that the correct
  633. * status code is returned */
  634. scic_sds_request_set_status(request,
  635. SCU_TASK_DONE_TASK_ABORT,
  636. SCI_FAILURE_IO_TERMINATED);
  637. sci_base_state_machine_change_state(&request->state_machine,
  638. SCI_BASE_REQUEST_STATE_COMPLETED);
  639. return SCI_SUCCESS;
  640. }
  641. /*
  642. * *****************************************************************************
  643. * * STARTED STATE HANDLERS
  644. * ***************************************************************************** */
  645. /*
  646. * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
  647. * object receives a scic_sds_request_terminate() request. Since the request
  648. * has been posted to the hardware the io request state is changed to the
  649. * aborting state. enum sci_status SCI_SUCCESS
  650. */
  651. enum sci_status scic_sds_request_started_state_abort_handler(
  652. struct scic_sds_request *request)
  653. {
  654. if (request->has_started_substate_machine)
  655. sci_base_state_machine_stop(&request->started_substate_machine);
  656. sci_base_state_machine_change_state(&request->state_machine,
  657. SCI_BASE_REQUEST_STATE_ABORTING);
  658. return SCI_SUCCESS;
  659. }
  660. /*
  661. * scic_sds_request_started_state_tc_completion_handler() - This method process
  662. * TC (task context) completions for normal IO request (i.e. Task/Abort
  663. * Completions of type 0). This method will update the
  664. * SCIC_SDS_IO_REQUEST_T::status field.
  665. * @sci_req: This parameter specifies the request for which a completion
  666. * occurred.
  667. * @completion_code: This parameter specifies the completion code received from
  668. * the SCU.
  669. *
  670. */
  671. static enum sci_status
  672. scic_sds_request_started_state_tc_completion_handler(struct scic_sds_request *sci_req,
  673. u32 completion_code)
  674. {
  675. u8 datapres;
  676. struct ssp_response_iu *resp_iu;
  677. /*
  678. * TODO: Any SDMA return code of other than 0 is bad
  679. * decode 0x003C0000 to determine SDMA status
  680. */
  681. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  682. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  683. scic_sds_request_set_status(sci_req,
  684. SCU_TASK_DONE_GOOD,
  685. SCI_SUCCESS);
  686. break;
  687. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP):
  688. {
  689. /*
  690. * There are times when the SCU hardware will return an early
  691. * response because the io request specified more data than is
  692. * returned by the target device (mode pages, inquiry data,
  693. * etc.). We must check the response stats to see if this is
  694. * truly a failed request or a good request that just got
  695. * completed early.
  696. */
  697. struct ssp_response_iu *resp = &sci_req->ssp.rsp;
  698. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  699. sci_swab32_cpy(&sci_req->ssp.rsp,
  700. &sci_req->ssp.rsp,
  701. word_cnt);
  702. if (resp->status == 0) {
  703. scic_sds_request_set_status(
  704. sci_req,
  705. SCU_TASK_DONE_GOOD,
  706. SCI_SUCCESS_IO_DONE_EARLY);
  707. } else {
  708. scic_sds_request_set_status(
  709. sci_req,
  710. SCU_TASK_DONE_CHECK_RESPONSE,
  711. SCI_FAILURE_IO_RESPONSE_VALID);
  712. }
  713. }
  714. break;
  715. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE):
  716. {
  717. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  718. sci_swab32_cpy(&sci_req->ssp.rsp,
  719. &sci_req->ssp.rsp,
  720. word_cnt);
  721. scic_sds_request_set_status(sci_req,
  722. SCU_TASK_DONE_CHECK_RESPONSE,
  723. SCI_FAILURE_IO_RESPONSE_VALID);
  724. break;
  725. }
  726. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
  727. /*
  728. * / @todo With TASK_DONE_RESP_LEN_ERR is the response frame
  729. * guaranteed to be received before this completion status is
  730. * posted?
  731. */
  732. resp_iu = &sci_req->ssp.rsp;
  733. datapres = resp_iu->datapres;
  734. if ((datapres == 0x01) || (datapres == 0x02)) {
  735. scic_sds_request_set_status(
  736. sci_req,
  737. SCU_TASK_DONE_CHECK_RESPONSE,
  738. SCI_FAILURE_IO_RESPONSE_VALID);
  739. } else
  740. scic_sds_request_set_status(
  741. sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS);
  742. break;
  743. /* only stp device gets suspended. */
  744. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  745. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
  746. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
  747. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
  748. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
  749. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
  750. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  751. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
  752. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
  753. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  754. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
  755. if (sci_req->protocol == SCIC_STP_PROTOCOL) {
  756. scic_sds_request_set_status(
  757. sci_req,
  758. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  759. SCU_COMPLETION_TL_STATUS_SHIFT,
  760. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  761. } else {
  762. scic_sds_request_set_status(
  763. sci_req,
  764. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  765. SCU_COMPLETION_TL_STATUS_SHIFT,
  766. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  767. }
  768. break;
  769. /* both stp/ssp device gets suspended */
  770. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
  771. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
  772. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
  773. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
  774. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
  775. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
  776. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
  777. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
  778. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
  779. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
  780. scic_sds_request_set_status(
  781. sci_req,
  782. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  783. SCU_COMPLETION_TL_STATUS_SHIFT,
  784. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  785. break;
  786. /* neither ssp nor stp gets suspended. */
  787. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
  788. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
  789. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
  790. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
  791. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
  792. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
  793. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  794. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  795. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  796. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  797. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
  798. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
  799. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
  800. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
  801. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
  802. default:
  803. scic_sds_request_set_status(
  804. sci_req,
  805. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  806. SCU_COMPLETION_TL_STATUS_SHIFT,
  807. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  808. break;
  809. }
  810. /*
  811. * TODO: This is probably wrong for ACK/NAK timeout conditions
  812. */
  813. /* In all cases we will treat this as the completion of the IO req. */
  814. sci_base_state_machine_change_state(
  815. &sci_req->state_machine,
  816. SCI_BASE_REQUEST_STATE_COMPLETED);
  817. return SCI_SUCCESS;
  818. }
  819. enum sci_status
  820. scic_sds_io_request_tc_completion(struct scic_sds_request *request, u32 completion_code)
  821. {
  822. if (request->state_machine.current_state_id == SCI_BASE_REQUEST_STATE_STARTED &&
  823. request->has_started_substate_machine == false)
  824. return scic_sds_request_started_state_tc_completion_handler(request, completion_code);
  825. else if (request->state_handlers->tc_completion_handler)
  826. return request->state_handlers->tc_completion_handler(request, completion_code);
  827. dev_warn(scic_to_dev(request->owning_controller),
  828. "%s: SCIC IO Request given task completion notification %x "
  829. "while in wrong state %d\n",
  830. __func__,
  831. completion_code,
  832. sci_base_state_machine_get_state(&request->state_machine));
  833. return SCI_FAILURE_INVALID_STATE;
  834. }
  835. /*
  836. * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
  837. * object receives a scic_sds_request_frame_handler() request. This method
  838. * first determines the frame type received. If this is a response frame then
  839. * the response data is copied to the io request response buffer for processing
  840. * at completion time. If the frame type is not a response buffer an error is
  841. * logged. enum sci_status SCI_SUCCESS SCI_FAILURE_INVALID_PARAMETER_VALUE
  842. */
  843. static enum sci_status
  844. scic_sds_request_started_state_frame_handler(struct scic_sds_request *sci_req,
  845. u32 frame_index)
  846. {
  847. enum sci_status status;
  848. u32 *frame_header;
  849. struct ssp_frame_hdr ssp_hdr;
  850. ssize_t word_cnt;
  851. status = scic_sds_unsolicited_frame_control_get_header(
  852. &(scic_sds_request_get_controller(sci_req)->uf_control),
  853. frame_index,
  854. (void **)&frame_header);
  855. word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
  856. sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
  857. if (ssp_hdr.frame_type == SSP_RESPONSE) {
  858. struct ssp_response_iu *resp_iu;
  859. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  860. status = scic_sds_unsolicited_frame_control_get_buffer(
  861. &(scic_sds_request_get_controller(sci_req)->uf_control),
  862. frame_index,
  863. (void **)&resp_iu);
  864. sci_swab32_cpy(&sci_req->ssp.rsp,
  865. resp_iu, word_cnt);
  866. resp_iu = &sci_req->ssp.rsp;
  867. if ((resp_iu->datapres == 0x01) ||
  868. (resp_iu->datapres == 0x02)) {
  869. scic_sds_request_set_status(
  870. sci_req,
  871. SCU_TASK_DONE_CHECK_RESPONSE,
  872. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  873. } else
  874. scic_sds_request_set_status(
  875. sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS);
  876. } else {
  877. /* This was not a response frame why did it get forwarded? */
  878. dev_err(scic_to_dev(sci_req->owning_controller),
  879. "%s: SCIC IO Request 0x%p received unexpected "
  880. "frame %d type 0x%02x\n",
  881. __func__,
  882. sci_req,
  883. frame_index,
  884. ssp_hdr.frame_type);
  885. }
  886. /*
  887. * In any case we are done with this frame buffer return it to the
  888. * controller
  889. */
  890. scic_sds_controller_release_frame(
  891. sci_req->owning_controller, frame_index);
  892. return SCI_SUCCESS;
  893. }
  894. /*
  895. * *****************************************************************************
  896. * * COMPLETED STATE HANDLERS
  897. * ***************************************************************************** */
  898. /*
  899. * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
  900. * object receives a scic_sds_request_complete() request. This method frees up
  901. * any io request resources that have been allocated and transitions the
  902. * request to its final state. Consider stopping the state machine instead of
  903. * transitioning to the final state? enum sci_status SCI_SUCCESS
  904. */
  905. static enum sci_status scic_sds_request_completed_state_complete_handler(
  906. struct scic_sds_request *request)
  907. {
  908. if (request->was_tag_assigned_by_user != true) {
  909. scic_controller_free_io_tag(
  910. request->owning_controller, request->io_tag);
  911. }
  912. if (request->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX) {
  913. scic_sds_controller_release_frame(
  914. request->owning_controller, request->saved_rx_frame_index);
  915. }
  916. sci_base_state_machine_change_state(&request->state_machine,
  917. SCI_BASE_REQUEST_STATE_FINAL);
  918. return SCI_SUCCESS;
  919. }
  920. /*
  921. * *****************************************************************************
  922. * * ABORTING STATE HANDLERS
  923. * ***************************************************************************** */
  924. /*
  925. * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
  926. * object receives a scic_sds_request_terminate() request. This method is the
  927. * io request aborting state abort handlers. On receipt of a multiple
  928. * terminate requests the io request will transition to the completed state.
  929. * This should not happen in normal operation. enum sci_status SCI_SUCCESS
  930. */
  931. static enum sci_status scic_sds_request_aborting_state_abort_handler(
  932. struct scic_sds_request *request)
  933. {
  934. sci_base_state_machine_change_state(&request->state_machine,
  935. SCI_BASE_REQUEST_STATE_COMPLETED);
  936. return SCI_SUCCESS;
  937. }
  938. /*
  939. * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
  940. * object receives a scic_sds_request_task_completion() request. This method
  941. * decodes the completion type waiting for the abort task complete
  942. * notification. When the abort task complete is received the io request
  943. * transitions to the completed state. enum sci_status SCI_SUCCESS
  944. */
  945. static enum sci_status scic_sds_request_aborting_state_tc_completion_handler(
  946. struct scic_sds_request *sci_req,
  947. u32 completion_code)
  948. {
  949. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  950. case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
  951. case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
  952. scic_sds_request_set_status(
  953. sci_req, SCU_TASK_DONE_TASK_ABORT, SCI_FAILURE_IO_TERMINATED
  954. );
  955. sci_base_state_machine_change_state(&sci_req->state_machine,
  956. SCI_BASE_REQUEST_STATE_COMPLETED);
  957. break;
  958. default:
  959. /*
  960. * Unless we get some strange error wait for the task abort to complete
  961. * TODO: Should there be a state change for this completion? */
  962. break;
  963. }
  964. return SCI_SUCCESS;
  965. }
  966. /*
  967. * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
  968. * object receives a scic_sds_request_frame_handler() request. This method
  969. * discards the unsolicited frame since we are waiting for the abort task
  970. * completion. enum sci_status SCI_SUCCESS
  971. */
  972. static enum sci_status scic_sds_request_aborting_state_frame_handler(
  973. struct scic_sds_request *sci_req,
  974. u32 frame_index)
  975. {
  976. /* TODO: Is it even possible to get an unsolicited frame in the aborting state? */
  977. scic_sds_controller_release_frame(
  978. sci_req->owning_controller, frame_index);
  979. return SCI_SUCCESS;
  980. }
  981. /**
  982. * This method processes the completions transport layer (TL) status to
  983. * determine if the RAW task management frame was sent successfully. If the
  984. * raw frame was sent successfully, then the state for the task request
  985. * transitions to waiting for a response frame.
  986. * @sci_req: This parameter specifies the request for which the TC
  987. * completion was received.
  988. * @completion_code: This parameter indicates the completion status information
  989. * for the TC.
  990. *
  991. * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
  992. * this method always returns success.
  993. */
  994. static enum sci_status scic_sds_ssp_task_request_await_tc_completion_tc_completion_handler(
  995. struct scic_sds_request *sci_req,
  996. u32 completion_code)
  997. {
  998. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  999. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1000. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1001. SCI_SUCCESS);
  1002. sci_base_state_machine_change_state(&sci_req->state_machine,
  1003. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE);
  1004. break;
  1005. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  1006. /*
  1007. * Currently, the decision is to simply allow the task request to
  1008. * timeout if the task IU wasn't received successfully.
  1009. * There is a potential for receiving multiple task responses if we
  1010. * decide to send the task IU again. */
  1011. dev_warn(scic_to_dev(sci_req->owning_controller),
  1012. "%s: TaskRequest:0x%p CompletionCode:%x - "
  1013. "ACK/NAK timeout\n",
  1014. __func__,
  1015. sci_req,
  1016. completion_code);
  1017. sci_base_state_machine_change_state(&sci_req->state_machine,
  1018. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE);
  1019. break;
  1020. default:
  1021. /*
  1022. * All other completion status cause the IO to be complete. If a NAK
  1023. * was received, then it is up to the user to retry the request. */
  1024. scic_sds_request_set_status(
  1025. sci_req,
  1026. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1027. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
  1028. );
  1029. sci_base_state_machine_change_state(&sci_req->state_machine,
  1030. SCI_BASE_REQUEST_STATE_COMPLETED);
  1031. break;
  1032. }
  1033. return SCI_SUCCESS;
  1034. }
  1035. /**
  1036. * This method is responsible for processing a terminate/abort request for this
  1037. * TC while the request is waiting for the task management response
  1038. * unsolicited frame.
  1039. * @sci_req: This parameter specifies the request for which the
  1040. * termination was requested.
  1041. *
  1042. * This method returns an indication as to whether the abort request was
  1043. * successfully handled. need to update to ensure the received UF doesn't cause
  1044. * damage to subsequent requests (i.e. put the extended tag in a holding
  1045. * pattern for this particular device).
  1046. */
  1047. static enum sci_status scic_sds_ssp_task_request_await_tc_response_abort_handler(
  1048. struct scic_sds_request *request)
  1049. {
  1050. sci_base_state_machine_change_state(&request->state_machine,
  1051. SCI_BASE_REQUEST_STATE_ABORTING);
  1052. sci_base_state_machine_change_state(&request->state_machine,
  1053. SCI_BASE_REQUEST_STATE_COMPLETED);
  1054. return SCI_SUCCESS;
  1055. }
  1056. /**
  1057. * This method processes an unsolicited frame while the task mgmt request is
  1058. * waiting for a response frame. It will copy the response data, release
  1059. * the unsolicited frame, and transition the request to the
  1060. * SCI_BASE_REQUEST_STATE_COMPLETED state.
  1061. * @sci_req: This parameter specifies the request for which the
  1062. * unsolicited frame was received.
  1063. * @frame_index: This parameter indicates the unsolicited frame index that
  1064. * should contain the response.
  1065. *
  1066. * This method returns an indication of whether the TC response frame was
  1067. * handled successfully or not. SCI_SUCCESS Currently this value is always
  1068. * returned and indicates successful processing of the TC response. Should
  1069. * probably update to check frame type and make sure it is a response frame.
  1070. */
  1071. static enum sci_status scic_sds_ssp_task_request_await_tc_response_frame_handler(
  1072. struct scic_sds_request *request,
  1073. u32 frame_index)
  1074. {
  1075. scic_sds_io_request_copy_response(request);
  1076. sci_base_state_machine_change_state(&request->state_machine,
  1077. SCI_BASE_REQUEST_STATE_COMPLETED);
  1078. scic_sds_controller_release_frame(request->owning_controller,
  1079. frame_index);
  1080. return SCI_SUCCESS;
  1081. }
  1082. static const struct scic_sds_io_request_state_handler scic_sds_request_state_handler_table[] = {
  1083. [SCI_BASE_REQUEST_STATE_INITIAL] = { },
  1084. [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
  1085. .start_handler = scic_sds_request_constructed_state_start_handler,
  1086. .abort_handler = scic_sds_request_constructed_state_abort_handler,
  1087. },
  1088. [SCI_BASE_REQUEST_STATE_STARTED] = {
  1089. .abort_handler = scic_sds_request_started_state_abort_handler,
  1090. .tc_completion_handler = scic_sds_request_started_state_tc_completion_handler,
  1091. .frame_handler = scic_sds_request_started_state_frame_handler,
  1092. },
  1093. [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION] = {
  1094. .abort_handler = scic_sds_request_started_state_abort_handler,
  1095. .tc_completion_handler = scic_sds_ssp_task_request_await_tc_completion_tc_completion_handler,
  1096. },
  1097. [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE] = {
  1098. .abort_handler = scic_sds_ssp_task_request_await_tc_response_abort_handler,
  1099. .frame_handler = scic_sds_ssp_task_request_await_tc_response_frame_handler,
  1100. },
  1101. [SCI_BASE_REQUEST_STATE_COMPLETED] = {
  1102. .complete_handler = scic_sds_request_completed_state_complete_handler,
  1103. },
  1104. [SCI_BASE_REQUEST_STATE_ABORTING] = {
  1105. .abort_handler = scic_sds_request_aborting_state_abort_handler,
  1106. .tc_completion_handler = scic_sds_request_aborting_state_tc_completion_handler,
  1107. .frame_handler = scic_sds_request_aborting_state_frame_handler,
  1108. },
  1109. [SCI_BASE_REQUEST_STATE_FINAL] = { },
  1110. };
  1111. /**
  1112. * isci_request_process_response_iu() - This function sets the status and
  1113. * response iu, in the task struct, from the request object for the upper
  1114. * layer driver.
  1115. * @sas_task: This parameter is the task struct from the upper layer driver.
  1116. * @resp_iu: This parameter points to the response iu of the completed request.
  1117. * @dev: This parameter specifies the linux device struct.
  1118. *
  1119. * none.
  1120. */
  1121. static void isci_request_process_response_iu(
  1122. struct sas_task *task,
  1123. struct ssp_response_iu *resp_iu,
  1124. struct device *dev)
  1125. {
  1126. dev_dbg(dev,
  1127. "%s: resp_iu = %p "
  1128. "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
  1129. "resp_iu->response_data_len = %x, "
  1130. "resp_iu->sense_data_len = %x\nrepsonse data: ",
  1131. __func__,
  1132. resp_iu,
  1133. resp_iu->status,
  1134. resp_iu->datapres,
  1135. resp_iu->response_data_len,
  1136. resp_iu->sense_data_len);
  1137. task->task_status.stat = resp_iu->status;
  1138. /* libsas updates the task status fields based on the response iu. */
  1139. sas_ssp_task_response(dev, task, resp_iu);
  1140. }
  1141. /**
  1142. * isci_request_set_open_reject_status() - This function prepares the I/O
  1143. * completion for OPEN_REJECT conditions.
  1144. * @request: This parameter is the completed isci_request object.
  1145. * @response_ptr: This parameter specifies the service response for the I/O.
  1146. * @status_ptr: This parameter specifies the exec status for the I/O.
  1147. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  1148. * the LLDD with respect to completing this request or forcing an abort
  1149. * condition on the I/O.
  1150. * @open_rej_reason: This parameter specifies the encoded reason for the
  1151. * abandon-class reject.
  1152. *
  1153. * none.
  1154. */
  1155. static void isci_request_set_open_reject_status(
  1156. struct isci_request *request,
  1157. struct sas_task *task,
  1158. enum service_response *response_ptr,
  1159. enum exec_status *status_ptr,
  1160. enum isci_completion_selection *complete_to_host_ptr,
  1161. enum sas_open_rej_reason open_rej_reason)
  1162. {
  1163. /* Task in the target is done. */
  1164. request->complete_in_target = true;
  1165. *response_ptr = SAS_TASK_UNDELIVERED;
  1166. *status_ptr = SAS_OPEN_REJECT;
  1167. *complete_to_host_ptr = isci_perform_normal_io_completion;
  1168. task->task_status.open_rej_reason = open_rej_reason;
  1169. }
  1170. /**
  1171. * isci_request_handle_controller_specific_errors() - This function decodes
  1172. * controller-specific I/O completion error conditions.
  1173. * @request: This parameter is the completed isci_request object.
  1174. * @response_ptr: This parameter specifies the service response for the I/O.
  1175. * @status_ptr: This parameter specifies the exec status for the I/O.
  1176. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  1177. * the LLDD with respect to completing this request or forcing an abort
  1178. * condition on the I/O.
  1179. *
  1180. * none.
  1181. */
  1182. static void isci_request_handle_controller_specific_errors(
  1183. struct isci_remote_device *isci_device,
  1184. struct isci_request *request,
  1185. struct sas_task *task,
  1186. enum service_response *response_ptr,
  1187. enum exec_status *status_ptr,
  1188. enum isci_completion_selection *complete_to_host_ptr)
  1189. {
  1190. unsigned int cstatus;
  1191. cstatus = request->sci.scu_status;
  1192. dev_dbg(&request->isci_host->pdev->dev,
  1193. "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
  1194. "- controller status = 0x%x\n",
  1195. __func__, request, cstatus);
  1196. /* Decode the controller-specific errors; most
  1197. * important is to recognize those conditions in which
  1198. * the target may still have a task outstanding that
  1199. * must be aborted.
  1200. *
  1201. * Note that there are SCU completion codes being
  1202. * named in the decode below for which SCIC has already
  1203. * done work to handle them in a way other than as
  1204. * a controller-specific completion code; these are left
  1205. * in the decode below for completeness sake.
  1206. */
  1207. switch (cstatus) {
  1208. case SCU_TASK_DONE_DMASETUP_DIRERR:
  1209. /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
  1210. case SCU_TASK_DONE_XFERCNT_ERR:
  1211. /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
  1212. if (task->task_proto == SAS_PROTOCOL_SMP) {
  1213. /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
  1214. *response_ptr = SAS_TASK_COMPLETE;
  1215. /* See if the device has been/is being stopped. Note
  1216. * that we ignore the quiesce state, since we are
  1217. * concerned about the actual device state.
  1218. */
  1219. if ((isci_device->status == isci_stopping) ||
  1220. (isci_device->status == isci_stopped))
  1221. *status_ptr = SAS_DEVICE_UNKNOWN;
  1222. else
  1223. *status_ptr = SAS_ABORTED_TASK;
  1224. request->complete_in_target = true;
  1225. *complete_to_host_ptr =
  1226. isci_perform_normal_io_completion;
  1227. } else {
  1228. /* Task in the target is not done. */
  1229. *response_ptr = SAS_TASK_UNDELIVERED;
  1230. if ((isci_device->status == isci_stopping) ||
  1231. (isci_device->status == isci_stopped))
  1232. *status_ptr = SAS_DEVICE_UNKNOWN;
  1233. else
  1234. *status_ptr = SAM_STAT_TASK_ABORTED;
  1235. request->complete_in_target = false;
  1236. *complete_to_host_ptr =
  1237. isci_perform_error_io_completion;
  1238. }
  1239. break;
  1240. case SCU_TASK_DONE_CRC_ERR:
  1241. case SCU_TASK_DONE_NAK_CMD_ERR:
  1242. case SCU_TASK_DONE_EXCESS_DATA:
  1243. case SCU_TASK_DONE_UNEXP_FIS:
  1244. /* Also SCU_TASK_DONE_UNEXP_RESP: */
  1245. case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
  1246. case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
  1247. case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
  1248. /* These are conditions in which the target
  1249. * has completed the task, so that no cleanup
  1250. * is necessary.
  1251. */
  1252. *response_ptr = SAS_TASK_COMPLETE;
  1253. /* See if the device has been/is being stopped. Note
  1254. * that we ignore the quiesce state, since we are
  1255. * concerned about the actual device state.
  1256. */
  1257. if ((isci_device->status == isci_stopping) ||
  1258. (isci_device->status == isci_stopped))
  1259. *status_ptr = SAS_DEVICE_UNKNOWN;
  1260. else
  1261. *status_ptr = SAS_ABORTED_TASK;
  1262. request->complete_in_target = true;
  1263. *complete_to_host_ptr = isci_perform_normal_io_completion;
  1264. break;
  1265. /* Note that the only open reject completion codes seen here will be
  1266. * abandon-class codes; all others are automatically retried in the SCU.
  1267. */
  1268. case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
  1269. isci_request_set_open_reject_status(
  1270. request, task, response_ptr, status_ptr,
  1271. complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
  1272. break;
  1273. case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
  1274. /* Note - the return of AB0 will change when
  1275. * libsas implements detection of zone violations.
  1276. */
  1277. isci_request_set_open_reject_status(
  1278. request, task, response_ptr, status_ptr,
  1279. complete_to_host_ptr, SAS_OREJ_RESV_AB0);
  1280. break;
  1281. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
  1282. isci_request_set_open_reject_status(
  1283. request, task, response_ptr, status_ptr,
  1284. complete_to_host_ptr, SAS_OREJ_RESV_AB1);
  1285. break;
  1286. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
  1287. isci_request_set_open_reject_status(
  1288. request, task, response_ptr, status_ptr,
  1289. complete_to_host_ptr, SAS_OREJ_RESV_AB2);
  1290. break;
  1291. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
  1292. isci_request_set_open_reject_status(
  1293. request, task, response_ptr, status_ptr,
  1294. complete_to_host_ptr, SAS_OREJ_RESV_AB3);
  1295. break;
  1296. case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
  1297. isci_request_set_open_reject_status(
  1298. request, task, response_ptr, status_ptr,
  1299. complete_to_host_ptr, SAS_OREJ_BAD_DEST);
  1300. break;
  1301. case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
  1302. isci_request_set_open_reject_status(
  1303. request, task, response_ptr, status_ptr,
  1304. complete_to_host_ptr, SAS_OREJ_STP_NORES);
  1305. break;
  1306. case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
  1307. isci_request_set_open_reject_status(
  1308. request, task, response_ptr, status_ptr,
  1309. complete_to_host_ptr, SAS_OREJ_EPROTO);
  1310. break;
  1311. case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
  1312. isci_request_set_open_reject_status(
  1313. request, task, response_ptr, status_ptr,
  1314. complete_to_host_ptr, SAS_OREJ_CONN_RATE);
  1315. break;
  1316. case SCU_TASK_DONE_LL_R_ERR:
  1317. /* Also SCU_TASK_DONE_ACK_NAK_TO: */
  1318. case SCU_TASK_DONE_LL_PERR:
  1319. case SCU_TASK_DONE_LL_SY_TERM:
  1320. /* Also SCU_TASK_DONE_NAK_ERR:*/
  1321. case SCU_TASK_DONE_LL_LF_TERM:
  1322. /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
  1323. case SCU_TASK_DONE_LL_ABORT_ERR:
  1324. case SCU_TASK_DONE_SEQ_INV_TYPE:
  1325. /* Also SCU_TASK_DONE_UNEXP_XR: */
  1326. case SCU_TASK_DONE_XR_IU_LEN_ERR:
  1327. case SCU_TASK_DONE_INV_FIS_LEN:
  1328. /* Also SCU_TASK_DONE_XR_WD_LEN: */
  1329. case SCU_TASK_DONE_SDMA_ERR:
  1330. case SCU_TASK_DONE_OFFSET_ERR:
  1331. case SCU_TASK_DONE_MAX_PLD_ERR:
  1332. case SCU_TASK_DONE_LF_ERR:
  1333. case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
  1334. case SCU_TASK_DONE_SMP_LL_RX_ERR:
  1335. case SCU_TASK_DONE_UNEXP_DATA:
  1336. case SCU_TASK_DONE_UNEXP_SDBFIS:
  1337. case SCU_TASK_DONE_REG_ERR:
  1338. case SCU_TASK_DONE_SDB_ERR:
  1339. case SCU_TASK_DONE_TASK_ABORT:
  1340. default:
  1341. /* Task in the target is not done. */
  1342. *response_ptr = SAS_TASK_UNDELIVERED;
  1343. *status_ptr = SAM_STAT_TASK_ABORTED;
  1344. request->complete_in_target = false;
  1345. *complete_to_host_ptr = isci_perform_error_io_completion;
  1346. break;
  1347. }
  1348. }
  1349. /**
  1350. * isci_task_save_for_upper_layer_completion() - This function saves the
  1351. * request for later completion to the upper layer driver.
  1352. * @host: This parameter is a pointer to the host on which the the request
  1353. * should be queued (either as an error or success).
  1354. * @request: This parameter is the completed request.
  1355. * @response: This parameter is the response code for the completed task.
  1356. * @status: This parameter is the status code for the completed task.
  1357. *
  1358. * none.
  1359. */
  1360. static void isci_task_save_for_upper_layer_completion(
  1361. struct isci_host *host,
  1362. struct isci_request *request,
  1363. enum service_response response,
  1364. enum exec_status status,
  1365. enum isci_completion_selection task_notification_selection)
  1366. {
  1367. struct sas_task *task = isci_request_access_task(request);
  1368. task_notification_selection
  1369. = isci_task_set_completion_status(task, response, status,
  1370. task_notification_selection);
  1371. /* Tasks aborted specifically by a call to the lldd_abort_task
  1372. * function should not be completed to the host in the regular path.
  1373. */
  1374. switch (task_notification_selection) {
  1375. case isci_perform_normal_io_completion:
  1376. /* Normal notification (task_done) */
  1377. dev_dbg(&host->pdev->dev,
  1378. "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
  1379. __func__,
  1380. task,
  1381. task->task_status.resp, response,
  1382. task->task_status.stat, status);
  1383. /* Add to the completed list. */
  1384. list_add(&request->completed_node,
  1385. &host->requests_to_complete);
  1386. /* Take the request off the device's pending request list. */
  1387. list_del_init(&request->dev_node);
  1388. break;
  1389. case isci_perform_aborted_io_completion:
  1390. /* No notification to libsas because this request is
  1391. * already in the abort path.
  1392. */
  1393. dev_warn(&host->pdev->dev,
  1394. "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
  1395. __func__,
  1396. task,
  1397. task->task_status.resp, response,
  1398. task->task_status.stat, status);
  1399. /* Wake up whatever process was waiting for this
  1400. * request to complete.
  1401. */
  1402. WARN_ON(request->io_request_completion == NULL);
  1403. if (request->io_request_completion != NULL) {
  1404. /* Signal whoever is waiting that this
  1405. * request is complete.
  1406. */
  1407. complete(request->io_request_completion);
  1408. }
  1409. break;
  1410. case isci_perform_error_io_completion:
  1411. /* Use sas_task_abort */
  1412. dev_warn(&host->pdev->dev,
  1413. "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
  1414. __func__,
  1415. task,
  1416. task->task_status.resp, response,
  1417. task->task_status.stat, status);
  1418. /* Add to the aborted list. */
  1419. list_add(&request->completed_node,
  1420. &host->requests_to_errorback);
  1421. break;
  1422. default:
  1423. dev_warn(&host->pdev->dev,
  1424. "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
  1425. __func__,
  1426. task,
  1427. task->task_status.resp, response,
  1428. task->task_status.stat, status);
  1429. /* Add to the error to libsas list. */
  1430. list_add(&request->completed_node,
  1431. &host->requests_to_errorback);
  1432. break;
  1433. }
  1434. }
  1435. static void isci_request_io_request_complete(struct isci_host *isci_host,
  1436. struct isci_request *request,
  1437. enum sci_io_status completion_status)
  1438. {
  1439. struct sas_task *task = isci_request_access_task(request);
  1440. struct ssp_response_iu *resp_iu;
  1441. void *resp_buf;
  1442. unsigned long task_flags;
  1443. struct isci_remote_device *isci_device = request->isci_device;
  1444. enum service_response response = SAS_TASK_UNDELIVERED;
  1445. enum exec_status status = SAS_ABORTED_TASK;
  1446. enum isci_request_status request_status;
  1447. enum isci_completion_selection complete_to_host
  1448. = isci_perform_normal_io_completion;
  1449. dev_dbg(&isci_host->pdev->dev,
  1450. "%s: request = %p, task = %p,\n"
  1451. "task->data_dir = %d completion_status = 0x%x\n",
  1452. __func__,
  1453. request,
  1454. task,
  1455. task->data_dir,
  1456. completion_status);
  1457. spin_lock(&request->state_lock);
  1458. request_status = isci_request_get_state(request);
  1459. /* Decode the request status. Note that if the request has been
  1460. * aborted by a task management function, we don't care
  1461. * what the status is.
  1462. */
  1463. switch (request_status) {
  1464. case aborted:
  1465. /* "aborted" indicates that the request was aborted by a task
  1466. * management function, since once a task management request is
  1467. * perfomed by the device, the request only completes because
  1468. * of the subsequent driver terminate.
  1469. *
  1470. * Aborted also means an external thread is explicitly managing
  1471. * this request, so that we do not complete it up the stack.
  1472. *
  1473. * The target is still there (since the TMF was successful).
  1474. */
  1475. request->complete_in_target = true;
  1476. response = SAS_TASK_COMPLETE;
  1477. /* See if the device has been/is being stopped. Note
  1478. * that we ignore the quiesce state, since we are
  1479. * concerned about the actual device state.
  1480. */
  1481. if ((isci_device->status == isci_stopping)
  1482. || (isci_device->status == isci_stopped)
  1483. )
  1484. status = SAS_DEVICE_UNKNOWN;
  1485. else
  1486. status = SAS_ABORTED_TASK;
  1487. complete_to_host = isci_perform_aborted_io_completion;
  1488. /* This was an aborted request. */
  1489. spin_unlock(&request->state_lock);
  1490. break;
  1491. case aborting:
  1492. /* aborting means that the task management function tried and
  1493. * failed to abort the request. We need to note the request
  1494. * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
  1495. * target as down.
  1496. *
  1497. * Aborting also means an external thread is explicitly managing
  1498. * this request, so that we do not complete it up the stack.
  1499. */
  1500. request->complete_in_target = true;
  1501. response = SAS_TASK_UNDELIVERED;
  1502. if ((isci_device->status == isci_stopping) ||
  1503. (isci_device->status == isci_stopped))
  1504. /* The device has been /is being stopped. Note that
  1505. * we ignore the quiesce state, since we are
  1506. * concerned about the actual device state.
  1507. */
  1508. status = SAS_DEVICE_UNKNOWN;
  1509. else
  1510. status = SAS_PHY_DOWN;
  1511. complete_to_host = isci_perform_aborted_io_completion;
  1512. /* This was an aborted request. */
  1513. spin_unlock(&request->state_lock);
  1514. break;
  1515. case terminating:
  1516. /* This was an terminated request. This happens when
  1517. * the I/O is being terminated because of an action on
  1518. * the device (reset, tear down, etc.), and the I/O needs
  1519. * to be completed up the stack.
  1520. */
  1521. request->complete_in_target = true;
  1522. response = SAS_TASK_UNDELIVERED;
  1523. /* See if the device has been/is being stopped. Note
  1524. * that we ignore the quiesce state, since we are
  1525. * concerned about the actual device state.
  1526. */
  1527. if ((isci_device->status == isci_stopping) ||
  1528. (isci_device->status == isci_stopped))
  1529. status = SAS_DEVICE_UNKNOWN;
  1530. else
  1531. status = SAS_ABORTED_TASK;
  1532. complete_to_host = isci_perform_aborted_io_completion;
  1533. /* This was a terminated request. */
  1534. spin_unlock(&request->state_lock);
  1535. break;
  1536. default:
  1537. /* The request is done from an SCU HW perspective. */
  1538. request->status = completed;
  1539. spin_unlock(&request->state_lock);
  1540. /* This is an active request being completed from the core. */
  1541. switch (completion_status) {
  1542. case SCI_IO_FAILURE_RESPONSE_VALID:
  1543. dev_dbg(&isci_host->pdev->dev,
  1544. "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
  1545. __func__,
  1546. request,
  1547. task);
  1548. if (sas_protocol_ata(task->task_proto)) {
  1549. resp_buf = &request->sci.stp.rsp;
  1550. isci_request_process_stp_response(task,
  1551. resp_buf);
  1552. } else if (SAS_PROTOCOL_SSP == task->task_proto) {
  1553. /* crack the iu response buffer. */
  1554. resp_iu = &request->sci.ssp.rsp;
  1555. isci_request_process_response_iu(task, resp_iu,
  1556. &isci_host->pdev->dev);
  1557. } else if (SAS_PROTOCOL_SMP == task->task_proto) {
  1558. dev_err(&isci_host->pdev->dev,
  1559. "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
  1560. "SAS_PROTOCOL_SMP protocol\n",
  1561. __func__);
  1562. } else
  1563. dev_err(&isci_host->pdev->dev,
  1564. "%s: unknown protocol\n", __func__);
  1565. /* use the task status set in the task struct by the
  1566. * isci_request_process_response_iu call.
  1567. */
  1568. request->complete_in_target = true;
  1569. response = task->task_status.resp;
  1570. status = task->task_status.stat;
  1571. break;
  1572. case SCI_IO_SUCCESS:
  1573. case SCI_IO_SUCCESS_IO_DONE_EARLY:
  1574. response = SAS_TASK_COMPLETE;
  1575. status = SAM_STAT_GOOD;
  1576. request->complete_in_target = true;
  1577. if (task->task_proto == SAS_PROTOCOL_SMP) {
  1578. void *rsp = &request->sci.smp.rsp;
  1579. dev_dbg(&isci_host->pdev->dev,
  1580. "%s: SMP protocol completion\n",
  1581. __func__);
  1582. sg_copy_from_buffer(
  1583. &task->smp_task.smp_resp, 1,
  1584. rsp, sizeof(struct smp_resp));
  1585. } else if (completion_status
  1586. == SCI_IO_SUCCESS_IO_DONE_EARLY) {
  1587. /* This was an SSP / STP / SATA transfer.
  1588. * There is a possibility that less data than
  1589. * the maximum was transferred.
  1590. */
  1591. u32 transferred_length = sci_req_tx_bytes(&request->sci);
  1592. task->task_status.residual
  1593. = task->total_xfer_len - transferred_length;
  1594. /* If there were residual bytes, call this an
  1595. * underrun.
  1596. */
  1597. if (task->task_status.residual != 0)
  1598. status = SAS_DATA_UNDERRUN;
  1599. dev_dbg(&isci_host->pdev->dev,
  1600. "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
  1601. __func__,
  1602. status);
  1603. } else
  1604. dev_dbg(&isci_host->pdev->dev,
  1605. "%s: SCI_IO_SUCCESS\n",
  1606. __func__);
  1607. break;
  1608. case SCI_IO_FAILURE_TERMINATED:
  1609. dev_dbg(&isci_host->pdev->dev,
  1610. "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
  1611. __func__,
  1612. request,
  1613. task);
  1614. /* The request was terminated explicitly. No handling
  1615. * is needed in the SCSI error handler path.
  1616. */
  1617. request->complete_in_target = true;
  1618. response = SAS_TASK_UNDELIVERED;
  1619. /* See if the device has been/is being stopped. Note
  1620. * that we ignore the quiesce state, since we are
  1621. * concerned about the actual device state.
  1622. */
  1623. if ((isci_device->status == isci_stopping) ||
  1624. (isci_device->status == isci_stopped))
  1625. status = SAS_DEVICE_UNKNOWN;
  1626. else
  1627. status = SAS_ABORTED_TASK;
  1628. complete_to_host = isci_perform_normal_io_completion;
  1629. break;
  1630. case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
  1631. isci_request_handle_controller_specific_errors(
  1632. isci_device, request, task, &response, &status,
  1633. &complete_to_host);
  1634. break;
  1635. case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
  1636. /* This is a special case, in that the I/O completion
  1637. * is telling us that the device needs a reset.
  1638. * In order for the device reset condition to be
  1639. * noticed, the I/O has to be handled in the error
  1640. * handler. Set the reset flag and cause the
  1641. * SCSI error thread to be scheduled.
  1642. */
  1643. spin_lock_irqsave(&task->task_state_lock, task_flags);
  1644. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  1645. spin_unlock_irqrestore(&task->task_state_lock, task_flags);
  1646. /* Fail the I/O. */
  1647. response = SAS_TASK_UNDELIVERED;
  1648. status = SAM_STAT_TASK_ABORTED;
  1649. complete_to_host = isci_perform_error_io_completion;
  1650. request->complete_in_target = false;
  1651. break;
  1652. default:
  1653. /* Catch any otherwise unhandled error codes here. */
  1654. dev_warn(&isci_host->pdev->dev,
  1655. "%s: invalid completion code: 0x%x - "
  1656. "isci_request = %p\n",
  1657. __func__, completion_status, request);
  1658. response = SAS_TASK_UNDELIVERED;
  1659. /* See if the device has been/is being stopped. Note
  1660. * that we ignore the quiesce state, since we are
  1661. * concerned about the actual device state.
  1662. */
  1663. if ((isci_device->status == isci_stopping) ||
  1664. (isci_device->status == isci_stopped))
  1665. status = SAS_DEVICE_UNKNOWN;
  1666. else
  1667. status = SAS_ABORTED_TASK;
  1668. complete_to_host = isci_perform_error_io_completion;
  1669. request->complete_in_target = false;
  1670. break;
  1671. }
  1672. break;
  1673. }
  1674. isci_request_unmap_sgl(request, isci_host->pdev);
  1675. /* Put the completed request on the correct list */
  1676. isci_task_save_for_upper_layer_completion(isci_host, request, response,
  1677. status, complete_to_host
  1678. );
  1679. /* complete the io request to the core. */
  1680. scic_controller_complete_io(&isci_host->sci,
  1681. &isci_device->sci,
  1682. &request->sci);
  1683. /* set terminated handle so it cannot be completed or
  1684. * terminated again, and to cause any calls into abort
  1685. * task to recognize the already completed case.
  1686. */
  1687. request->terminated = true;
  1688. isci_host_can_dequeue(isci_host, 1);
  1689. }
  1690. /**
  1691. * scic_sds_request_initial_state_enter() -
  1692. * @object: This parameter specifies the base object for which the state
  1693. * transition is occurring.
  1694. *
  1695. * This method implements the actions taken when entering the
  1696. * SCI_BASE_REQUEST_STATE_INITIAL state. This state is entered when the initial
  1697. * base request is constructed. Entry into the initial state sets all handlers
  1698. * for the io request object to their default handlers. none
  1699. */
  1700. static void scic_sds_request_initial_state_enter(void *object)
  1701. {
  1702. struct scic_sds_request *sci_req = object;
  1703. SET_STATE_HANDLER(
  1704. sci_req,
  1705. scic_sds_request_state_handler_table,
  1706. SCI_BASE_REQUEST_STATE_INITIAL
  1707. );
  1708. }
  1709. /**
  1710. * scic_sds_request_constructed_state_enter() -
  1711. * @object: The io request object that is to enter the constructed state.
  1712. *
  1713. * This method implements the actions taken when entering the
  1714. * SCI_BASE_REQUEST_STATE_CONSTRUCTED state. The method sets the state handlers
  1715. * for the the constructed state. none
  1716. */
  1717. static void scic_sds_request_constructed_state_enter(void *object)
  1718. {
  1719. struct scic_sds_request *sci_req = object;
  1720. SET_STATE_HANDLER(
  1721. sci_req,
  1722. scic_sds_request_state_handler_table,
  1723. SCI_BASE_REQUEST_STATE_CONSTRUCTED
  1724. );
  1725. }
  1726. /**
  1727. * scic_sds_request_started_state_enter() -
  1728. * @object: This parameter specifies the base object for which the state
  1729. * transition is occurring. This is cast into a SCIC_SDS_IO_REQUEST object.
  1730. *
  1731. * This method implements the actions taken when entering the
  1732. * SCI_BASE_REQUEST_STATE_STARTED state. If the io request object type is a
  1733. * SCSI Task request we must enter the started substate machine. none
  1734. */
  1735. static void scic_sds_request_started_state_enter(void *object)
  1736. {
  1737. struct scic_sds_request *sci_req = object;
  1738. struct sci_base_state_machine *sm = &sci_req->state_machine;
  1739. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  1740. struct domain_device *dev = sci_dev_to_domain(sci_req->target_device);
  1741. SET_STATE_HANDLER(
  1742. sci_req,
  1743. scic_sds_request_state_handler_table,
  1744. SCI_BASE_REQUEST_STATE_STARTED
  1745. );
  1746. if (ireq->ttype == tmf_task && dev->dev_type == SAS_END_DEV)
  1747. sci_base_state_machine_change_state(sm,
  1748. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION);
  1749. /* Most of the request state machines have a started substate machine so
  1750. * start its execution on the entry to the started state.
  1751. */
  1752. if (sci_req->has_started_substate_machine == true)
  1753. sci_base_state_machine_start(&sci_req->started_substate_machine);
  1754. }
  1755. /**
  1756. * scic_sds_request_started_state_exit() -
  1757. * @object: This parameter specifies the base object for which the state
  1758. * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
  1759. * object.
  1760. *
  1761. * This method implements the actions taken when exiting the
  1762. * SCI_BASE_REQUEST_STATE_STARTED state. For task requests the action will be
  1763. * to stop the started substate machine. none
  1764. */
  1765. static void scic_sds_request_started_state_exit(void *object)
  1766. {
  1767. struct scic_sds_request *sci_req = object;
  1768. if (sci_req->has_started_substate_machine == true)
  1769. sci_base_state_machine_stop(&sci_req->started_substate_machine);
  1770. }
  1771. /**
  1772. * scic_sds_request_completed_state_enter() -
  1773. * @object: This parameter specifies the base object for which the state
  1774. * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
  1775. * object.
  1776. *
  1777. * This method implements the actions taken when entering the
  1778. * SCI_BASE_REQUEST_STATE_COMPLETED state. This state is entered when the
  1779. * SCIC_SDS_IO_REQUEST has completed. The method will decode the request
  1780. * completion status and convert it to an enum sci_status to return in the
  1781. * completion callback function. none
  1782. */
  1783. static void scic_sds_request_completed_state_enter(void *object)
  1784. {
  1785. struct scic_sds_request *sci_req = object;
  1786. struct scic_sds_controller *scic =
  1787. scic_sds_request_get_controller(sci_req);
  1788. struct isci_host *ihost = scic_to_ihost(scic);
  1789. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  1790. SET_STATE_HANDLER(sci_req,
  1791. scic_sds_request_state_handler_table,
  1792. SCI_BASE_REQUEST_STATE_COMPLETED);
  1793. /* Tell the SCI_USER that the IO request is complete */
  1794. if (sci_req->is_task_management_request == false)
  1795. isci_request_io_request_complete(ihost, ireq,
  1796. sci_req->sci_status);
  1797. else
  1798. isci_task_request_complete(ihost, ireq, sci_req->sci_status);
  1799. }
  1800. /**
  1801. * scic_sds_request_aborting_state_enter() -
  1802. * @object: This parameter specifies the base object for which the state
  1803. * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
  1804. * object.
  1805. *
  1806. * This method implements the actions taken when entering the
  1807. * SCI_BASE_REQUEST_STATE_ABORTING state. none
  1808. */
  1809. static void scic_sds_request_aborting_state_enter(void *object)
  1810. {
  1811. struct scic_sds_request *sci_req = object;
  1812. /* Setting the abort bit in the Task Context is required by the silicon. */
  1813. sci_req->task_context_buffer->abort = 1;
  1814. SET_STATE_HANDLER(
  1815. sci_req,
  1816. scic_sds_request_state_handler_table,
  1817. SCI_BASE_REQUEST_STATE_ABORTING
  1818. );
  1819. }
  1820. /**
  1821. * scic_sds_request_final_state_enter() -
  1822. * @object: This parameter specifies the base object for which the state
  1823. * transition is occurring. This is cast into a SCIC_SDS_IO_REQUEST object.
  1824. *
  1825. * This method implements the actions taken when entering the
  1826. * SCI_BASE_REQUEST_STATE_FINAL state. The only action required is to put the
  1827. * state handlers in place. none
  1828. */
  1829. static void scic_sds_request_final_state_enter(void *object)
  1830. {
  1831. struct scic_sds_request *sci_req = object;
  1832. SET_STATE_HANDLER(
  1833. sci_req,
  1834. scic_sds_request_state_handler_table,
  1835. SCI_BASE_REQUEST_STATE_FINAL
  1836. );
  1837. }
  1838. static void scic_sds_io_request_started_task_mgmt_await_tc_completion_substate_enter(
  1839. void *object)
  1840. {
  1841. struct scic_sds_request *sci_req = object;
  1842. SET_STATE_HANDLER(
  1843. sci_req,
  1844. scic_sds_request_state_handler_table,
  1845. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION
  1846. );
  1847. }
  1848. static void scic_sds_io_request_started_task_mgmt_await_task_response_substate_enter(
  1849. void *object)
  1850. {
  1851. struct scic_sds_request *sci_req = object;
  1852. SET_STATE_HANDLER(
  1853. sci_req,
  1854. scic_sds_request_state_handler_table,
  1855. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE
  1856. );
  1857. }
  1858. static const struct sci_base_state scic_sds_request_state_table[] = {
  1859. [SCI_BASE_REQUEST_STATE_INITIAL] = {
  1860. .enter_state = scic_sds_request_initial_state_enter,
  1861. },
  1862. [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
  1863. .enter_state = scic_sds_request_constructed_state_enter,
  1864. },
  1865. [SCI_BASE_REQUEST_STATE_STARTED] = {
  1866. .enter_state = scic_sds_request_started_state_enter,
  1867. .exit_state = scic_sds_request_started_state_exit
  1868. },
  1869. [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION] = {
  1870. .enter_state = scic_sds_io_request_started_task_mgmt_await_tc_completion_substate_enter,
  1871. },
  1872. [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE] = {
  1873. .enter_state = scic_sds_io_request_started_task_mgmt_await_task_response_substate_enter,
  1874. },
  1875. [SCI_BASE_REQUEST_STATE_COMPLETED] = {
  1876. .enter_state = scic_sds_request_completed_state_enter,
  1877. },
  1878. [SCI_BASE_REQUEST_STATE_ABORTING] = {
  1879. .enter_state = scic_sds_request_aborting_state_enter,
  1880. },
  1881. [SCI_BASE_REQUEST_STATE_FINAL] = {
  1882. .enter_state = scic_sds_request_final_state_enter,
  1883. },
  1884. };
  1885. static void scic_sds_general_request_construct(struct scic_sds_controller *scic,
  1886. struct scic_sds_remote_device *sci_dev,
  1887. u16 io_tag, struct scic_sds_request *sci_req)
  1888. {
  1889. sci_base_state_machine_construct(&sci_req->state_machine, sci_req,
  1890. scic_sds_request_state_table, SCI_BASE_REQUEST_STATE_INITIAL);
  1891. sci_base_state_machine_start(&sci_req->state_machine);
  1892. sci_req->io_tag = io_tag;
  1893. sci_req->owning_controller = scic;
  1894. sci_req->target_device = sci_dev;
  1895. sci_req->has_started_substate_machine = false;
  1896. sci_req->protocol = SCIC_NO_PROTOCOL;
  1897. sci_req->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
  1898. sci_req->device_sequence = scic_sds_remote_device_get_sequence(sci_dev);
  1899. sci_req->sci_status = SCI_SUCCESS;
  1900. sci_req->scu_status = 0;
  1901. sci_req->post_context = 0xFFFFFFFF;
  1902. sci_req->is_task_management_request = false;
  1903. if (io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  1904. sci_req->was_tag_assigned_by_user = false;
  1905. sci_req->task_context_buffer = NULL;
  1906. } else {
  1907. sci_req->was_tag_assigned_by_user = true;
  1908. sci_req->task_context_buffer =
  1909. scic_sds_controller_get_task_context_buffer(scic, io_tag);
  1910. }
  1911. }
  1912. static enum sci_status
  1913. scic_io_request_construct(struct scic_sds_controller *scic,
  1914. struct scic_sds_remote_device *sci_dev,
  1915. u16 io_tag, struct scic_sds_request *sci_req)
  1916. {
  1917. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1918. enum sci_status status = SCI_SUCCESS;
  1919. /* Build the common part of the request */
  1920. scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
  1921. if (sci_dev->rnc.remote_node_index ==
  1922. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
  1923. return SCI_FAILURE_INVALID_REMOTE_DEVICE;
  1924. if (dev->dev_type == SAS_END_DEV)
  1925. scic_sds_ssp_io_request_assign_buffers(sci_req);
  1926. else if ((dev->dev_type == SATA_DEV) ||
  1927. (dev->tproto & SAS_PROTOCOL_STP)) {
  1928. scic_sds_stp_request_assign_buffers(sci_req);
  1929. memset(&sci_req->stp.cmd, 0, sizeof(sci_req->stp.cmd));
  1930. } else if (dev_is_expander(dev)) {
  1931. scic_sds_smp_request_assign_buffers(sci_req);
  1932. memset(&sci_req->smp.cmd, 0, sizeof(sci_req->smp.cmd));
  1933. } else
  1934. status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1935. if (status == SCI_SUCCESS) {
  1936. memset(sci_req->task_context_buffer, 0,
  1937. offsetof(struct scu_task_context, sgl_pair_ab));
  1938. }
  1939. return status;
  1940. }
  1941. enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
  1942. struct scic_sds_remote_device *sci_dev,
  1943. u16 io_tag, struct scic_sds_request *sci_req)
  1944. {
  1945. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1946. enum sci_status status = SCI_SUCCESS;
  1947. /* Build the common part of the request */
  1948. scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
  1949. if (dev->dev_type == SAS_END_DEV)
  1950. scic_sds_ssp_task_request_assign_buffers(sci_req);
  1951. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  1952. scic_sds_stp_request_assign_buffers(sci_req);
  1953. else
  1954. status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1955. if (status == SCI_SUCCESS) {
  1956. sci_req->is_task_management_request = true;
  1957. memset(sci_req->task_context_buffer, 0, sizeof(struct scu_task_context));
  1958. }
  1959. return status;
  1960. }
  1961. static enum sci_status isci_request_ssp_request_construct(
  1962. struct isci_request *request)
  1963. {
  1964. enum sci_status status;
  1965. dev_dbg(&request->isci_host->pdev->dev,
  1966. "%s: request = %p\n",
  1967. __func__,
  1968. request);
  1969. status = scic_io_request_construct_basic_ssp(&request->sci);
  1970. return status;
  1971. }
  1972. static enum sci_status isci_request_stp_request_construct(
  1973. struct isci_request *request)
  1974. {
  1975. struct sas_task *task = isci_request_access_task(request);
  1976. enum sci_status status;
  1977. struct host_to_dev_fis *register_fis;
  1978. dev_dbg(&request->isci_host->pdev->dev,
  1979. "%s: request = %p\n",
  1980. __func__,
  1981. request);
  1982. /* Get the host_to_dev_fis from the core and copy
  1983. * the fis from the task into it.
  1984. */
  1985. register_fis = isci_sata_task_to_fis_copy(task);
  1986. status = scic_io_request_construct_basic_sata(&request->sci);
  1987. /* Set the ncq tag in the fis, from the queue
  1988. * command in the task.
  1989. */
  1990. if (isci_sata_is_task_ncq(task)) {
  1991. isci_sata_set_ncq_tag(
  1992. register_fis,
  1993. task
  1994. );
  1995. }
  1996. return status;
  1997. }
  1998. /*
  1999. * isci_smp_request_build() - This function builds the smp request.
  2000. * @ireq: This parameter points to the isci_request allocated in the
  2001. * request construct function.
  2002. *
  2003. * SCI_SUCCESS on successfull completion, or specific failure code.
  2004. */
  2005. static enum sci_status isci_smp_request_build(struct isci_request *ireq)
  2006. {
  2007. enum sci_status status = SCI_FAILURE;
  2008. struct sas_task *task = isci_request_access_task(ireq);
  2009. struct scic_sds_request *sci_req = &ireq->sci;
  2010. dev_dbg(&ireq->isci_host->pdev->dev,
  2011. "%s: request = %p\n", __func__, ireq);
  2012. dev_dbg(&ireq->isci_host->pdev->dev,
  2013. "%s: smp_req len = %d\n",
  2014. __func__,
  2015. task->smp_task.smp_req.length);
  2016. /* copy the smp_command to the address; */
  2017. sg_copy_to_buffer(&task->smp_task.smp_req, 1,
  2018. &sci_req->smp.cmd,
  2019. sizeof(struct smp_req));
  2020. status = scic_io_request_construct_smp(sci_req);
  2021. if (status != SCI_SUCCESS)
  2022. dev_warn(&ireq->isci_host->pdev->dev,
  2023. "%s: failed with status = %d\n",
  2024. __func__,
  2025. status);
  2026. return status;
  2027. }
  2028. /**
  2029. * isci_io_request_build() - This function builds the io request object.
  2030. * @isci_host: This parameter specifies the ISCI host object
  2031. * @request: This parameter points to the isci_request object allocated in the
  2032. * request construct function.
  2033. * @sci_device: This parameter is the handle for the sci core's remote device
  2034. * object that is the destination for this request.
  2035. *
  2036. * SCI_SUCCESS on successfull completion, or specific failure code.
  2037. */
  2038. static enum sci_status isci_io_request_build(
  2039. struct isci_host *isci_host,
  2040. struct isci_request *request,
  2041. struct isci_remote_device *isci_device)
  2042. {
  2043. enum sci_status status = SCI_SUCCESS;
  2044. struct sas_task *task = isci_request_access_task(request);
  2045. struct scic_sds_remote_device *sci_device = &isci_device->sci;
  2046. dev_dbg(&isci_host->pdev->dev,
  2047. "%s: isci_device = 0x%p; request = %p, "
  2048. "num_scatter = %d\n",
  2049. __func__,
  2050. isci_device,
  2051. request,
  2052. task->num_scatter);
  2053. /* map the sgl addresses, if present.
  2054. * libata does the mapping for sata devices
  2055. * before we get the request.
  2056. */
  2057. if (task->num_scatter &&
  2058. !sas_protocol_ata(task->task_proto) &&
  2059. !(SAS_PROTOCOL_SMP & task->task_proto)) {
  2060. request->num_sg_entries = dma_map_sg(
  2061. &isci_host->pdev->dev,
  2062. task->scatter,
  2063. task->num_scatter,
  2064. task->data_dir
  2065. );
  2066. if (request->num_sg_entries == 0)
  2067. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2068. }
  2069. /* build the common request object. For now,
  2070. * we will let the core allocate the IO tag.
  2071. */
  2072. status = scic_io_request_construct(&isci_host->sci, sci_device,
  2073. SCI_CONTROLLER_INVALID_IO_TAG,
  2074. &request->sci);
  2075. if (status != SCI_SUCCESS) {
  2076. dev_warn(&isci_host->pdev->dev,
  2077. "%s: failed request construct\n",
  2078. __func__);
  2079. return SCI_FAILURE;
  2080. }
  2081. switch (task->task_proto) {
  2082. case SAS_PROTOCOL_SMP:
  2083. status = isci_smp_request_build(request);
  2084. break;
  2085. case SAS_PROTOCOL_SSP:
  2086. status = isci_request_ssp_request_construct(request);
  2087. break;
  2088. case SAS_PROTOCOL_SATA:
  2089. case SAS_PROTOCOL_STP:
  2090. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  2091. status = isci_request_stp_request_construct(request);
  2092. break;
  2093. default:
  2094. dev_warn(&isci_host->pdev->dev,
  2095. "%s: unknown protocol\n", __func__);
  2096. return SCI_FAILURE;
  2097. }
  2098. return SCI_SUCCESS;
  2099. }
  2100. /**
  2101. * isci_request_alloc_core() - This function gets the request object from the
  2102. * isci_host dma cache.
  2103. * @isci_host: This parameter specifies the ISCI host object
  2104. * @isci_request: This parameter will contain the pointer to the new
  2105. * isci_request object.
  2106. * @isci_device: This parameter is the pointer to the isci remote device object
  2107. * that is the destination for this request.
  2108. * @gfp_flags: This parameter specifies the os allocation flags.
  2109. *
  2110. * SCI_SUCCESS on successfull completion, or specific failure code.
  2111. */
  2112. static int isci_request_alloc_core(
  2113. struct isci_host *isci_host,
  2114. struct isci_request **isci_request,
  2115. struct isci_remote_device *isci_device,
  2116. gfp_t gfp_flags)
  2117. {
  2118. int ret = 0;
  2119. dma_addr_t handle;
  2120. struct isci_request *request;
  2121. /* get pointer to dma memory. This actually points
  2122. * to both the isci_remote_device object and the
  2123. * sci object. The isci object is at the beginning
  2124. * of the memory allocated here.
  2125. */
  2126. request = dma_pool_alloc(isci_host->dma_pool, gfp_flags, &handle);
  2127. if (!request) {
  2128. dev_warn(&isci_host->pdev->dev,
  2129. "%s: dma_pool_alloc returned NULL\n", __func__);
  2130. return -ENOMEM;
  2131. }
  2132. /* initialize the request object. */
  2133. spin_lock_init(&request->state_lock);
  2134. request->request_daddr = handle;
  2135. request->isci_host = isci_host;
  2136. request->isci_device = isci_device;
  2137. request->io_request_completion = NULL;
  2138. request->terminated = false;
  2139. request->num_sg_entries = 0;
  2140. request->complete_in_target = false;
  2141. INIT_LIST_HEAD(&request->completed_node);
  2142. INIT_LIST_HEAD(&request->dev_node);
  2143. *isci_request = request;
  2144. isci_request_change_state(request, allocated);
  2145. return ret;
  2146. }
  2147. static int isci_request_alloc_io(
  2148. struct isci_host *isci_host,
  2149. struct sas_task *task,
  2150. struct isci_request **isci_request,
  2151. struct isci_remote_device *isci_device,
  2152. gfp_t gfp_flags)
  2153. {
  2154. int retval = isci_request_alloc_core(isci_host, isci_request,
  2155. isci_device, gfp_flags);
  2156. if (!retval) {
  2157. (*isci_request)->ttype_ptr.io_task_ptr = task;
  2158. (*isci_request)->ttype = io_task;
  2159. task->lldd_task = *isci_request;
  2160. }
  2161. return retval;
  2162. }
  2163. /**
  2164. * isci_request_alloc_tmf() - This function gets the request object from the
  2165. * isci_host dma cache and initializes the relevant fields as a sas_task.
  2166. * @isci_host: This parameter specifies the ISCI host object
  2167. * @sas_task: This parameter is the task struct from the upper layer driver.
  2168. * @isci_request: This parameter will contain the pointer to the new
  2169. * isci_request object.
  2170. * @isci_device: This parameter is the pointer to the isci remote device object
  2171. * that is the destination for this request.
  2172. * @gfp_flags: This parameter specifies the os allocation flags.
  2173. *
  2174. * SCI_SUCCESS on successfull completion, or specific failure code.
  2175. */
  2176. int isci_request_alloc_tmf(
  2177. struct isci_host *isci_host,
  2178. struct isci_tmf *isci_tmf,
  2179. struct isci_request **isci_request,
  2180. struct isci_remote_device *isci_device,
  2181. gfp_t gfp_flags)
  2182. {
  2183. int retval = isci_request_alloc_core(isci_host, isci_request,
  2184. isci_device, gfp_flags);
  2185. if (!retval) {
  2186. (*isci_request)->ttype_ptr.tmf_task_ptr = isci_tmf;
  2187. (*isci_request)->ttype = tmf_task;
  2188. }
  2189. return retval;
  2190. }
  2191. /**
  2192. * isci_request_execute() - This function allocates the isci_request object,
  2193. * all fills in some common fields.
  2194. * @isci_host: This parameter specifies the ISCI host object
  2195. * @sas_task: This parameter is the task struct from the upper layer driver.
  2196. * @isci_request: This parameter will contain the pointer to the new
  2197. * isci_request object.
  2198. * @gfp_flags: This parameter specifies the os allocation flags.
  2199. *
  2200. * SCI_SUCCESS on successfull completion, or specific failure code.
  2201. */
  2202. int isci_request_execute(
  2203. struct isci_host *isci_host,
  2204. struct sas_task *task,
  2205. struct isci_request **isci_request,
  2206. gfp_t gfp_flags)
  2207. {
  2208. int ret = 0;
  2209. struct scic_sds_remote_device *sci_device;
  2210. enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2211. struct isci_remote_device *isci_device;
  2212. struct isci_request *request;
  2213. unsigned long flags;
  2214. isci_device = task->dev->lldd_dev;
  2215. sci_device = &isci_device->sci;
  2216. /* do common allocation and init of request object. */
  2217. ret = isci_request_alloc_io(
  2218. isci_host,
  2219. task,
  2220. &request,
  2221. isci_device,
  2222. gfp_flags
  2223. );
  2224. if (ret)
  2225. goto out;
  2226. status = isci_io_request_build(isci_host, request, isci_device);
  2227. if (status != SCI_SUCCESS) {
  2228. dev_warn(&isci_host->pdev->dev,
  2229. "%s: request_construct failed - status = 0x%x\n",
  2230. __func__,
  2231. status);
  2232. goto out;
  2233. }
  2234. spin_lock_irqsave(&isci_host->scic_lock, flags);
  2235. /* send the request, let the core assign the IO TAG. */
  2236. status = scic_controller_start_io(&isci_host->sci, sci_device,
  2237. &request->sci,
  2238. SCI_CONTROLLER_INVALID_IO_TAG);
  2239. if (status != SCI_SUCCESS &&
  2240. status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  2241. dev_warn(&isci_host->pdev->dev,
  2242. "%s: failed request start (0x%x)\n",
  2243. __func__, status);
  2244. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  2245. goto out;
  2246. }
  2247. /* Either I/O started OK, or the core has signaled that
  2248. * the device needs a target reset.
  2249. *
  2250. * In either case, hold onto the I/O for later.
  2251. *
  2252. * Update it's status and add it to the list in the
  2253. * remote device object.
  2254. */
  2255. isci_request_change_state(request, started);
  2256. list_add(&request->dev_node, &isci_device->reqs_in_process);
  2257. if (status == SCI_SUCCESS) {
  2258. /* Save the tag for possible task mgmt later. */
  2259. request->io_tag = request->sci.io_tag;
  2260. } else {
  2261. /* The request did not really start in the
  2262. * hardware, so clear the request handle
  2263. * here so no terminations will be done.
  2264. */
  2265. request->terminated = true;
  2266. }
  2267. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  2268. if (status ==
  2269. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  2270. /* Signal libsas that we need the SCSI error
  2271. * handler thread to work on this I/O and that
  2272. * we want a device reset.
  2273. */
  2274. spin_lock_irqsave(&task->task_state_lock, flags);
  2275. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  2276. spin_unlock_irqrestore(&task->task_state_lock, flags);
  2277. /* Cause this task to be scheduled in the SCSI error
  2278. * handler thread.
  2279. */
  2280. isci_execpath_callback(isci_host, task,
  2281. sas_task_abort);
  2282. /* Change the status, since we are holding
  2283. * the I/O until it is managed by the SCSI
  2284. * error handler.
  2285. */
  2286. status = SCI_SUCCESS;
  2287. }
  2288. out:
  2289. if (status != SCI_SUCCESS) {
  2290. /* release dma memory on failure. */
  2291. isci_request_free(isci_host, request);
  2292. request = NULL;
  2293. ret = SCI_FAILURE;
  2294. }
  2295. *isci_request = request;
  2296. return ret;
  2297. }