mpt3sas_ctl.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636
  1. /*
  2. * Management Module Support for MPT (Message Passing Technology) based
  3. * controllers
  4. *
  5. * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
  6. * Copyright (C) 2012-2014 LSI Corporation
  7. * Copyright (C) 2013-2014 Avago Technologies
  8. * (mailto: MPT-FusionLinux.pdl@avagotech.com)
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * NO WARRANTY
  21. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  22. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  23. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  24. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  25. * solely responsible for determining the appropriateness of using and
  26. * distributing the Program and assumes all risks associated with its
  27. * exercise of rights under this Agreement, including but not limited to
  28. * the risks and costs of program errors, damage to or loss of data,
  29. * programs or equipment, and unavailability or interruption of operations.
  30. * DISCLAIMER OF LIABILITY
  31. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  34. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  35. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  37. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/errno.h>
  46. #include <linux/init.h>
  47. #include <linux/slab.h>
  48. #include <linux/types.h>
  49. #include <linux/pci.h>
  50. #include <linux/delay.h>
  51. #include <linux/compat.h>
  52. #include <linux/poll.h>
  53. #include <linux/io.h>
  54. #include <linux/uaccess.h>
  55. #include "mpt3sas_base.h"
  56. #include "mpt3sas_ctl.h"
  57. static struct fasync_struct *async_queue;
  58. static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
  59. /**
  60. * enum block_state - blocking state
  61. * @NON_BLOCKING: non blocking
  62. * @BLOCKING: blocking
  63. *
  64. * These states are for ioctls that need to wait for a response
  65. * from firmware, so they probably require sleep.
  66. */
  67. enum block_state {
  68. NON_BLOCKING,
  69. BLOCKING,
  70. };
  71. /**
  72. * _ctl_display_some_debug - debug routine
  73. * @ioc: per adapter object
  74. * @smid: system request message index
  75. * @calling_function_name: string pass from calling function
  76. * @mpi_reply: reply message frame
  77. * Context: none.
  78. *
  79. * Function for displaying debug info helpful when debugging issues
  80. * in this module.
  81. */
  82. static void
  83. _ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  84. char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
  85. {
  86. Mpi2ConfigRequest_t *mpi_request;
  87. char *desc = NULL;
  88. if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
  89. return;
  90. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  91. switch (mpi_request->Function) {
  92. case MPI2_FUNCTION_SCSI_IO_REQUEST:
  93. {
  94. Mpi2SCSIIORequest_t *scsi_request =
  95. (Mpi2SCSIIORequest_t *)mpi_request;
  96. snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
  97. "scsi_io, cmd(0x%02x), cdb_len(%d)",
  98. scsi_request->CDB.CDB32[0],
  99. le16_to_cpu(scsi_request->IoFlags) & 0xF);
  100. desc = ioc->tmp_string;
  101. break;
  102. }
  103. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  104. desc = "task_mgmt";
  105. break;
  106. case MPI2_FUNCTION_IOC_INIT:
  107. desc = "ioc_init";
  108. break;
  109. case MPI2_FUNCTION_IOC_FACTS:
  110. desc = "ioc_facts";
  111. break;
  112. case MPI2_FUNCTION_CONFIG:
  113. {
  114. Mpi2ConfigRequest_t *config_request =
  115. (Mpi2ConfigRequest_t *)mpi_request;
  116. snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
  117. "config, type(0x%02x), ext_type(0x%02x), number(%d)",
  118. (config_request->Header.PageType &
  119. MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
  120. config_request->Header.PageNumber);
  121. desc = ioc->tmp_string;
  122. break;
  123. }
  124. case MPI2_FUNCTION_PORT_FACTS:
  125. desc = "port_facts";
  126. break;
  127. case MPI2_FUNCTION_PORT_ENABLE:
  128. desc = "port_enable";
  129. break;
  130. case MPI2_FUNCTION_EVENT_NOTIFICATION:
  131. desc = "event_notification";
  132. break;
  133. case MPI2_FUNCTION_FW_DOWNLOAD:
  134. desc = "fw_download";
  135. break;
  136. case MPI2_FUNCTION_FW_UPLOAD:
  137. desc = "fw_upload";
  138. break;
  139. case MPI2_FUNCTION_RAID_ACTION:
  140. desc = "raid_action";
  141. break;
  142. case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
  143. {
  144. Mpi2SCSIIORequest_t *scsi_request =
  145. (Mpi2SCSIIORequest_t *)mpi_request;
  146. snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
  147. "raid_pass, cmd(0x%02x), cdb_len(%d)",
  148. scsi_request->CDB.CDB32[0],
  149. le16_to_cpu(scsi_request->IoFlags) & 0xF);
  150. desc = ioc->tmp_string;
  151. break;
  152. }
  153. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  154. desc = "sas_iounit_cntl";
  155. break;
  156. case MPI2_FUNCTION_SATA_PASSTHROUGH:
  157. desc = "sata_pass";
  158. break;
  159. case MPI2_FUNCTION_DIAG_BUFFER_POST:
  160. desc = "diag_buffer_post";
  161. break;
  162. case MPI2_FUNCTION_DIAG_RELEASE:
  163. desc = "diag_release";
  164. break;
  165. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  166. desc = "smp_passthrough";
  167. break;
  168. }
  169. if (!desc)
  170. return;
  171. pr_info(MPT3SAS_FMT "%s: %s, smid(%d)\n",
  172. ioc->name, calling_function_name, desc, smid);
  173. if (!mpi_reply)
  174. return;
  175. if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
  176. pr_info(MPT3SAS_FMT
  177. "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
  178. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  179. le32_to_cpu(mpi_reply->IOCLogInfo));
  180. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  181. mpi_request->Function ==
  182. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
  183. Mpi2SCSIIOReply_t *scsi_reply =
  184. (Mpi2SCSIIOReply_t *)mpi_reply;
  185. struct _sas_device *sas_device = NULL;
  186. struct _pcie_device *pcie_device = NULL;
  187. sas_device = mpt3sas_get_sdev_by_handle(ioc,
  188. le16_to_cpu(scsi_reply->DevHandle));
  189. if (sas_device) {
  190. pr_warn(MPT3SAS_FMT "\tsas_address(0x%016llx), phy(%d)\n",
  191. ioc->name, (unsigned long long)
  192. sas_device->sas_address, sas_device->phy);
  193. pr_warn(MPT3SAS_FMT
  194. "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
  195. ioc->name, (unsigned long long)
  196. sas_device->enclosure_logical_id, sas_device->slot);
  197. sas_device_put(sas_device);
  198. }
  199. if (!sas_device) {
  200. pcie_device = mpt3sas_get_pdev_by_handle(ioc,
  201. le16_to_cpu(scsi_reply->DevHandle));
  202. if (pcie_device) {
  203. pr_warn(MPT3SAS_FMT
  204. "\tWWID(0x%016llx), port(%d)\n", ioc->name,
  205. (unsigned long long)pcie_device->wwid,
  206. pcie_device->port_num);
  207. if (pcie_device->enclosure_handle != 0)
  208. pr_warn(MPT3SAS_FMT
  209. "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
  210. ioc->name, (unsigned long long)
  211. pcie_device->enclosure_logical_id,
  212. pcie_device->slot);
  213. pcie_device_put(pcie_device);
  214. }
  215. }
  216. if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
  217. pr_info(MPT3SAS_FMT
  218. "\tscsi_state(0x%02x), scsi_status"
  219. "(0x%02x)\n", ioc->name,
  220. scsi_reply->SCSIState,
  221. scsi_reply->SCSIStatus);
  222. }
  223. }
  224. /**
  225. * mpt3sas_ctl_done - ctl module completion routine
  226. * @ioc: per adapter object
  227. * @smid: system request message index
  228. * @msix_index: MSIX table index supplied by the OS
  229. * @reply: reply message frame(lower 32bit addr)
  230. * Context: none.
  231. *
  232. * The callback handler when using ioc->ctl_cb_idx.
  233. *
  234. * Return 1 meaning mf should be freed from _base_interrupt
  235. * 0 means the mf is freed from this function.
  236. */
  237. u8
  238. mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  239. u32 reply)
  240. {
  241. MPI2DefaultReply_t *mpi_reply;
  242. Mpi2SCSIIOReply_t *scsiio_reply;
  243. Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
  244. const void *sense_data;
  245. u32 sz;
  246. if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
  247. return 1;
  248. if (ioc->ctl_cmds.smid != smid)
  249. return 1;
  250. ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
  251. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  252. if (mpi_reply) {
  253. memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  254. ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
  255. /* get sense data */
  256. if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  257. mpi_reply->Function ==
  258. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
  259. scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
  260. if (scsiio_reply->SCSIState &
  261. MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  262. sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  263. le32_to_cpu(scsiio_reply->SenseCount));
  264. sense_data = mpt3sas_base_get_sense_buffer(ioc,
  265. smid);
  266. memcpy(ioc->ctl_cmds.sense, sense_data, sz);
  267. }
  268. }
  269. /*
  270. * Get Error Response data for NVMe device. The ctl_cmds.sense
  271. * buffer is used to store the Error Response data.
  272. */
  273. if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
  274. nvme_error_reply =
  275. (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
  276. sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
  277. le16_to_cpu(nvme_error_reply->ErrorResponseCount));
  278. sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
  279. memcpy(ioc->ctl_cmds.sense, sense_data, sz);
  280. }
  281. }
  282. _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
  283. ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
  284. complete(&ioc->ctl_cmds.done);
  285. return 1;
  286. }
  287. /**
  288. * _ctl_check_event_type - determines when an event needs logging
  289. * @ioc: per adapter object
  290. * @event: firmware event
  291. *
  292. * The bitmask in ioc->event_type[] indicates which events should be
  293. * be saved in the driver event_log. This bitmask is set by application.
  294. *
  295. * Returns 1 when event should be captured, or zero means no match.
  296. */
  297. static int
  298. _ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
  299. {
  300. u16 i;
  301. u32 desired_event;
  302. if (event >= 128 || !event || !ioc->event_log)
  303. return 0;
  304. desired_event = (1 << (event % 32));
  305. if (!desired_event)
  306. desired_event = 1;
  307. i = event / 32;
  308. return desired_event & ioc->event_type[i];
  309. }
  310. /**
  311. * mpt3sas_ctl_add_to_event_log - add event
  312. * @ioc: per adapter object
  313. * @mpi_reply: reply message frame
  314. *
  315. * Return nothing.
  316. */
  317. void
  318. mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
  319. Mpi2EventNotificationReply_t *mpi_reply)
  320. {
  321. struct MPT3_IOCTL_EVENTS *event_log;
  322. u16 event;
  323. int i;
  324. u32 sz, event_data_sz;
  325. u8 send_aen = 0;
  326. if (!ioc->event_log)
  327. return;
  328. event = le16_to_cpu(mpi_reply->Event);
  329. if (_ctl_check_event_type(ioc, event)) {
  330. /* insert entry into circular event_log */
  331. i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
  332. event_log = ioc->event_log;
  333. event_log[i].event = event;
  334. event_log[i].context = ioc->event_context++;
  335. event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
  336. sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
  337. memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
  338. memcpy(event_log[i].data, mpi_reply->EventData, sz);
  339. send_aen = 1;
  340. }
  341. /* This aen_event_read_flag flag is set until the
  342. * application has read the event log.
  343. * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
  344. */
  345. if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
  346. (send_aen && !ioc->aen_event_read_flag)) {
  347. ioc->aen_event_read_flag = 1;
  348. wake_up_interruptible(&ctl_poll_wait);
  349. if (async_queue)
  350. kill_fasync(&async_queue, SIGIO, POLL_IN);
  351. }
  352. }
  353. /**
  354. * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
  355. * @ioc: per adapter object
  356. * @msix_index: MSIX table index supplied by the OS
  357. * @reply: reply message frame(lower 32bit addr)
  358. * Context: interrupt.
  359. *
  360. * This function merely adds a new work task into ioc->firmware_event_thread.
  361. * The tasks are worked from _firmware_event_work in user context.
  362. *
  363. * Return 1 meaning mf should be freed from _base_interrupt
  364. * 0 means the mf is freed from this function.
  365. */
  366. u8
  367. mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
  368. u32 reply)
  369. {
  370. Mpi2EventNotificationReply_t *mpi_reply;
  371. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  372. if (mpi_reply)
  373. mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
  374. return 1;
  375. }
  376. /**
  377. * _ctl_verify_adapter - validates ioc_number passed from application
  378. * @ioc: per adapter object
  379. * @iocpp: The ioc pointer is returned in this.
  380. * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
  381. * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
  382. *
  383. * Return (-1) means error, else ioc_number.
  384. */
  385. static int
  386. _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
  387. int mpi_version)
  388. {
  389. struct MPT3SAS_ADAPTER *ioc;
  390. int version = 0;
  391. /* global ioc lock to protect controller on list operations */
  392. spin_lock(&gioc_lock);
  393. list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
  394. if (ioc->id != ioc_number)
  395. continue;
  396. /* Check whether this ioctl command is from right
  397. * ioctl device or not, if not continue the search.
  398. */
  399. version = ioc->hba_mpi_version_belonged;
  400. /* MPI25_VERSION and MPI26_VERSION uses same ioctl
  401. * device.
  402. */
  403. if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
  404. if ((version == MPI25_VERSION) ||
  405. (version == MPI26_VERSION))
  406. goto out;
  407. else
  408. continue;
  409. } else {
  410. if (version != mpi_version)
  411. continue;
  412. }
  413. out:
  414. spin_unlock(&gioc_lock);
  415. *iocpp = ioc;
  416. return ioc_number;
  417. }
  418. spin_unlock(&gioc_lock);
  419. *iocpp = NULL;
  420. return -1;
  421. }
  422. /**
  423. * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
  424. * @ioc: per adapter object
  425. * @reset_phase: phase
  426. *
  427. * The handler for doing any required cleanup or initialization.
  428. *
  429. * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
  430. * MPT3_IOC_DONE_RESET
  431. */
  432. void
  433. mpt3sas_ctl_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
  434. {
  435. int i;
  436. u8 issue_reset;
  437. switch (reset_phase) {
  438. case MPT3_IOC_PRE_RESET:
  439. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  440. "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
  441. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  442. if (!(ioc->diag_buffer_status[i] &
  443. MPT3_DIAG_BUFFER_IS_REGISTERED))
  444. continue;
  445. if ((ioc->diag_buffer_status[i] &
  446. MPT3_DIAG_BUFFER_IS_RELEASED))
  447. continue;
  448. mpt3sas_send_diag_release(ioc, i, &issue_reset);
  449. }
  450. break;
  451. case MPT3_IOC_AFTER_RESET:
  452. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  453. "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
  454. if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
  455. ioc->ctl_cmds.status |= MPT3_CMD_RESET;
  456. mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
  457. complete(&ioc->ctl_cmds.done);
  458. }
  459. break;
  460. case MPT3_IOC_DONE_RESET:
  461. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  462. "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
  463. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  464. if (!(ioc->diag_buffer_status[i] &
  465. MPT3_DIAG_BUFFER_IS_REGISTERED))
  466. continue;
  467. if ((ioc->diag_buffer_status[i] &
  468. MPT3_DIAG_BUFFER_IS_RELEASED))
  469. continue;
  470. ioc->diag_buffer_status[i] |=
  471. MPT3_DIAG_BUFFER_IS_DIAG_RESET;
  472. }
  473. break;
  474. }
  475. }
  476. /**
  477. * _ctl_fasync -
  478. * @fd -
  479. * @filep -
  480. * @mode -
  481. *
  482. * Called when application request fasyn callback handler.
  483. */
  484. static int
  485. _ctl_fasync(int fd, struct file *filep, int mode)
  486. {
  487. return fasync_helper(fd, filep, mode, &async_queue);
  488. }
  489. /**
  490. * _ctl_poll -
  491. * @file -
  492. * @wait -
  493. *
  494. */
  495. static __poll_t
  496. _ctl_poll(struct file *filep, poll_table *wait)
  497. {
  498. struct MPT3SAS_ADAPTER *ioc;
  499. poll_wait(filep, &ctl_poll_wait, wait);
  500. /* global ioc lock to protect controller on list operations */
  501. spin_lock(&gioc_lock);
  502. list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
  503. if (ioc->aen_event_read_flag) {
  504. spin_unlock(&gioc_lock);
  505. return EPOLLIN | EPOLLRDNORM;
  506. }
  507. }
  508. spin_unlock(&gioc_lock);
  509. return 0;
  510. }
  511. /**
  512. * _ctl_set_task_mid - assign an active smid to tm request
  513. * @ioc: per adapter object
  514. * @karg - (struct mpt3_ioctl_command)
  515. * @tm_request - pointer to mf from user space
  516. *
  517. * Returns 0 when an smid if found, else fail.
  518. * during failure, the reply frame is filled.
  519. */
  520. static int
  521. _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
  522. Mpi2SCSITaskManagementRequest_t *tm_request)
  523. {
  524. u8 found = 0;
  525. u16 smid;
  526. u16 handle;
  527. struct scsi_cmnd *scmd;
  528. struct MPT3SAS_DEVICE *priv_data;
  529. Mpi2SCSITaskManagementReply_t *tm_reply;
  530. u32 sz;
  531. u32 lun;
  532. char *desc = NULL;
  533. if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
  534. desc = "abort_task";
  535. else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
  536. desc = "query_task";
  537. else
  538. return 0;
  539. lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
  540. handle = le16_to_cpu(tm_request->DevHandle);
  541. for (smid = ioc->scsiio_depth; smid && !found; smid--) {
  542. struct scsiio_tracker *st;
  543. scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
  544. if (!scmd)
  545. continue;
  546. if (lun != scmd->device->lun)
  547. continue;
  548. priv_data = scmd->device->hostdata;
  549. if (priv_data->sas_target == NULL)
  550. continue;
  551. if (priv_data->sas_target->handle != handle)
  552. continue;
  553. st = scsi_cmd_priv(scmd);
  554. tm_request->TaskMID = cpu_to_le16(st->smid);
  555. found = 1;
  556. }
  557. if (!found) {
  558. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  559. "%s: handle(0x%04x), lun(%d), no active mid!!\n",
  560. ioc->name,
  561. desc, le16_to_cpu(tm_request->DevHandle), lun));
  562. tm_reply = ioc->ctl_cmds.reply;
  563. tm_reply->DevHandle = tm_request->DevHandle;
  564. tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  565. tm_reply->TaskType = tm_request->TaskType;
  566. tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
  567. tm_reply->VP_ID = tm_request->VP_ID;
  568. tm_reply->VF_ID = tm_request->VF_ID;
  569. sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
  570. if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
  571. sz))
  572. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  573. __LINE__, __func__);
  574. return 1;
  575. }
  576. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  577. "%s: handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
  578. desc, le16_to_cpu(tm_request->DevHandle), lun,
  579. le16_to_cpu(tm_request->TaskMID)));
  580. return 0;
  581. }
  582. /**
  583. * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
  584. * @ioc: per adapter object
  585. * @karg - (struct mpt3_ioctl_command)
  586. * @mf - pointer to mf in user space
  587. */
  588. static long
  589. _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
  590. void __user *mf)
  591. {
  592. MPI2RequestHeader_t *mpi_request = NULL, *request;
  593. MPI2DefaultReply_t *mpi_reply;
  594. Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
  595. struct _pcie_device *pcie_device = NULL;
  596. u32 ioc_state;
  597. u16 smid;
  598. u8 timeout;
  599. u8 issue_reset;
  600. u32 sz, sz_arg;
  601. void *psge;
  602. void *data_out = NULL;
  603. dma_addr_t data_out_dma = 0;
  604. size_t data_out_sz = 0;
  605. void *data_in = NULL;
  606. dma_addr_t data_in_dma = 0;
  607. size_t data_in_sz = 0;
  608. long ret;
  609. u16 wait_state_count;
  610. u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
  611. u8 tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
  612. issue_reset = 0;
  613. if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
  614. pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
  615. ioc->name, __func__);
  616. ret = -EAGAIN;
  617. goto out;
  618. }
  619. wait_state_count = 0;
  620. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  621. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  622. if (wait_state_count++ == 10) {
  623. pr_err(MPT3SAS_FMT
  624. "%s: failed due to ioc not operational\n",
  625. ioc->name, __func__);
  626. ret = -EFAULT;
  627. goto out;
  628. }
  629. ssleep(1);
  630. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  631. pr_info(MPT3SAS_FMT
  632. "%s: waiting for operational state(count=%d)\n",
  633. ioc->name,
  634. __func__, wait_state_count);
  635. }
  636. if (wait_state_count)
  637. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  638. ioc->name, __func__);
  639. mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
  640. if (!mpi_request) {
  641. pr_err(MPT3SAS_FMT
  642. "%s: failed obtaining a memory for mpi_request\n",
  643. ioc->name, __func__);
  644. ret = -ENOMEM;
  645. goto out;
  646. }
  647. /* Check for overflow and wraparound */
  648. if (karg.data_sge_offset * 4 > ioc->request_sz ||
  649. karg.data_sge_offset > (UINT_MAX / 4)) {
  650. ret = -EINVAL;
  651. goto out;
  652. }
  653. /* copy in request message frame from user */
  654. if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
  655. pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
  656. __func__);
  657. ret = -EFAULT;
  658. goto out;
  659. }
  660. if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
  661. smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
  662. if (!smid) {
  663. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  664. ioc->name, __func__);
  665. ret = -EAGAIN;
  666. goto out;
  667. }
  668. } else {
  669. /* Use first reserved smid for passthrough ioctls */
  670. smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
  671. }
  672. ret = 0;
  673. ioc->ctl_cmds.status = MPT3_CMD_PENDING;
  674. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  675. request = mpt3sas_base_get_msg_frame(ioc, smid);
  676. memcpy(request, mpi_request, karg.data_sge_offset*4);
  677. ioc->ctl_cmds.smid = smid;
  678. data_out_sz = karg.data_out_size;
  679. data_in_sz = karg.data_in_size;
  680. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  681. mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  682. mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
  683. mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
  684. mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
  685. device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
  686. if (!device_handle || (device_handle >
  687. ioc->facts.MaxDevHandle)) {
  688. ret = -EINVAL;
  689. mpt3sas_base_free_smid(ioc, smid);
  690. goto out;
  691. }
  692. }
  693. /* obtain dma-able memory for data transfer */
  694. if (data_out_sz) /* WRITE */ {
  695. data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
  696. &data_out_dma);
  697. if (!data_out) {
  698. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  699. __LINE__, __func__);
  700. ret = -ENOMEM;
  701. mpt3sas_base_free_smid(ioc, smid);
  702. goto out;
  703. }
  704. if (copy_from_user(data_out, karg.data_out_buf_ptr,
  705. data_out_sz)) {
  706. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  707. __LINE__, __func__);
  708. ret = -EFAULT;
  709. mpt3sas_base_free_smid(ioc, smid);
  710. goto out;
  711. }
  712. }
  713. if (data_in_sz) /* READ */ {
  714. data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
  715. &data_in_dma);
  716. if (!data_in) {
  717. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  718. __LINE__, __func__);
  719. ret = -ENOMEM;
  720. mpt3sas_base_free_smid(ioc, smid);
  721. goto out;
  722. }
  723. }
  724. psge = (void *)request + (karg.data_sge_offset*4);
  725. /* send command to firmware */
  726. _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
  727. init_completion(&ioc->ctl_cmds.done);
  728. switch (mpi_request->Function) {
  729. case MPI2_FUNCTION_NVME_ENCAPSULATED:
  730. {
  731. nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
  732. /*
  733. * Get the Physical Address of the sense buffer.
  734. * Use Error Response buffer address field to hold the sense
  735. * buffer address.
  736. * Clear the internal sense buffer, which will potentially hold
  737. * the Completion Queue Entry on return, or 0 if no Entry.
  738. * Build the PRPs and set direction bits.
  739. * Send the request.
  740. */
  741. nvme_encap_request->ErrorResponseBaseAddress =
  742. cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
  743. nvme_encap_request->ErrorResponseBaseAddress |=
  744. cpu_to_le64(le32_to_cpu(
  745. mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
  746. nvme_encap_request->ErrorResponseAllocationLength =
  747. cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
  748. memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
  749. ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
  750. data_out_dma, data_out_sz, data_in_dma, data_in_sz);
  751. if (test_bit(device_handle, ioc->device_remove_in_progress)) {
  752. dtmprintk(ioc, pr_info(MPT3SAS_FMT "handle(0x%04x) :"
  753. "ioctl failed due to device removal in progress\n",
  754. ioc->name, device_handle));
  755. mpt3sas_base_free_smid(ioc, smid);
  756. ret = -EINVAL;
  757. goto out;
  758. }
  759. mpt3sas_base_put_smid_nvme_encap(ioc, smid);
  760. break;
  761. }
  762. case MPI2_FUNCTION_SCSI_IO_REQUEST:
  763. case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
  764. {
  765. Mpi2SCSIIORequest_t *scsiio_request =
  766. (Mpi2SCSIIORequest_t *)request;
  767. scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  768. scsiio_request->SenseBufferLowAddress =
  769. mpt3sas_base_get_sense_buffer_dma(ioc, smid);
  770. memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
  771. if (test_bit(device_handle, ioc->device_remove_in_progress)) {
  772. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  773. "handle(0x%04x) :ioctl failed due to device removal in progress\n",
  774. ioc->name, device_handle));
  775. mpt3sas_base_free_smid(ioc, smid);
  776. ret = -EINVAL;
  777. goto out;
  778. }
  779. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
  780. data_in_dma, data_in_sz);
  781. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
  782. ioc->put_smid_scsi_io(ioc, smid, device_handle);
  783. else
  784. mpt3sas_base_put_smid_default(ioc, smid);
  785. break;
  786. }
  787. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  788. {
  789. Mpi2SCSITaskManagementRequest_t *tm_request =
  790. (Mpi2SCSITaskManagementRequest_t *)request;
  791. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  792. "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
  793. ioc->name,
  794. le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
  795. ioc->got_task_abort_from_ioctl = 1;
  796. if (tm_request->TaskType ==
  797. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
  798. tm_request->TaskType ==
  799. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
  800. if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
  801. mpt3sas_base_free_smid(ioc, smid);
  802. ioc->got_task_abort_from_ioctl = 0;
  803. goto out;
  804. }
  805. }
  806. ioc->got_task_abort_from_ioctl = 0;
  807. if (test_bit(device_handle, ioc->device_remove_in_progress)) {
  808. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  809. "handle(0x%04x) :ioctl failed due to device removal in progress\n",
  810. ioc->name, device_handle));
  811. mpt3sas_base_free_smid(ioc, smid);
  812. ret = -EINVAL;
  813. goto out;
  814. }
  815. mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
  816. tm_request->DevHandle));
  817. ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
  818. data_in_dma, data_in_sz);
  819. mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
  820. break;
  821. }
  822. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  823. {
  824. Mpi2SmpPassthroughRequest_t *smp_request =
  825. (Mpi2SmpPassthroughRequest_t *)mpi_request;
  826. u8 *data;
  827. /* ioc determines which port to use */
  828. smp_request->PhysicalPort = 0xFF;
  829. if (smp_request->PassthroughFlags &
  830. MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
  831. data = (u8 *)&smp_request->SGL;
  832. else {
  833. if (unlikely(data_out == NULL)) {
  834. pr_err("failure at %s:%d/%s()!\n",
  835. __FILE__, __LINE__, __func__);
  836. mpt3sas_base_free_smid(ioc, smid);
  837. ret = -EINVAL;
  838. goto out;
  839. }
  840. data = data_out;
  841. }
  842. if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
  843. ioc->ioc_link_reset_in_progress = 1;
  844. ioc->ignore_loginfos = 1;
  845. }
  846. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
  847. data_in_sz);
  848. mpt3sas_base_put_smid_default(ioc, smid);
  849. break;
  850. }
  851. case MPI2_FUNCTION_SATA_PASSTHROUGH:
  852. {
  853. if (test_bit(device_handle, ioc->device_remove_in_progress)) {
  854. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  855. "handle(0x%04x) :ioctl failed due to device removal in progress\n",
  856. ioc->name, device_handle));
  857. mpt3sas_base_free_smid(ioc, smid);
  858. ret = -EINVAL;
  859. goto out;
  860. }
  861. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
  862. data_in_sz);
  863. mpt3sas_base_put_smid_default(ioc, smid);
  864. break;
  865. }
  866. case MPI2_FUNCTION_FW_DOWNLOAD:
  867. case MPI2_FUNCTION_FW_UPLOAD:
  868. {
  869. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
  870. data_in_sz);
  871. mpt3sas_base_put_smid_default(ioc, smid);
  872. break;
  873. }
  874. case MPI2_FUNCTION_TOOLBOX:
  875. {
  876. Mpi2ToolboxCleanRequest_t *toolbox_request =
  877. (Mpi2ToolboxCleanRequest_t *)mpi_request;
  878. if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
  879. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
  880. data_in_dma, data_in_sz);
  881. } else {
  882. ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
  883. data_in_dma, data_in_sz);
  884. }
  885. mpt3sas_base_put_smid_default(ioc, smid);
  886. break;
  887. }
  888. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  889. {
  890. Mpi2SasIoUnitControlRequest_t *sasiounit_request =
  891. (Mpi2SasIoUnitControlRequest_t *)mpi_request;
  892. if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
  893. || sasiounit_request->Operation ==
  894. MPI2_SAS_OP_PHY_LINK_RESET) {
  895. ioc->ioc_link_reset_in_progress = 1;
  896. ioc->ignore_loginfos = 1;
  897. }
  898. /* drop to default case for posting the request */
  899. }
  900. default:
  901. ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
  902. data_in_dma, data_in_sz);
  903. mpt3sas_base_put_smid_default(ioc, smid);
  904. break;
  905. }
  906. if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
  907. timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
  908. else
  909. timeout = karg.timeout;
  910. wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
  911. if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
  912. Mpi2SCSITaskManagementRequest_t *tm_request =
  913. (Mpi2SCSITaskManagementRequest_t *)mpi_request;
  914. mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
  915. tm_request->DevHandle));
  916. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
  917. } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
  918. mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
  919. ioc->ioc_link_reset_in_progress) {
  920. ioc->ioc_link_reset_in_progress = 0;
  921. ioc->ignore_loginfos = 0;
  922. }
  923. if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
  924. pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
  925. __func__);
  926. _debug_dump_mf(mpi_request, karg.data_sge_offset);
  927. if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
  928. issue_reset = 1;
  929. goto issue_host_reset;
  930. }
  931. mpi_reply = ioc->ctl_cmds.reply;
  932. if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
  933. (ioc->logging_level & MPT_DEBUG_TM)) {
  934. Mpi2SCSITaskManagementReply_t *tm_reply =
  935. (Mpi2SCSITaskManagementReply_t *)mpi_reply;
  936. pr_info(MPT3SAS_FMT "TASK_MGMT: " \
  937. "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
  938. "TerminationCount(0x%08x)\n", ioc->name,
  939. le16_to_cpu(tm_reply->IOCStatus),
  940. le32_to_cpu(tm_reply->IOCLogInfo),
  941. le32_to_cpu(tm_reply->TerminationCount));
  942. }
  943. /* copy out xdata to user */
  944. if (data_in_sz) {
  945. if (copy_to_user(karg.data_in_buf_ptr, data_in,
  946. data_in_sz)) {
  947. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  948. __LINE__, __func__);
  949. ret = -ENODATA;
  950. goto out;
  951. }
  952. }
  953. /* copy out reply message frame to user */
  954. if (karg.max_reply_bytes) {
  955. sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
  956. if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
  957. sz)) {
  958. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  959. __LINE__, __func__);
  960. ret = -ENODATA;
  961. goto out;
  962. }
  963. }
  964. /* copy out sense/NVMe Error Response to user */
  965. if (karg.max_sense_bytes && (mpi_request->Function ==
  966. MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
  967. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
  968. MPI2_FUNCTION_NVME_ENCAPSULATED)) {
  969. if (karg.sense_data_ptr == NULL) {
  970. pr_info(MPT3SAS_FMT "Response buffer provided"
  971. " by application is NULL; Response data will"
  972. " not be returned.\n", ioc->name);
  973. goto out;
  974. }
  975. sz_arg = (mpi_request->Function ==
  976. MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
  977. SCSI_SENSE_BUFFERSIZE;
  978. sz = min_t(u32, karg.max_sense_bytes, sz_arg);
  979. if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
  980. sz)) {
  981. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  982. __LINE__, __func__);
  983. ret = -ENODATA;
  984. goto out;
  985. }
  986. }
  987. issue_host_reset:
  988. if (issue_reset) {
  989. ret = -ENODATA;
  990. if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  991. mpi_request->Function ==
  992. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  993. mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
  994. pr_info(MPT3SAS_FMT "issue target reset: handle = (0x%04x)\n",
  995. ioc->name,
  996. le16_to_cpu(mpi_request->FunctionDependent1));
  997. mpt3sas_halt_firmware(ioc);
  998. pcie_device = mpt3sas_get_pdev_by_handle(ioc,
  999. le16_to_cpu(mpi_request->FunctionDependent1));
  1000. if (pcie_device && (!ioc->tm_custom_handling))
  1001. mpt3sas_scsih_issue_locked_tm(ioc,
  1002. le16_to_cpu(mpi_request->FunctionDependent1),
  1003. 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
  1004. 0, pcie_device->reset_timeout,
  1005. tr_method);
  1006. else
  1007. mpt3sas_scsih_issue_locked_tm(ioc,
  1008. le16_to_cpu(mpi_request->FunctionDependent1),
  1009. 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
  1010. 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
  1011. } else
  1012. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1013. }
  1014. out:
  1015. if (pcie_device)
  1016. pcie_device_put(pcie_device);
  1017. /* free memory associated with sg buffers */
  1018. if (data_in)
  1019. pci_free_consistent(ioc->pdev, data_in_sz, data_in,
  1020. data_in_dma);
  1021. if (data_out)
  1022. pci_free_consistent(ioc->pdev, data_out_sz, data_out,
  1023. data_out_dma);
  1024. kfree(mpi_request);
  1025. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  1026. return ret;
  1027. }
  1028. /**
  1029. * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
  1030. * @ioc: per adapter object
  1031. * @arg - user space buffer containing ioctl content
  1032. */
  1033. static long
  1034. _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1035. {
  1036. struct mpt3_ioctl_iocinfo karg;
  1037. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1038. __func__));
  1039. memset(&karg, 0 , sizeof(karg));
  1040. if (ioc->pfacts)
  1041. karg.port_number = ioc->pfacts[0].PortNumber;
  1042. karg.hw_rev = ioc->pdev->revision;
  1043. karg.pci_id = ioc->pdev->device;
  1044. karg.subsystem_device = ioc->pdev->subsystem_device;
  1045. karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
  1046. karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
  1047. karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
  1048. karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
  1049. karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
  1050. karg.firmware_version = ioc->facts.FWVersion.Word;
  1051. strcpy(karg.driver_version, ioc->driver_name);
  1052. strcat(karg.driver_version, "-");
  1053. switch (ioc->hba_mpi_version_belonged) {
  1054. case MPI2_VERSION:
  1055. if (ioc->is_warpdrive)
  1056. karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
  1057. else
  1058. karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
  1059. strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
  1060. break;
  1061. case MPI25_VERSION:
  1062. case MPI26_VERSION:
  1063. if (ioc->is_gen35_ioc)
  1064. karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
  1065. else
  1066. karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
  1067. strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
  1068. break;
  1069. }
  1070. karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  1071. if (copy_to_user(arg, &karg, sizeof(karg))) {
  1072. pr_err("failure at %s:%d/%s()!\n",
  1073. __FILE__, __LINE__, __func__);
  1074. return -EFAULT;
  1075. }
  1076. return 0;
  1077. }
  1078. /**
  1079. * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
  1080. * @ioc: per adapter object
  1081. * @arg - user space buffer containing ioctl content
  1082. */
  1083. static long
  1084. _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1085. {
  1086. struct mpt3_ioctl_eventquery karg;
  1087. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1088. pr_err("failure at %s:%d/%s()!\n",
  1089. __FILE__, __LINE__, __func__);
  1090. return -EFAULT;
  1091. }
  1092. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1093. __func__));
  1094. karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
  1095. memcpy(karg.event_types, ioc->event_type,
  1096. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
  1097. if (copy_to_user(arg, &karg, sizeof(karg))) {
  1098. pr_err("failure at %s:%d/%s()!\n",
  1099. __FILE__, __LINE__, __func__);
  1100. return -EFAULT;
  1101. }
  1102. return 0;
  1103. }
  1104. /**
  1105. * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
  1106. * @ioc: per adapter object
  1107. * @arg - user space buffer containing ioctl content
  1108. */
  1109. static long
  1110. _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1111. {
  1112. struct mpt3_ioctl_eventenable karg;
  1113. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1114. pr_err("failure at %s:%d/%s()!\n",
  1115. __FILE__, __LINE__, __func__);
  1116. return -EFAULT;
  1117. }
  1118. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1119. __func__));
  1120. memcpy(ioc->event_type, karg.event_types,
  1121. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
  1122. mpt3sas_base_validate_event_type(ioc, ioc->event_type);
  1123. if (ioc->event_log)
  1124. return 0;
  1125. /* initialize event_log */
  1126. ioc->event_context = 0;
  1127. ioc->aen_event_read_flag = 0;
  1128. ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
  1129. sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
  1130. if (!ioc->event_log) {
  1131. pr_err("failure at %s:%d/%s()!\n",
  1132. __FILE__, __LINE__, __func__);
  1133. return -ENOMEM;
  1134. }
  1135. return 0;
  1136. }
  1137. /**
  1138. * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
  1139. * @ioc: per adapter object
  1140. * @arg - user space buffer containing ioctl content
  1141. */
  1142. static long
  1143. _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1144. {
  1145. struct mpt3_ioctl_eventreport karg;
  1146. u32 number_bytes, max_events, max;
  1147. struct mpt3_ioctl_eventreport __user *uarg = arg;
  1148. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1149. pr_err("failure at %s:%d/%s()!\n",
  1150. __FILE__, __LINE__, __func__);
  1151. return -EFAULT;
  1152. }
  1153. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1154. __func__));
  1155. number_bytes = karg.hdr.max_data_size -
  1156. sizeof(struct mpt3_ioctl_header);
  1157. max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
  1158. max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
  1159. /* If fewer than 1 event is requested, there must have
  1160. * been some type of error.
  1161. */
  1162. if (!max || !ioc->event_log)
  1163. return -ENODATA;
  1164. number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
  1165. if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
  1166. pr_err("failure at %s:%d/%s()!\n",
  1167. __FILE__, __LINE__, __func__);
  1168. return -EFAULT;
  1169. }
  1170. /* reset flag so SIGIO can restart */
  1171. ioc->aen_event_read_flag = 0;
  1172. return 0;
  1173. }
  1174. /**
  1175. * _ctl_do_reset - main handler for MPT3HARDRESET opcode
  1176. * @ioc: per adapter object
  1177. * @arg - user space buffer containing ioctl content
  1178. */
  1179. static long
  1180. _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1181. {
  1182. struct mpt3_ioctl_diag_reset karg;
  1183. int retval;
  1184. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1185. pr_err("failure at %s:%d/%s()!\n",
  1186. __FILE__, __LINE__, __func__);
  1187. return -EFAULT;
  1188. }
  1189. if (ioc->shost_recovery || ioc->pci_error_recovery ||
  1190. ioc->is_driver_loading)
  1191. return -EAGAIN;
  1192. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1193. __func__));
  1194. retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1195. pr_info(MPT3SAS_FMT "host reset: %s\n",
  1196. ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
  1197. return 0;
  1198. }
  1199. /**
  1200. * _ctl_btdh_search_sas_device - searching for sas device
  1201. * @ioc: per adapter object
  1202. * @btdh: btdh ioctl payload
  1203. */
  1204. static int
  1205. _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
  1206. struct mpt3_ioctl_btdh_mapping *btdh)
  1207. {
  1208. struct _sas_device *sas_device;
  1209. unsigned long flags;
  1210. int rc = 0;
  1211. if (list_empty(&ioc->sas_device_list))
  1212. return rc;
  1213. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1214. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  1215. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1216. btdh->handle == sas_device->handle) {
  1217. btdh->bus = sas_device->channel;
  1218. btdh->id = sas_device->id;
  1219. rc = 1;
  1220. goto out;
  1221. } else if (btdh->bus == sas_device->channel && btdh->id ==
  1222. sas_device->id && btdh->handle == 0xFFFF) {
  1223. btdh->handle = sas_device->handle;
  1224. rc = 1;
  1225. goto out;
  1226. }
  1227. }
  1228. out:
  1229. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1230. return rc;
  1231. }
  1232. /**
  1233. * _ctl_btdh_search_pcie_device - searching for pcie device
  1234. * @ioc: per adapter object
  1235. * @btdh: btdh ioctl payload
  1236. */
  1237. static int
  1238. _ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
  1239. struct mpt3_ioctl_btdh_mapping *btdh)
  1240. {
  1241. struct _pcie_device *pcie_device;
  1242. unsigned long flags;
  1243. int rc = 0;
  1244. if (list_empty(&ioc->pcie_device_list))
  1245. return rc;
  1246. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  1247. list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
  1248. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1249. btdh->handle == pcie_device->handle) {
  1250. btdh->bus = pcie_device->channel;
  1251. btdh->id = pcie_device->id;
  1252. rc = 1;
  1253. goto out;
  1254. } else if (btdh->bus == pcie_device->channel && btdh->id ==
  1255. pcie_device->id && btdh->handle == 0xFFFF) {
  1256. btdh->handle = pcie_device->handle;
  1257. rc = 1;
  1258. goto out;
  1259. }
  1260. }
  1261. out:
  1262. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  1263. return rc;
  1264. }
  1265. /**
  1266. * _ctl_btdh_search_raid_device - searching for raid device
  1267. * @ioc: per adapter object
  1268. * @btdh: btdh ioctl payload
  1269. */
  1270. static int
  1271. _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
  1272. struct mpt3_ioctl_btdh_mapping *btdh)
  1273. {
  1274. struct _raid_device *raid_device;
  1275. unsigned long flags;
  1276. int rc = 0;
  1277. if (list_empty(&ioc->raid_device_list))
  1278. return rc;
  1279. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1280. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  1281. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1282. btdh->handle == raid_device->handle) {
  1283. btdh->bus = raid_device->channel;
  1284. btdh->id = raid_device->id;
  1285. rc = 1;
  1286. goto out;
  1287. } else if (btdh->bus == raid_device->channel && btdh->id ==
  1288. raid_device->id && btdh->handle == 0xFFFF) {
  1289. btdh->handle = raid_device->handle;
  1290. rc = 1;
  1291. goto out;
  1292. }
  1293. }
  1294. out:
  1295. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1296. return rc;
  1297. }
  1298. /**
  1299. * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
  1300. * @ioc: per adapter object
  1301. * @arg - user space buffer containing ioctl content
  1302. */
  1303. static long
  1304. _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1305. {
  1306. struct mpt3_ioctl_btdh_mapping karg;
  1307. int rc;
  1308. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1309. pr_err("failure at %s:%d/%s()!\n",
  1310. __FILE__, __LINE__, __func__);
  1311. return -EFAULT;
  1312. }
  1313. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1314. __func__));
  1315. rc = _ctl_btdh_search_sas_device(ioc, &karg);
  1316. if (!rc)
  1317. rc = _ctl_btdh_search_pcie_device(ioc, &karg);
  1318. if (!rc)
  1319. _ctl_btdh_search_raid_device(ioc, &karg);
  1320. if (copy_to_user(arg, &karg, sizeof(karg))) {
  1321. pr_err("failure at %s:%d/%s()!\n",
  1322. __FILE__, __LINE__, __func__);
  1323. return -EFAULT;
  1324. }
  1325. return 0;
  1326. }
  1327. /**
  1328. * _ctl_diag_capability - return diag buffer capability
  1329. * @ioc: per adapter object
  1330. * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
  1331. *
  1332. * returns 1 when diag buffer support is enabled in firmware
  1333. */
  1334. static u8
  1335. _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
  1336. {
  1337. u8 rc = 0;
  1338. switch (buffer_type) {
  1339. case MPI2_DIAG_BUF_TYPE_TRACE:
  1340. if (ioc->facts.IOCCapabilities &
  1341. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
  1342. rc = 1;
  1343. break;
  1344. case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
  1345. if (ioc->facts.IOCCapabilities &
  1346. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
  1347. rc = 1;
  1348. break;
  1349. case MPI2_DIAG_BUF_TYPE_EXTENDED:
  1350. if (ioc->facts.IOCCapabilities &
  1351. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
  1352. rc = 1;
  1353. }
  1354. return rc;
  1355. }
  1356. /**
  1357. * _ctl_diag_register_2 - wrapper for registering diag buffer support
  1358. * @ioc: per adapter object
  1359. * @diag_register: the diag_register struct passed in from user space
  1360. *
  1361. */
  1362. static long
  1363. _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
  1364. struct mpt3_diag_register *diag_register)
  1365. {
  1366. int rc, i;
  1367. void *request_data = NULL;
  1368. dma_addr_t request_data_dma;
  1369. u32 request_data_sz = 0;
  1370. Mpi2DiagBufferPostRequest_t *mpi_request;
  1371. Mpi2DiagBufferPostReply_t *mpi_reply;
  1372. u8 buffer_type;
  1373. u16 smid;
  1374. u16 ioc_status;
  1375. u32 ioc_state;
  1376. u8 issue_reset = 0;
  1377. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1378. __func__));
  1379. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1380. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1381. pr_err(MPT3SAS_FMT
  1382. "%s: failed due to ioc not operational\n",
  1383. ioc->name, __func__);
  1384. rc = -EAGAIN;
  1385. goto out;
  1386. }
  1387. if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
  1388. pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
  1389. ioc->name, __func__);
  1390. rc = -EAGAIN;
  1391. goto out;
  1392. }
  1393. buffer_type = diag_register->buffer_type;
  1394. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1395. pr_err(MPT3SAS_FMT
  1396. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1397. ioc->name, __func__, buffer_type);
  1398. return -EPERM;
  1399. }
  1400. if (ioc->diag_buffer_status[buffer_type] &
  1401. MPT3_DIAG_BUFFER_IS_REGISTERED) {
  1402. pr_err(MPT3SAS_FMT
  1403. "%s: already has a registered buffer for buffer_type(0x%02x)\n",
  1404. ioc->name, __func__,
  1405. buffer_type);
  1406. return -EINVAL;
  1407. }
  1408. if (diag_register->requested_buffer_size % 4) {
  1409. pr_err(MPT3SAS_FMT
  1410. "%s: the requested_buffer_size is not 4 byte aligned\n",
  1411. ioc->name, __func__);
  1412. return -EINVAL;
  1413. }
  1414. smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1415. if (!smid) {
  1416. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1417. ioc->name, __func__);
  1418. rc = -EAGAIN;
  1419. goto out;
  1420. }
  1421. rc = 0;
  1422. ioc->ctl_cmds.status = MPT3_CMD_PENDING;
  1423. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1424. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1425. ioc->ctl_cmds.smid = smid;
  1426. request_data = ioc->diag_buffer[buffer_type];
  1427. request_data_sz = diag_register->requested_buffer_size;
  1428. ioc->unique_id[buffer_type] = diag_register->unique_id;
  1429. ioc->diag_buffer_status[buffer_type] = 0;
  1430. memcpy(ioc->product_specific[buffer_type],
  1431. diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
  1432. ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
  1433. if (request_data) {
  1434. request_data_dma = ioc->diag_buffer_dma[buffer_type];
  1435. if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
  1436. pci_free_consistent(ioc->pdev,
  1437. ioc->diag_buffer_sz[buffer_type],
  1438. request_data, request_data_dma);
  1439. request_data = NULL;
  1440. }
  1441. }
  1442. if (request_data == NULL) {
  1443. ioc->diag_buffer_sz[buffer_type] = 0;
  1444. ioc->diag_buffer_dma[buffer_type] = 0;
  1445. request_data = pci_alloc_consistent(
  1446. ioc->pdev, request_data_sz, &request_data_dma);
  1447. if (request_data == NULL) {
  1448. pr_err(MPT3SAS_FMT "%s: failed allocating memory" \
  1449. " for diag buffers, requested size(%d)\n",
  1450. ioc->name, __func__, request_data_sz);
  1451. mpt3sas_base_free_smid(ioc, smid);
  1452. return -ENOMEM;
  1453. }
  1454. ioc->diag_buffer[buffer_type] = request_data;
  1455. ioc->diag_buffer_sz[buffer_type] = request_data_sz;
  1456. ioc->diag_buffer_dma[buffer_type] = request_data_dma;
  1457. }
  1458. mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
  1459. mpi_request->BufferType = diag_register->buffer_type;
  1460. mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
  1461. mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
  1462. mpi_request->BufferLength = cpu_to_le32(request_data_sz);
  1463. mpi_request->VF_ID = 0; /* TODO */
  1464. mpi_request->VP_ID = 0;
  1465. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1466. "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
  1467. ioc->name, __func__, request_data,
  1468. (unsigned long long)request_data_dma,
  1469. le32_to_cpu(mpi_request->BufferLength)));
  1470. for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
  1471. mpi_request->ProductSpecific[i] =
  1472. cpu_to_le32(ioc->product_specific[buffer_type][i]);
  1473. init_completion(&ioc->ctl_cmds.done);
  1474. mpt3sas_base_put_smid_default(ioc, smid);
  1475. wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1476. MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
  1477. if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
  1478. pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
  1479. __func__);
  1480. _debug_dump_mf(mpi_request,
  1481. sizeof(Mpi2DiagBufferPostRequest_t)/4);
  1482. if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
  1483. issue_reset = 1;
  1484. goto issue_host_reset;
  1485. }
  1486. /* process the completed Reply Message Frame */
  1487. if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
  1488. pr_err(MPT3SAS_FMT "%s: no reply message\n",
  1489. ioc->name, __func__);
  1490. rc = -EFAULT;
  1491. goto out;
  1492. }
  1493. mpi_reply = ioc->ctl_cmds.reply;
  1494. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1495. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1496. ioc->diag_buffer_status[buffer_type] |=
  1497. MPT3_DIAG_BUFFER_IS_REGISTERED;
  1498. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
  1499. ioc->name, __func__));
  1500. } else {
  1501. pr_info(MPT3SAS_FMT
  1502. "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
  1503. ioc->name, __func__,
  1504. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1505. rc = -EFAULT;
  1506. }
  1507. issue_host_reset:
  1508. if (issue_reset)
  1509. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1510. out:
  1511. if (rc && request_data)
  1512. pci_free_consistent(ioc->pdev, request_data_sz,
  1513. request_data, request_data_dma);
  1514. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  1515. return rc;
  1516. }
  1517. /**
  1518. * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
  1519. * @ioc: per adapter object
  1520. * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
  1521. *
  1522. * This is called when command line option diag_buffer_enable is enabled
  1523. * at driver load time.
  1524. */
  1525. void
  1526. mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
  1527. {
  1528. struct mpt3_diag_register diag_register;
  1529. memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
  1530. if (bits_to_register & 1) {
  1531. pr_info(MPT3SAS_FMT "registering trace buffer support\n",
  1532. ioc->name);
  1533. ioc->diag_trigger_master.MasterData =
  1534. (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
  1535. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
  1536. /* register for 2MB buffers */
  1537. diag_register.requested_buffer_size = 2 * (1024 * 1024);
  1538. diag_register.unique_id = 0x7075900;
  1539. _ctl_diag_register_2(ioc, &diag_register);
  1540. }
  1541. if (bits_to_register & 2) {
  1542. pr_info(MPT3SAS_FMT "registering snapshot buffer support\n",
  1543. ioc->name);
  1544. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
  1545. /* register for 2MB buffers */
  1546. diag_register.requested_buffer_size = 2 * (1024 * 1024);
  1547. diag_register.unique_id = 0x7075901;
  1548. _ctl_diag_register_2(ioc, &diag_register);
  1549. }
  1550. if (bits_to_register & 4) {
  1551. pr_info(MPT3SAS_FMT "registering extended buffer support\n",
  1552. ioc->name);
  1553. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
  1554. /* register for 2MB buffers */
  1555. diag_register.requested_buffer_size = 2 * (1024 * 1024);
  1556. diag_register.unique_id = 0x7075901;
  1557. _ctl_diag_register_2(ioc, &diag_register);
  1558. }
  1559. }
  1560. /**
  1561. * _ctl_diag_register - application register with driver
  1562. * @ioc: per adapter object
  1563. * @arg - user space buffer containing ioctl content
  1564. *
  1565. * This will allow the driver to setup any required buffers that will be
  1566. * needed by firmware to communicate with the driver.
  1567. */
  1568. static long
  1569. _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1570. {
  1571. struct mpt3_diag_register karg;
  1572. long rc;
  1573. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1574. pr_err("failure at %s:%d/%s()!\n",
  1575. __FILE__, __LINE__, __func__);
  1576. return -EFAULT;
  1577. }
  1578. rc = _ctl_diag_register_2(ioc, &karg);
  1579. return rc;
  1580. }
  1581. /**
  1582. * _ctl_diag_unregister - application unregister with driver
  1583. * @ioc: per adapter object
  1584. * @arg - user space buffer containing ioctl content
  1585. *
  1586. * This will allow the driver to cleanup any memory allocated for diag
  1587. * messages and to free up any resources.
  1588. */
  1589. static long
  1590. _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1591. {
  1592. struct mpt3_diag_unregister karg;
  1593. void *request_data;
  1594. dma_addr_t request_data_dma;
  1595. u32 request_data_sz;
  1596. u8 buffer_type;
  1597. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1598. pr_err("failure at %s:%d/%s()!\n",
  1599. __FILE__, __LINE__, __func__);
  1600. return -EFAULT;
  1601. }
  1602. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1603. __func__));
  1604. buffer_type = karg.unique_id & 0x000000ff;
  1605. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1606. pr_err(MPT3SAS_FMT
  1607. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1608. ioc->name, __func__, buffer_type);
  1609. return -EPERM;
  1610. }
  1611. if ((ioc->diag_buffer_status[buffer_type] &
  1612. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1613. pr_err(MPT3SAS_FMT
  1614. "%s: buffer_type(0x%02x) is not registered\n",
  1615. ioc->name, __func__, buffer_type);
  1616. return -EINVAL;
  1617. }
  1618. if ((ioc->diag_buffer_status[buffer_type] &
  1619. MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
  1620. pr_err(MPT3SAS_FMT
  1621. "%s: buffer_type(0x%02x) has not been released\n",
  1622. ioc->name, __func__, buffer_type);
  1623. return -EINVAL;
  1624. }
  1625. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1626. pr_err(MPT3SAS_FMT
  1627. "%s: unique_id(0x%08x) is not registered\n",
  1628. ioc->name, __func__, karg.unique_id);
  1629. return -EINVAL;
  1630. }
  1631. request_data = ioc->diag_buffer[buffer_type];
  1632. if (!request_data) {
  1633. pr_err(MPT3SAS_FMT
  1634. "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
  1635. ioc->name, __func__, buffer_type);
  1636. return -ENOMEM;
  1637. }
  1638. request_data_sz = ioc->diag_buffer_sz[buffer_type];
  1639. request_data_dma = ioc->diag_buffer_dma[buffer_type];
  1640. pci_free_consistent(ioc->pdev, request_data_sz,
  1641. request_data, request_data_dma);
  1642. ioc->diag_buffer[buffer_type] = NULL;
  1643. ioc->diag_buffer_status[buffer_type] = 0;
  1644. return 0;
  1645. }
  1646. /**
  1647. * _ctl_diag_query - query relevant info associated with diag buffers
  1648. * @ioc: per adapter object
  1649. * @arg - user space buffer containing ioctl content
  1650. *
  1651. * The application will send only buffer_type and unique_id. Driver will
  1652. * inspect unique_id first, if valid, fill in all the info. If unique_id is
  1653. * 0x00, the driver will return info specified by Buffer Type.
  1654. */
  1655. static long
  1656. _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1657. {
  1658. struct mpt3_diag_query karg;
  1659. void *request_data;
  1660. int i;
  1661. u8 buffer_type;
  1662. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1663. pr_err("failure at %s:%d/%s()!\n",
  1664. __FILE__, __LINE__, __func__);
  1665. return -EFAULT;
  1666. }
  1667. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1668. __func__));
  1669. karg.application_flags = 0;
  1670. buffer_type = karg.buffer_type;
  1671. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1672. pr_err(MPT3SAS_FMT
  1673. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1674. ioc->name, __func__, buffer_type);
  1675. return -EPERM;
  1676. }
  1677. if ((ioc->diag_buffer_status[buffer_type] &
  1678. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1679. pr_err(MPT3SAS_FMT
  1680. "%s: buffer_type(0x%02x) is not registered\n",
  1681. ioc->name, __func__, buffer_type);
  1682. return -EINVAL;
  1683. }
  1684. if (karg.unique_id & 0xffffff00) {
  1685. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1686. pr_err(MPT3SAS_FMT
  1687. "%s: unique_id(0x%08x) is not registered\n",
  1688. ioc->name, __func__, karg.unique_id);
  1689. return -EINVAL;
  1690. }
  1691. }
  1692. request_data = ioc->diag_buffer[buffer_type];
  1693. if (!request_data) {
  1694. pr_err(MPT3SAS_FMT
  1695. "%s: doesn't have buffer for buffer_type(0x%02x)\n",
  1696. ioc->name, __func__, buffer_type);
  1697. return -ENOMEM;
  1698. }
  1699. if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
  1700. karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
  1701. MPT3_APP_FLAGS_BUFFER_VALID);
  1702. else
  1703. karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
  1704. MPT3_APP_FLAGS_BUFFER_VALID |
  1705. MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
  1706. for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
  1707. karg.product_specific[i] =
  1708. ioc->product_specific[buffer_type][i];
  1709. karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
  1710. karg.driver_added_buffer_size = 0;
  1711. karg.unique_id = ioc->unique_id[buffer_type];
  1712. karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
  1713. if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
  1714. pr_err(MPT3SAS_FMT
  1715. "%s: unable to write mpt3_diag_query data @ %p\n",
  1716. ioc->name, __func__, arg);
  1717. return -EFAULT;
  1718. }
  1719. return 0;
  1720. }
  1721. /**
  1722. * mpt3sas_send_diag_release - Diag Release Message
  1723. * @ioc: per adapter object
  1724. * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
  1725. * @issue_reset - specifies whether host reset is required.
  1726. *
  1727. */
  1728. int
  1729. mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
  1730. u8 *issue_reset)
  1731. {
  1732. Mpi2DiagReleaseRequest_t *mpi_request;
  1733. Mpi2DiagReleaseReply_t *mpi_reply;
  1734. u16 smid;
  1735. u16 ioc_status;
  1736. u32 ioc_state;
  1737. int rc;
  1738. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1739. __func__));
  1740. rc = 0;
  1741. *issue_reset = 0;
  1742. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1743. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1744. if (ioc->diag_buffer_status[buffer_type] &
  1745. MPT3_DIAG_BUFFER_IS_REGISTERED)
  1746. ioc->diag_buffer_status[buffer_type] |=
  1747. MPT3_DIAG_BUFFER_IS_RELEASED;
  1748. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1749. "%s: skipping due to FAULT state\n", ioc->name,
  1750. __func__));
  1751. rc = -EAGAIN;
  1752. goto out;
  1753. }
  1754. if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
  1755. pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
  1756. ioc->name, __func__);
  1757. rc = -EAGAIN;
  1758. goto out;
  1759. }
  1760. smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1761. if (!smid) {
  1762. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1763. ioc->name, __func__);
  1764. rc = -EAGAIN;
  1765. goto out;
  1766. }
  1767. ioc->ctl_cmds.status = MPT3_CMD_PENDING;
  1768. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1769. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1770. ioc->ctl_cmds.smid = smid;
  1771. mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
  1772. mpi_request->BufferType = buffer_type;
  1773. mpi_request->VF_ID = 0; /* TODO */
  1774. mpi_request->VP_ID = 0;
  1775. init_completion(&ioc->ctl_cmds.done);
  1776. mpt3sas_base_put_smid_default(ioc, smid);
  1777. wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1778. MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
  1779. if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
  1780. pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
  1781. __func__);
  1782. _debug_dump_mf(mpi_request,
  1783. sizeof(Mpi2DiagReleaseRequest_t)/4);
  1784. if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
  1785. *issue_reset = 1;
  1786. rc = -EFAULT;
  1787. goto out;
  1788. }
  1789. /* process the completed Reply Message Frame */
  1790. if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
  1791. pr_err(MPT3SAS_FMT "%s: no reply message\n",
  1792. ioc->name, __func__);
  1793. rc = -EFAULT;
  1794. goto out;
  1795. }
  1796. mpi_reply = ioc->ctl_cmds.reply;
  1797. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1798. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1799. ioc->diag_buffer_status[buffer_type] |=
  1800. MPT3_DIAG_BUFFER_IS_RELEASED;
  1801. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
  1802. ioc->name, __func__));
  1803. } else {
  1804. pr_info(MPT3SAS_FMT
  1805. "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
  1806. ioc->name, __func__,
  1807. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1808. rc = -EFAULT;
  1809. }
  1810. out:
  1811. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  1812. return rc;
  1813. }
  1814. /**
  1815. * _ctl_diag_release - request to send Diag Release Message to firmware
  1816. * @arg - user space buffer containing ioctl content
  1817. *
  1818. * This allows ownership of the specified buffer to returned to the driver,
  1819. * allowing an application to read the buffer without fear that firmware is
  1820. * overwriting information in the buffer.
  1821. */
  1822. static long
  1823. _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1824. {
  1825. struct mpt3_diag_release karg;
  1826. void *request_data;
  1827. int rc;
  1828. u8 buffer_type;
  1829. u8 issue_reset = 0;
  1830. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1831. pr_err("failure at %s:%d/%s()!\n",
  1832. __FILE__, __LINE__, __func__);
  1833. return -EFAULT;
  1834. }
  1835. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1836. __func__));
  1837. buffer_type = karg.unique_id & 0x000000ff;
  1838. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1839. pr_err(MPT3SAS_FMT
  1840. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1841. ioc->name, __func__, buffer_type);
  1842. return -EPERM;
  1843. }
  1844. if ((ioc->diag_buffer_status[buffer_type] &
  1845. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1846. pr_err(MPT3SAS_FMT
  1847. "%s: buffer_type(0x%02x) is not registered\n",
  1848. ioc->name, __func__, buffer_type);
  1849. return -EINVAL;
  1850. }
  1851. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1852. pr_err(MPT3SAS_FMT
  1853. "%s: unique_id(0x%08x) is not registered\n",
  1854. ioc->name, __func__, karg.unique_id);
  1855. return -EINVAL;
  1856. }
  1857. if (ioc->diag_buffer_status[buffer_type] &
  1858. MPT3_DIAG_BUFFER_IS_RELEASED) {
  1859. pr_err(MPT3SAS_FMT
  1860. "%s: buffer_type(0x%02x) is already released\n",
  1861. ioc->name, __func__,
  1862. buffer_type);
  1863. return 0;
  1864. }
  1865. request_data = ioc->diag_buffer[buffer_type];
  1866. if (!request_data) {
  1867. pr_err(MPT3SAS_FMT
  1868. "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
  1869. ioc->name, __func__, buffer_type);
  1870. return -ENOMEM;
  1871. }
  1872. /* buffers were released by due to host reset */
  1873. if ((ioc->diag_buffer_status[buffer_type] &
  1874. MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
  1875. ioc->diag_buffer_status[buffer_type] |=
  1876. MPT3_DIAG_BUFFER_IS_RELEASED;
  1877. ioc->diag_buffer_status[buffer_type] &=
  1878. ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
  1879. pr_err(MPT3SAS_FMT
  1880. "%s: buffer_type(0x%02x) was released due to host reset\n",
  1881. ioc->name, __func__, buffer_type);
  1882. return 0;
  1883. }
  1884. rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
  1885. if (issue_reset)
  1886. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1887. return rc;
  1888. }
  1889. /**
  1890. * _ctl_diag_read_buffer - request for copy of the diag buffer
  1891. * @ioc: per adapter object
  1892. * @arg - user space buffer containing ioctl content
  1893. */
  1894. static long
  1895. _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1896. {
  1897. struct mpt3_diag_read_buffer karg;
  1898. struct mpt3_diag_read_buffer __user *uarg = arg;
  1899. void *request_data, *diag_data;
  1900. Mpi2DiagBufferPostRequest_t *mpi_request;
  1901. Mpi2DiagBufferPostReply_t *mpi_reply;
  1902. int rc, i;
  1903. u8 buffer_type;
  1904. unsigned long request_size, copy_size;
  1905. u16 smid;
  1906. u16 ioc_status;
  1907. u8 issue_reset = 0;
  1908. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1909. pr_err("failure at %s:%d/%s()!\n",
  1910. __FILE__, __LINE__, __func__);
  1911. return -EFAULT;
  1912. }
  1913. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1914. __func__));
  1915. buffer_type = karg.unique_id & 0x000000ff;
  1916. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1917. pr_err(MPT3SAS_FMT
  1918. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1919. ioc->name, __func__, buffer_type);
  1920. return -EPERM;
  1921. }
  1922. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1923. pr_err(MPT3SAS_FMT
  1924. "%s: unique_id(0x%08x) is not registered\n",
  1925. ioc->name, __func__, karg.unique_id);
  1926. return -EINVAL;
  1927. }
  1928. request_data = ioc->diag_buffer[buffer_type];
  1929. if (!request_data) {
  1930. pr_err(MPT3SAS_FMT
  1931. "%s: doesn't have buffer for buffer_type(0x%02x)\n",
  1932. ioc->name, __func__, buffer_type);
  1933. return -ENOMEM;
  1934. }
  1935. request_size = ioc->diag_buffer_sz[buffer_type];
  1936. if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
  1937. pr_err(MPT3SAS_FMT "%s: either the starting_offset " \
  1938. "or bytes_to_read are not 4 byte aligned\n", ioc->name,
  1939. __func__);
  1940. return -EINVAL;
  1941. }
  1942. if (karg.starting_offset > request_size)
  1943. return -EINVAL;
  1944. diag_data = (void *)(request_data + karg.starting_offset);
  1945. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1946. "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
  1947. ioc->name, __func__,
  1948. diag_data, karg.starting_offset, karg.bytes_to_read));
  1949. /* Truncate data on requests that are too large */
  1950. if ((diag_data + karg.bytes_to_read < diag_data) ||
  1951. (diag_data + karg.bytes_to_read > request_data + request_size))
  1952. copy_size = request_size - karg.starting_offset;
  1953. else
  1954. copy_size = karg.bytes_to_read;
  1955. if (copy_to_user((void __user *)uarg->diagnostic_data,
  1956. diag_data, copy_size)) {
  1957. pr_err(MPT3SAS_FMT
  1958. "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
  1959. ioc->name, __func__, diag_data);
  1960. return -EFAULT;
  1961. }
  1962. if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
  1963. return 0;
  1964. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1965. "%s: Reregister buffer_type(0x%02x)\n",
  1966. ioc->name, __func__, buffer_type));
  1967. if ((ioc->diag_buffer_status[buffer_type] &
  1968. MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
  1969. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1970. "%s: buffer_type(0x%02x) is still registered\n",
  1971. ioc->name, __func__, buffer_type));
  1972. return 0;
  1973. }
  1974. /* Get a free request frame and save the message context.
  1975. */
  1976. if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
  1977. pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
  1978. ioc->name, __func__);
  1979. rc = -EAGAIN;
  1980. goto out;
  1981. }
  1982. smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1983. if (!smid) {
  1984. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1985. ioc->name, __func__);
  1986. rc = -EAGAIN;
  1987. goto out;
  1988. }
  1989. rc = 0;
  1990. ioc->ctl_cmds.status = MPT3_CMD_PENDING;
  1991. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1992. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1993. ioc->ctl_cmds.smid = smid;
  1994. mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
  1995. mpi_request->BufferType = buffer_type;
  1996. mpi_request->BufferLength =
  1997. cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
  1998. mpi_request->BufferAddress =
  1999. cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
  2000. for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
  2001. mpi_request->ProductSpecific[i] =
  2002. cpu_to_le32(ioc->product_specific[buffer_type][i]);
  2003. mpi_request->VF_ID = 0; /* TODO */
  2004. mpi_request->VP_ID = 0;
  2005. init_completion(&ioc->ctl_cmds.done);
  2006. mpt3sas_base_put_smid_default(ioc, smid);
  2007. wait_for_completion_timeout(&ioc->ctl_cmds.done,
  2008. MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
  2009. if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
  2010. pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
  2011. __func__);
  2012. _debug_dump_mf(mpi_request,
  2013. sizeof(Mpi2DiagBufferPostRequest_t)/4);
  2014. if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
  2015. issue_reset = 1;
  2016. goto issue_host_reset;
  2017. }
  2018. /* process the completed Reply Message Frame */
  2019. if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
  2020. pr_err(MPT3SAS_FMT "%s: no reply message\n",
  2021. ioc->name, __func__);
  2022. rc = -EFAULT;
  2023. goto out;
  2024. }
  2025. mpi_reply = ioc->ctl_cmds.reply;
  2026. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  2027. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  2028. ioc->diag_buffer_status[buffer_type] |=
  2029. MPT3_DIAG_BUFFER_IS_REGISTERED;
  2030. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
  2031. ioc->name, __func__));
  2032. } else {
  2033. pr_info(MPT3SAS_FMT
  2034. "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
  2035. ioc->name, __func__,
  2036. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  2037. rc = -EFAULT;
  2038. }
  2039. issue_host_reset:
  2040. if (issue_reset)
  2041. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  2042. out:
  2043. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  2044. return rc;
  2045. }
  2046. #ifdef CONFIG_COMPAT
  2047. /**
  2048. * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
  2049. * @ioc: per adapter object
  2050. * @cmd - ioctl opcode
  2051. * @arg - (struct mpt3_ioctl_command32)
  2052. *
  2053. * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
  2054. */
  2055. static long
  2056. _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
  2057. void __user *arg)
  2058. {
  2059. struct mpt3_ioctl_command32 karg32;
  2060. struct mpt3_ioctl_command32 __user *uarg;
  2061. struct mpt3_ioctl_command karg;
  2062. if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
  2063. return -EINVAL;
  2064. uarg = (struct mpt3_ioctl_command32 __user *) arg;
  2065. if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
  2066. pr_err("failure at %s:%d/%s()!\n",
  2067. __FILE__, __LINE__, __func__);
  2068. return -EFAULT;
  2069. }
  2070. memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
  2071. karg.hdr.ioc_number = karg32.hdr.ioc_number;
  2072. karg.hdr.port_number = karg32.hdr.port_number;
  2073. karg.hdr.max_data_size = karg32.hdr.max_data_size;
  2074. karg.timeout = karg32.timeout;
  2075. karg.max_reply_bytes = karg32.max_reply_bytes;
  2076. karg.data_in_size = karg32.data_in_size;
  2077. karg.data_out_size = karg32.data_out_size;
  2078. karg.max_sense_bytes = karg32.max_sense_bytes;
  2079. karg.data_sge_offset = karg32.data_sge_offset;
  2080. karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
  2081. karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
  2082. karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
  2083. karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
  2084. return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
  2085. }
  2086. #endif
  2087. /**
  2088. * _ctl_ioctl_main - main ioctl entry point
  2089. * @file - (struct file)
  2090. * @cmd - ioctl opcode
  2091. * @arg - user space data buffer
  2092. * @compat - handles 32 bit applications in 64bit os
  2093. * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
  2094. * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
  2095. */
  2096. static long
  2097. _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
  2098. u8 compat, u16 mpi_version)
  2099. {
  2100. struct MPT3SAS_ADAPTER *ioc;
  2101. struct mpt3_ioctl_header ioctl_header;
  2102. enum block_state state;
  2103. long ret = -EINVAL;
  2104. /* get IOCTL header */
  2105. if (copy_from_user(&ioctl_header, (char __user *)arg,
  2106. sizeof(struct mpt3_ioctl_header))) {
  2107. pr_err("failure at %s:%d/%s()!\n",
  2108. __FILE__, __LINE__, __func__);
  2109. return -EFAULT;
  2110. }
  2111. if (_ctl_verify_adapter(ioctl_header.ioc_number,
  2112. &ioc, mpi_version) == -1 || !ioc)
  2113. return -ENODEV;
  2114. /* pci_access_mutex lock acquired by ioctl path */
  2115. mutex_lock(&ioc->pci_access_mutex);
  2116. if (ioc->shost_recovery || ioc->pci_error_recovery ||
  2117. ioc->is_driver_loading || ioc->remove_host) {
  2118. ret = -EAGAIN;
  2119. goto out_unlock_pciaccess;
  2120. }
  2121. state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
  2122. if (state == NON_BLOCKING) {
  2123. if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
  2124. ret = -EAGAIN;
  2125. goto out_unlock_pciaccess;
  2126. }
  2127. } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
  2128. ret = -ERESTARTSYS;
  2129. goto out_unlock_pciaccess;
  2130. }
  2131. switch (cmd) {
  2132. case MPT3IOCINFO:
  2133. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
  2134. ret = _ctl_getiocinfo(ioc, arg);
  2135. break;
  2136. #ifdef CONFIG_COMPAT
  2137. case MPT3COMMAND32:
  2138. #endif
  2139. case MPT3COMMAND:
  2140. {
  2141. struct mpt3_ioctl_command __user *uarg;
  2142. struct mpt3_ioctl_command karg;
  2143. #ifdef CONFIG_COMPAT
  2144. if (compat) {
  2145. ret = _ctl_compat_mpt_command(ioc, cmd, arg);
  2146. break;
  2147. }
  2148. #endif
  2149. if (copy_from_user(&karg, arg, sizeof(karg))) {
  2150. pr_err("failure at %s:%d/%s()!\n",
  2151. __FILE__, __LINE__, __func__);
  2152. ret = -EFAULT;
  2153. break;
  2154. }
  2155. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
  2156. uarg = arg;
  2157. ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
  2158. }
  2159. break;
  2160. }
  2161. case MPT3EVENTQUERY:
  2162. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
  2163. ret = _ctl_eventquery(ioc, arg);
  2164. break;
  2165. case MPT3EVENTENABLE:
  2166. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
  2167. ret = _ctl_eventenable(ioc, arg);
  2168. break;
  2169. case MPT3EVENTREPORT:
  2170. ret = _ctl_eventreport(ioc, arg);
  2171. break;
  2172. case MPT3HARDRESET:
  2173. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
  2174. ret = _ctl_do_reset(ioc, arg);
  2175. break;
  2176. case MPT3BTDHMAPPING:
  2177. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
  2178. ret = _ctl_btdh_mapping(ioc, arg);
  2179. break;
  2180. case MPT3DIAGREGISTER:
  2181. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
  2182. ret = _ctl_diag_register(ioc, arg);
  2183. break;
  2184. case MPT3DIAGUNREGISTER:
  2185. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
  2186. ret = _ctl_diag_unregister(ioc, arg);
  2187. break;
  2188. case MPT3DIAGQUERY:
  2189. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
  2190. ret = _ctl_diag_query(ioc, arg);
  2191. break;
  2192. case MPT3DIAGRELEASE:
  2193. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
  2194. ret = _ctl_diag_release(ioc, arg);
  2195. break;
  2196. case MPT3DIAGREADBUFFER:
  2197. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
  2198. ret = _ctl_diag_read_buffer(ioc, arg);
  2199. break;
  2200. default:
  2201. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  2202. "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
  2203. break;
  2204. }
  2205. mutex_unlock(&ioc->ctl_cmds.mutex);
  2206. out_unlock_pciaccess:
  2207. mutex_unlock(&ioc->pci_access_mutex);
  2208. return ret;
  2209. }
  2210. /**
  2211. * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
  2212. * @file - (struct file)
  2213. * @cmd - ioctl opcode
  2214. * @arg -
  2215. */
  2216. static long
  2217. _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2218. {
  2219. long ret;
  2220. /* pass MPI25_VERSION | MPI26_VERSION value,
  2221. * to indicate that this ioctl cmd
  2222. * came from mpt3ctl ioctl device.
  2223. */
  2224. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
  2225. MPI25_VERSION | MPI26_VERSION);
  2226. return ret;
  2227. }
  2228. /**
  2229. * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
  2230. * @file - (struct file)
  2231. * @cmd - ioctl opcode
  2232. * @arg -
  2233. */
  2234. static long
  2235. _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2236. {
  2237. long ret;
  2238. /* pass MPI2_VERSION value, to indicate that this ioctl cmd
  2239. * came from mpt2ctl ioctl device.
  2240. */
  2241. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
  2242. return ret;
  2243. }
  2244. #ifdef CONFIG_COMPAT
  2245. /**
  2246. *_ ctl_ioctl_compat - main ioctl entry point (compat)
  2247. * @file -
  2248. * @cmd -
  2249. * @arg -
  2250. *
  2251. * This routine handles 32 bit applications in 64bit os.
  2252. */
  2253. static long
  2254. _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
  2255. {
  2256. long ret;
  2257. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
  2258. MPI25_VERSION | MPI26_VERSION);
  2259. return ret;
  2260. }
  2261. /**
  2262. *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
  2263. * @file -
  2264. * @cmd -
  2265. * @arg -
  2266. *
  2267. * This routine handles 32 bit applications in 64bit os.
  2268. */
  2269. static long
  2270. _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
  2271. {
  2272. long ret;
  2273. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
  2274. return ret;
  2275. }
  2276. #endif
  2277. /* scsi host attributes */
  2278. /**
  2279. * _ctl_version_fw_show - firmware version
  2280. * @cdev - pointer to embedded class device
  2281. * @buf - the buffer returned
  2282. *
  2283. * A sysfs 'read-only' shost attribute.
  2284. */
  2285. static ssize_t
  2286. _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
  2287. char *buf)
  2288. {
  2289. struct Scsi_Host *shost = class_to_shost(cdev);
  2290. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2291. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2292. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  2293. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  2294. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  2295. ioc->facts.FWVersion.Word & 0x000000FF);
  2296. }
  2297. static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
  2298. /**
  2299. * _ctl_version_bios_show - bios version
  2300. * @cdev - pointer to embedded class device
  2301. * @buf - the buffer returned
  2302. *
  2303. * A sysfs 'read-only' shost attribute.
  2304. */
  2305. static ssize_t
  2306. _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
  2307. char *buf)
  2308. {
  2309. struct Scsi_Host *shost = class_to_shost(cdev);
  2310. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2311. u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  2312. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2313. (version & 0xFF000000) >> 24,
  2314. (version & 0x00FF0000) >> 16,
  2315. (version & 0x0000FF00) >> 8,
  2316. version & 0x000000FF);
  2317. }
  2318. static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
  2319. /**
  2320. * _ctl_version_mpi_show - MPI (message passing interface) version
  2321. * @cdev - pointer to embedded class device
  2322. * @buf - the buffer returned
  2323. *
  2324. * A sysfs 'read-only' shost attribute.
  2325. */
  2326. static ssize_t
  2327. _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
  2328. char *buf)
  2329. {
  2330. struct Scsi_Host *shost = class_to_shost(cdev);
  2331. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2332. return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
  2333. ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
  2334. }
  2335. static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
  2336. /**
  2337. * _ctl_version_product_show - product name
  2338. * @cdev - pointer to embedded class device
  2339. * @buf - the buffer returned
  2340. *
  2341. * A sysfs 'read-only' shost attribute.
  2342. */
  2343. static ssize_t
  2344. _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
  2345. char *buf)
  2346. {
  2347. struct Scsi_Host *shost = class_to_shost(cdev);
  2348. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2349. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
  2350. }
  2351. static DEVICE_ATTR(version_product, S_IRUGO, _ctl_version_product_show, NULL);
  2352. /**
  2353. * _ctl_version_nvdata_persistent_show - ndvata persistent version
  2354. * @cdev - pointer to embedded class device
  2355. * @buf - the buffer returned
  2356. *
  2357. * A sysfs 'read-only' shost attribute.
  2358. */
  2359. static ssize_t
  2360. _ctl_version_nvdata_persistent_show(struct device *cdev,
  2361. struct device_attribute *attr, char *buf)
  2362. {
  2363. struct Scsi_Host *shost = class_to_shost(cdev);
  2364. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2365. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2366. le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
  2367. }
  2368. static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
  2369. _ctl_version_nvdata_persistent_show, NULL);
  2370. /**
  2371. * _ctl_version_nvdata_default_show - nvdata default version
  2372. * @cdev - pointer to embedded class device
  2373. * @buf - the buffer returned
  2374. *
  2375. * A sysfs 'read-only' shost attribute.
  2376. */
  2377. static ssize_t
  2378. _ctl_version_nvdata_default_show(struct device *cdev, struct device_attribute
  2379. *attr, char *buf)
  2380. {
  2381. struct Scsi_Host *shost = class_to_shost(cdev);
  2382. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2383. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2384. le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
  2385. }
  2386. static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
  2387. _ctl_version_nvdata_default_show, NULL);
  2388. /**
  2389. * _ctl_board_name_show - board name
  2390. * @cdev - pointer to embedded class device
  2391. * @buf - the buffer returned
  2392. *
  2393. * A sysfs 'read-only' shost attribute.
  2394. */
  2395. static ssize_t
  2396. _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
  2397. char *buf)
  2398. {
  2399. struct Scsi_Host *shost = class_to_shost(cdev);
  2400. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2401. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
  2402. }
  2403. static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
  2404. /**
  2405. * _ctl_board_assembly_show - board assembly name
  2406. * @cdev - pointer to embedded class device
  2407. * @buf - the buffer returned
  2408. *
  2409. * A sysfs 'read-only' shost attribute.
  2410. */
  2411. static ssize_t
  2412. _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
  2413. char *buf)
  2414. {
  2415. struct Scsi_Host *shost = class_to_shost(cdev);
  2416. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2417. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
  2418. }
  2419. static DEVICE_ATTR(board_assembly, S_IRUGO, _ctl_board_assembly_show, NULL);
  2420. /**
  2421. * _ctl_board_tracer_show - board tracer number
  2422. * @cdev - pointer to embedded class device
  2423. * @buf - the buffer returned
  2424. *
  2425. * A sysfs 'read-only' shost attribute.
  2426. */
  2427. static ssize_t
  2428. _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
  2429. char *buf)
  2430. {
  2431. struct Scsi_Host *shost = class_to_shost(cdev);
  2432. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2433. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
  2434. }
  2435. static DEVICE_ATTR(board_tracer, S_IRUGO, _ctl_board_tracer_show, NULL);
  2436. /**
  2437. * _ctl_io_delay_show - io missing delay
  2438. * @cdev - pointer to embedded class device
  2439. * @buf - the buffer returned
  2440. *
  2441. * This is for firmware implemention for deboucing device
  2442. * removal events.
  2443. *
  2444. * A sysfs 'read-only' shost attribute.
  2445. */
  2446. static ssize_t
  2447. _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
  2448. char *buf)
  2449. {
  2450. struct Scsi_Host *shost = class_to_shost(cdev);
  2451. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2452. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
  2453. }
  2454. static DEVICE_ATTR(io_delay, S_IRUGO, _ctl_io_delay_show, NULL);
  2455. /**
  2456. * _ctl_device_delay_show - device missing delay
  2457. * @cdev - pointer to embedded class device
  2458. * @buf - the buffer returned
  2459. *
  2460. * This is for firmware implemention for deboucing device
  2461. * removal events.
  2462. *
  2463. * A sysfs 'read-only' shost attribute.
  2464. */
  2465. static ssize_t
  2466. _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
  2467. char *buf)
  2468. {
  2469. struct Scsi_Host *shost = class_to_shost(cdev);
  2470. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2471. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
  2472. }
  2473. static DEVICE_ATTR(device_delay, S_IRUGO, _ctl_device_delay_show, NULL);
  2474. /**
  2475. * _ctl_fw_queue_depth_show - global credits
  2476. * @cdev - pointer to embedded class device
  2477. * @buf - the buffer returned
  2478. *
  2479. * This is firmware queue depth limit
  2480. *
  2481. * A sysfs 'read-only' shost attribute.
  2482. */
  2483. static ssize_t
  2484. _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
  2485. char *buf)
  2486. {
  2487. struct Scsi_Host *shost = class_to_shost(cdev);
  2488. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2489. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
  2490. }
  2491. static DEVICE_ATTR(fw_queue_depth, S_IRUGO, _ctl_fw_queue_depth_show, NULL);
  2492. /**
  2493. * _ctl_sas_address_show - sas address
  2494. * @cdev - pointer to embedded class device
  2495. * @buf - the buffer returned
  2496. *
  2497. * This is the controller sas address
  2498. *
  2499. * A sysfs 'read-only' shost attribute.
  2500. */
  2501. static ssize_t
  2502. _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
  2503. char *buf)
  2504. {
  2505. struct Scsi_Host *shost = class_to_shost(cdev);
  2506. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2507. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  2508. (unsigned long long)ioc->sas_hba.sas_address);
  2509. }
  2510. static DEVICE_ATTR(host_sas_address, S_IRUGO,
  2511. _ctl_host_sas_address_show, NULL);
  2512. /**
  2513. * _ctl_logging_level_show - logging level
  2514. * @cdev - pointer to embedded class device
  2515. * @buf - the buffer returned
  2516. *
  2517. * A sysfs 'read/write' shost attribute.
  2518. */
  2519. static ssize_t
  2520. _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
  2521. char *buf)
  2522. {
  2523. struct Scsi_Host *shost = class_to_shost(cdev);
  2524. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2525. return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
  2526. }
  2527. static ssize_t
  2528. _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
  2529. const char *buf, size_t count)
  2530. {
  2531. struct Scsi_Host *shost = class_to_shost(cdev);
  2532. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2533. int val = 0;
  2534. if (sscanf(buf, "%x", &val) != 1)
  2535. return -EINVAL;
  2536. ioc->logging_level = val;
  2537. pr_info(MPT3SAS_FMT "logging_level=%08xh\n", ioc->name,
  2538. ioc->logging_level);
  2539. return strlen(buf);
  2540. }
  2541. static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR, _ctl_logging_level_show,
  2542. _ctl_logging_level_store);
  2543. /**
  2544. * _ctl_fwfault_debug_show - show/store fwfault_debug
  2545. * @cdev - pointer to embedded class device
  2546. * @buf - the buffer returned
  2547. *
  2548. * mpt3sas_fwfault_debug is command line option
  2549. * A sysfs 'read/write' shost attribute.
  2550. */
  2551. static ssize_t
  2552. _ctl_fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
  2553. char *buf)
  2554. {
  2555. struct Scsi_Host *shost = class_to_shost(cdev);
  2556. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2557. return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
  2558. }
  2559. static ssize_t
  2560. _ctl_fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
  2561. const char *buf, size_t count)
  2562. {
  2563. struct Scsi_Host *shost = class_to_shost(cdev);
  2564. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2565. int val = 0;
  2566. if (sscanf(buf, "%d", &val) != 1)
  2567. return -EINVAL;
  2568. ioc->fwfault_debug = val;
  2569. pr_info(MPT3SAS_FMT "fwfault_debug=%d\n", ioc->name,
  2570. ioc->fwfault_debug);
  2571. return strlen(buf);
  2572. }
  2573. static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
  2574. _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
  2575. /**
  2576. * _ctl_ioc_reset_count_show - ioc reset count
  2577. * @cdev - pointer to embedded class device
  2578. * @buf - the buffer returned
  2579. *
  2580. * This is firmware queue depth limit
  2581. *
  2582. * A sysfs 'read-only' shost attribute.
  2583. */
  2584. static ssize_t
  2585. _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
  2586. char *buf)
  2587. {
  2588. struct Scsi_Host *shost = class_to_shost(cdev);
  2589. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2590. return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
  2591. }
  2592. static DEVICE_ATTR(ioc_reset_count, S_IRUGO, _ctl_ioc_reset_count_show, NULL);
  2593. /**
  2594. * _ctl_ioc_reply_queue_count_show - number of reply queues
  2595. * @cdev - pointer to embedded class device
  2596. * @buf - the buffer returned
  2597. *
  2598. * This is number of reply queues
  2599. *
  2600. * A sysfs 'read-only' shost attribute.
  2601. */
  2602. static ssize_t
  2603. _ctl_ioc_reply_queue_count_show(struct device *cdev,
  2604. struct device_attribute *attr, char *buf)
  2605. {
  2606. u8 reply_queue_count;
  2607. struct Scsi_Host *shost = class_to_shost(cdev);
  2608. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2609. if ((ioc->facts.IOCCapabilities &
  2610. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
  2611. reply_queue_count = ioc->reply_queue_count;
  2612. else
  2613. reply_queue_count = 1;
  2614. return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
  2615. }
  2616. static DEVICE_ATTR(reply_queue_count, S_IRUGO, _ctl_ioc_reply_queue_count_show,
  2617. NULL);
  2618. /**
  2619. * _ctl_BRM_status_show - Backup Rail Monitor Status
  2620. * @cdev - pointer to embedded class device
  2621. * @buf - the buffer returned
  2622. *
  2623. * This is number of reply queues
  2624. *
  2625. * A sysfs 'read-only' shost attribute.
  2626. */
  2627. static ssize_t
  2628. _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
  2629. char *buf)
  2630. {
  2631. struct Scsi_Host *shost = class_to_shost(cdev);
  2632. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2633. Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
  2634. Mpi2ConfigReply_t mpi_reply;
  2635. u16 backup_rail_monitor_status = 0;
  2636. u16 ioc_status;
  2637. int sz;
  2638. ssize_t rc = 0;
  2639. if (!ioc->is_warpdrive) {
  2640. pr_err(MPT3SAS_FMT "%s: BRM attribute is only for"
  2641. " warpdrive\n", ioc->name, __func__);
  2642. goto out;
  2643. }
  2644. /* pci_access_mutex lock acquired by sysfs show path */
  2645. mutex_lock(&ioc->pci_access_mutex);
  2646. if (ioc->pci_error_recovery || ioc->remove_host) {
  2647. mutex_unlock(&ioc->pci_access_mutex);
  2648. return 0;
  2649. }
  2650. /* allocate upto GPIOVal 36 entries */
  2651. sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
  2652. io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
  2653. if (!io_unit_pg3) {
  2654. pr_err(MPT3SAS_FMT "%s: failed allocating memory "
  2655. "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
  2656. goto out;
  2657. }
  2658. if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
  2659. 0) {
  2660. pr_err(MPT3SAS_FMT
  2661. "%s: failed reading iounit_pg3\n", ioc->name,
  2662. __func__);
  2663. goto out;
  2664. }
  2665. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  2666. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2667. pr_err(MPT3SAS_FMT "%s: iounit_pg3 failed with "
  2668. "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
  2669. goto out;
  2670. }
  2671. if (io_unit_pg3->GPIOCount < 25) {
  2672. pr_err(MPT3SAS_FMT "%s: iounit_pg3->GPIOCount less than "
  2673. "25 entries, detected (%d) entries\n", ioc->name, __func__,
  2674. io_unit_pg3->GPIOCount);
  2675. goto out;
  2676. }
  2677. /* BRM status is in bit zero of GPIOVal[24] */
  2678. backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
  2679. rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
  2680. out:
  2681. kfree(io_unit_pg3);
  2682. mutex_unlock(&ioc->pci_access_mutex);
  2683. return rc;
  2684. }
  2685. static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
  2686. struct DIAG_BUFFER_START {
  2687. __le32 Size;
  2688. __le32 DiagVersion;
  2689. u8 BufferType;
  2690. u8 Reserved[3];
  2691. __le32 Reserved1;
  2692. __le32 Reserved2;
  2693. __le32 Reserved3;
  2694. };
  2695. /**
  2696. * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
  2697. * @cdev - pointer to embedded class device
  2698. * @buf - the buffer returned
  2699. *
  2700. * A sysfs 'read-only' shost attribute.
  2701. */
  2702. static ssize_t
  2703. _ctl_host_trace_buffer_size_show(struct device *cdev,
  2704. struct device_attribute *attr, char *buf)
  2705. {
  2706. struct Scsi_Host *shost = class_to_shost(cdev);
  2707. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2708. u32 size = 0;
  2709. struct DIAG_BUFFER_START *request_data;
  2710. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2711. pr_err(MPT3SAS_FMT
  2712. "%s: host_trace_buffer is not registered\n",
  2713. ioc->name, __func__);
  2714. return 0;
  2715. }
  2716. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2717. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2718. pr_err(MPT3SAS_FMT
  2719. "%s: host_trace_buffer is not registered\n",
  2720. ioc->name, __func__);
  2721. return 0;
  2722. }
  2723. request_data = (struct DIAG_BUFFER_START *)
  2724. ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
  2725. if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
  2726. le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
  2727. le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
  2728. le32_to_cpu(request_data->Reserved3) == 0x4742444c)
  2729. size = le32_to_cpu(request_data->Size);
  2730. ioc->ring_buffer_sz = size;
  2731. return snprintf(buf, PAGE_SIZE, "%d\n", size);
  2732. }
  2733. static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
  2734. _ctl_host_trace_buffer_size_show, NULL);
  2735. /**
  2736. * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
  2737. * @cdev - pointer to embedded class device
  2738. * @buf - the buffer returned
  2739. *
  2740. * A sysfs 'read/write' shost attribute.
  2741. *
  2742. * You will only be able to read 4k bytes of ring buffer at a time.
  2743. * In order to read beyond 4k bytes, you will have to write out the
  2744. * offset to the same attribute, it will move the pointer.
  2745. */
  2746. static ssize_t
  2747. _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
  2748. char *buf)
  2749. {
  2750. struct Scsi_Host *shost = class_to_shost(cdev);
  2751. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2752. void *request_data;
  2753. u32 size;
  2754. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2755. pr_err(MPT3SAS_FMT
  2756. "%s: host_trace_buffer is not registered\n",
  2757. ioc->name, __func__);
  2758. return 0;
  2759. }
  2760. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2761. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2762. pr_err(MPT3SAS_FMT
  2763. "%s: host_trace_buffer is not registered\n",
  2764. ioc->name, __func__);
  2765. return 0;
  2766. }
  2767. if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
  2768. return 0;
  2769. size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
  2770. size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
  2771. request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
  2772. memcpy(buf, request_data, size);
  2773. return size;
  2774. }
  2775. static ssize_t
  2776. _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
  2777. const char *buf, size_t count)
  2778. {
  2779. struct Scsi_Host *shost = class_to_shost(cdev);
  2780. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2781. int val = 0;
  2782. if (sscanf(buf, "%d", &val) != 1)
  2783. return -EINVAL;
  2784. ioc->ring_buffer_offset = val;
  2785. return strlen(buf);
  2786. }
  2787. static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
  2788. _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
  2789. /*****************************************/
  2790. /**
  2791. * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
  2792. * @cdev - pointer to embedded class device
  2793. * @buf - the buffer returned
  2794. *
  2795. * A sysfs 'read/write' shost attribute.
  2796. *
  2797. * This is a mechnism to post/release host_trace_buffers
  2798. */
  2799. static ssize_t
  2800. _ctl_host_trace_buffer_enable_show(struct device *cdev,
  2801. struct device_attribute *attr, char *buf)
  2802. {
  2803. struct Scsi_Host *shost = class_to_shost(cdev);
  2804. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2805. if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
  2806. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2807. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
  2808. return snprintf(buf, PAGE_SIZE, "off\n");
  2809. else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2810. MPT3_DIAG_BUFFER_IS_RELEASED))
  2811. return snprintf(buf, PAGE_SIZE, "release\n");
  2812. else
  2813. return snprintf(buf, PAGE_SIZE, "post\n");
  2814. }
  2815. static ssize_t
  2816. _ctl_host_trace_buffer_enable_store(struct device *cdev,
  2817. struct device_attribute *attr, const char *buf, size_t count)
  2818. {
  2819. struct Scsi_Host *shost = class_to_shost(cdev);
  2820. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2821. char str[10] = "";
  2822. struct mpt3_diag_register diag_register;
  2823. u8 issue_reset = 0;
  2824. /* don't allow post/release occurr while recovery is active */
  2825. if (ioc->shost_recovery || ioc->remove_host ||
  2826. ioc->pci_error_recovery || ioc->is_driver_loading)
  2827. return -EBUSY;
  2828. if (sscanf(buf, "%9s", str) != 1)
  2829. return -EINVAL;
  2830. if (!strcmp(str, "post")) {
  2831. /* exit out if host buffers are already posted */
  2832. if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
  2833. (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2834. MPT3_DIAG_BUFFER_IS_REGISTERED) &&
  2835. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2836. MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
  2837. goto out;
  2838. memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
  2839. pr_info(MPT3SAS_FMT "posting host trace buffers\n",
  2840. ioc->name);
  2841. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
  2842. diag_register.requested_buffer_size = (1024 * 1024);
  2843. diag_register.unique_id = 0x7075900;
  2844. ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
  2845. _ctl_diag_register_2(ioc, &diag_register);
  2846. } else if (!strcmp(str, "release")) {
  2847. /* exit out if host buffers are already released */
  2848. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
  2849. goto out;
  2850. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2851. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
  2852. goto out;
  2853. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2854. MPT3_DIAG_BUFFER_IS_RELEASED))
  2855. goto out;
  2856. pr_info(MPT3SAS_FMT "releasing host trace buffer\n",
  2857. ioc->name);
  2858. mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
  2859. &issue_reset);
  2860. }
  2861. out:
  2862. return strlen(buf);
  2863. }
  2864. static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
  2865. _ctl_host_trace_buffer_enable_show,
  2866. _ctl_host_trace_buffer_enable_store);
  2867. /*********** diagnostic trigger suppport *********************************/
  2868. /**
  2869. * _ctl_diag_trigger_master_show - show the diag_trigger_master attribute
  2870. * @cdev - pointer to embedded class device
  2871. * @buf - the buffer returned
  2872. *
  2873. * A sysfs 'read/write' shost attribute.
  2874. */
  2875. static ssize_t
  2876. _ctl_diag_trigger_master_show(struct device *cdev,
  2877. struct device_attribute *attr, char *buf)
  2878. {
  2879. struct Scsi_Host *shost = class_to_shost(cdev);
  2880. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2881. unsigned long flags;
  2882. ssize_t rc;
  2883. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  2884. rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
  2885. memcpy(buf, &ioc->diag_trigger_master, rc);
  2886. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  2887. return rc;
  2888. }
  2889. /**
  2890. * _ctl_diag_trigger_master_store - store the diag_trigger_master attribute
  2891. * @cdev - pointer to embedded class device
  2892. * @buf - the buffer returned
  2893. *
  2894. * A sysfs 'read/write' shost attribute.
  2895. */
  2896. static ssize_t
  2897. _ctl_diag_trigger_master_store(struct device *cdev,
  2898. struct device_attribute *attr, const char *buf, size_t count)
  2899. {
  2900. struct Scsi_Host *shost = class_to_shost(cdev);
  2901. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2902. unsigned long flags;
  2903. ssize_t rc;
  2904. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  2905. rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
  2906. memset(&ioc->diag_trigger_master, 0,
  2907. sizeof(struct SL_WH_MASTER_TRIGGER_T));
  2908. memcpy(&ioc->diag_trigger_master, buf, rc);
  2909. ioc->diag_trigger_master.MasterData |=
  2910. (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
  2911. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  2912. return rc;
  2913. }
  2914. static DEVICE_ATTR(diag_trigger_master, S_IRUGO | S_IWUSR,
  2915. _ctl_diag_trigger_master_show, _ctl_diag_trigger_master_store);
  2916. /**
  2917. * _ctl_diag_trigger_event_show - show the diag_trigger_event attribute
  2918. * @cdev - pointer to embedded class device
  2919. * @buf - the buffer returned
  2920. *
  2921. * A sysfs 'read/write' shost attribute.
  2922. */
  2923. static ssize_t
  2924. _ctl_diag_trigger_event_show(struct device *cdev,
  2925. struct device_attribute *attr, char *buf)
  2926. {
  2927. struct Scsi_Host *shost = class_to_shost(cdev);
  2928. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2929. unsigned long flags;
  2930. ssize_t rc;
  2931. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  2932. rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
  2933. memcpy(buf, &ioc->diag_trigger_event, rc);
  2934. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  2935. return rc;
  2936. }
  2937. /**
  2938. * _ctl_diag_trigger_event_store - store the diag_trigger_event attribute
  2939. * @cdev - pointer to embedded class device
  2940. * @buf - the buffer returned
  2941. *
  2942. * A sysfs 'read/write' shost attribute.
  2943. */
  2944. static ssize_t
  2945. _ctl_diag_trigger_event_store(struct device *cdev,
  2946. struct device_attribute *attr, const char *buf, size_t count)
  2947. {
  2948. struct Scsi_Host *shost = class_to_shost(cdev);
  2949. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2950. unsigned long flags;
  2951. ssize_t sz;
  2952. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  2953. sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
  2954. memset(&ioc->diag_trigger_event, 0,
  2955. sizeof(struct SL_WH_EVENT_TRIGGERS_T));
  2956. memcpy(&ioc->diag_trigger_event, buf, sz);
  2957. if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
  2958. ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
  2959. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  2960. return sz;
  2961. }
  2962. static DEVICE_ATTR(diag_trigger_event, S_IRUGO | S_IWUSR,
  2963. _ctl_diag_trigger_event_show, _ctl_diag_trigger_event_store);
  2964. /**
  2965. * _ctl_diag_trigger_scsi_show - show the diag_trigger_scsi attribute
  2966. * @cdev - pointer to embedded class device
  2967. * @buf - the buffer returned
  2968. *
  2969. * A sysfs 'read/write' shost attribute.
  2970. */
  2971. static ssize_t
  2972. _ctl_diag_trigger_scsi_show(struct device *cdev,
  2973. struct device_attribute *attr, char *buf)
  2974. {
  2975. struct Scsi_Host *shost = class_to_shost(cdev);
  2976. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2977. unsigned long flags;
  2978. ssize_t rc;
  2979. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  2980. rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
  2981. memcpy(buf, &ioc->diag_trigger_scsi, rc);
  2982. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  2983. return rc;
  2984. }
  2985. /**
  2986. * _ctl_diag_trigger_scsi_store - store the diag_trigger_scsi attribute
  2987. * @cdev - pointer to embedded class device
  2988. * @buf - the buffer returned
  2989. *
  2990. * A sysfs 'read/write' shost attribute.
  2991. */
  2992. static ssize_t
  2993. _ctl_diag_trigger_scsi_store(struct device *cdev,
  2994. struct device_attribute *attr, const char *buf, size_t count)
  2995. {
  2996. struct Scsi_Host *shost = class_to_shost(cdev);
  2997. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2998. unsigned long flags;
  2999. ssize_t sz;
  3000. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  3001. sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
  3002. memset(&ioc->diag_trigger_scsi, 0,
  3003. sizeof(struct SL_WH_EVENT_TRIGGERS_T));
  3004. memcpy(&ioc->diag_trigger_scsi, buf, sz);
  3005. if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
  3006. ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
  3007. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  3008. return sz;
  3009. }
  3010. static DEVICE_ATTR(diag_trigger_scsi, S_IRUGO | S_IWUSR,
  3011. _ctl_diag_trigger_scsi_show, _ctl_diag_trigger_scsi_store);
  3012. /**
  3013. * _ctl_diag_trigger_scsi_show - show the diag_trigger_mpi attribute
  3014. * @cdev - pointer to embedded class device
  3015. * @buf - the buffer returned
  3016. *
  3017. * A sysfs 'read/write' shost attribute.
  3018. */
  3019. static ssize_t
  3020. _ctl_diag_trigger_mpi_show(struct device *cdev,
  3021. struct device_attribute *attr, char *buf)
  3022. {
  3023. struct Scsi_Host *shost = class_to_shost(cdev);
  3024. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  3025. unsigned long flags;
  3026. ssize_t rc;
  3027. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  3028. rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
  3029. memcpy(buf, &ioc->diag_trigger_mpi, rc);
  3030. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  3031. return rc;
  3032. }
  3033. /**
  3034. * _ctl_diag_trigger_mpi_store - store the diag_trigger_mpi attribute
  3035. * @cdev - pointer to embedded class device
  3036. * @buf - the buffer returned
  3037. *
  3038. * A sysfs 'read/write' shost attribute.
  3039. */
  3040. static ssize_t
  3041. _ctl_diag_trigger_mpi_store(struct device *cdev,
  3042. struct device_attribute *attr, const char *buf, size_t count)
  3043. {
  3044. struct Scsi_Host *shost = class_to_shost(cdev);
  3045. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  3046. unsigned long flags;
  3047. ssize_t sz;
  3048. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  3049. sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
  3050. memset(&ioc->diag_trigger_mpi, 0,
  3051. sizeof(ioc->diag_trigger_mpi));
  3052. memcpy(&ioc->diag_trigger_mpi, buf, sz);
  3053. if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
  3054. ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
  3055. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  3056. return sz;
  3057. }
  3058. static DEVICE_ATTR(diag_trigger_mpi, S_IRUGO | S_IWUSR,
  3059. _ctl_diag_trigger_mpi_show, _ctl_diag_trigger_mpi_store);
  3060. /*********** diagnostic trigger suppport *** END ****************************/
  3061. /*****************************************/
  3062. struct device_attribute *mpt3sas_host_attrs[] = {
  3063. &dev_attr_version_fw,
  3064. &dev_attr_version_bios,
  3065. &dev_attr_version_mpi,
  3066. &dev_attr_version_product,
  3067. &dev_attr_version_nvdata_persistent,
  3068. &dev_attr_version_nvdata_default,
  3069. &dev_attr_board_name,
  3070. &dev_attr_board_assembly,
  3071. &dev_attr_board_tracer,
  3072. &dev_attr_io_delay,
  3073. &dev_attr_device_delay,
  3074. &dev_attr_logging_level,
  3075. &dev_attr_fwfault_debug,
  3076. &dev_attr_fw_queue_depth,
  3077. &dev_attr_host_sas_address,
  3078. &dev_attr_ioc_reset_count,
  3079. &dev_attr_host_trace_buffer_size,
  3080. &dev_attr_host_trace_buffer,
  3081. &dev_attr_host_trace_buffer_enable,
  3082. &dev_attr_reply_queue_count,
  3083. &dev_attr_diag_trigger_master,
  3084. &dev_attr_diag_trigger_event,
  3085. &dev_attr_diag_trigger_scsi,
  3086. &dev_attr_diag_trigger_mpi,
  3087. &dev_attr_BRM_status,
  3088. NULL,
  3089. };
  3090. /* device attributes */
  3091. /**
  3092. * _ctl_device_sas_address_show - sas address
  3093. * @cdev - pointer to embedded class device
  3094. * @buf - the buffer returned
  3095. *
  3096. * This is the sas address for the target
  3097. *
  3098. * A sysfs 'read-only' shost attribute.
  3099. */
  3100. static ssize_t
  3101. _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
  3102. char *buf)
  3103. {
  3104. struct scsi_device *sdev = to_scsi_device(dev);
  3105. struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  3106. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  3107. (unsigned long long)sas_device_priv_data->sas_target->sas_address);
  3108. }
  3109. static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
  3110. /**
  3111. * _ctl_device_handle_show - device handle
  3112. * @cdev - pointer to embedded class device
  3113. * @buf - the buffer returned
  3114. *
  3115. * This is the firmware assigned device handle
  3116. *
  3117. * A sysfs 'read-only' shost attribute.
  3118. */
  3119. static ssize_t
  3120. _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
  3121. char *buf)
  3122. {
  3123. struct scsi_device *sdev = to_scsi_device(dev);
  3124. struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  3125. return snprintf(buf, PAGE_SIZE, "0x%04x\n",
  3126. sas_device_priv_data->sas_target->handle);
  3127. }
  3128. static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
  3129. /**
  3130. * _ctl_device_ncq_io_prio_show - send prioritized io commands to device
  3131. * @dev - pointer to embedded device
  3132. * @buf - the buffer returned
  3133. *
  3134. * A sysfs 'read/write' sdev attribute, only works with SATA
  3135. */
  3136. static ssize_t
  3137. _ctl_device_ncq_prio_enable_show(struct device *dev,
  3138. struct device_attribute *attr, char *buf)
  3139. {
  3140. struct scsi_device *sdev = to_scsi_device(dev);
  3141. struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  3142. return snprintf(buf, PAGE_SIZE, "%d\n",
  3143. sas_device_priv_data->ncq_prio_enable);
  3144. }
  3145. static ssize_t
  3146. _ctl_device_ncq_prio_enable_store(struct device *dev,
  3147. struct device_attribute *attr,
  3148. const char *buf, size_t count)
  3149. {
  3150. struct scsi_device *sdev = to_scsi_device(dev);
  3151. struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  3152. bool ncq_prio_enable = 0;
  3153. if (kstrtobool(buf, &ncq_prio_enable))
  3154. return -EINVAL;
  3155. if (!scsih_ncq_prio_supp(sdev))
  3156. return -EINVAL;
  3157. sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
  3158. return strlen(buf);
  3159. }
  3160. static DEVICE_ATTR(sas_ncq_prio_enable, S_IRUGO | S_IWUSR,
  3161. _ctl_device_ncq_prio_enable_show,
  3162. _ctl_device_ncq_prio_enable_store);
  3163. struct device_attribute *mpt3sas_dev_attrs[] = {
  3164. &dev_attr_sas_address,
  3165. &dev_attr_sas_device_handle,
  3166. &dev_attr_sas_ncq_prio_enable,
  3167. NULL,
  3168. };
  3169. /* file operations table for mpt3ctl device */
  3170. static const struct file_operations ctl_fops = {
  3171. .owner = THIS_MODULE,
  3172. .unlocked_ioctl = _ctl_ioctl,
  3173. .poll = _ctl_poll,
  3174. .fasync = _ctl_fasync,
  3175. #ifdef CONFIG_COMPAT
  3176. .compat_ioctl = _ctl_ioctl_compat,
  3177. #endif
  3178. };
  3179. /* file operations table for mpt2ctl device */
  3180. static const struct file_operations ctl_gen2_fops = {
  3181. .owner = THIS_MODULE,
  3182. .unlocked_ioctl = _ctl_mpt2_ioctl,
  3183. .poll = _ctl_poll,
  3184. .fasync = _ctl_fasync,
  3185. #ifdef CONFIG_COMPAT
  3186. .compat_ioctl = _ctl_mpt2_ioctl_compat,
  3187. #endif
  3188. };
  3189. static struct miscdevice ctl_dev = {
  3190. .minor = MPT3SAS_MINOR,
  3191. .name = MPT3SAS_DEV_NAME,
  3192. .fops = &ctl_fops,
  3193. };
  3194. static struct miscdevice gen2_ctl_dev = {
  3195. .minor = MPT2SAS_MINOR,
  3196. .name = MPT2SAS_DEV_NAME,
  3197. .fops = &ctl_gen2_fops,
  3198. };
  3199. /**
  3200. * mpt3sas_ctl_init - main entry point for ctl.
  3201. *
  3202. */
  3203. void
  3204. mpt3sas_ctl_init(ushort hbas_to_enumerate)
  3205. {
  3206. async_queue = NULL;
  3207. /* Don't register mpt3ctl ioctl device if
  3208. * hbas_to_enumarate is one.
  3209. */
  3210. if (hbas_to_enumerate != 1)
  3211. if (misc_register(&ctl_dev) < 0)
  3212. pr_err("%s can't register misc device [minor=%d]\n",
  3213. MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
  3214. /* Don't register mpt3ctl ioctl device if
  3215. * hbas_to_enumarate is two.
  3216. */
  3217. if (hbas_to_enumerate != 2)
  3218. if (misc_register(&gen2_ctl_dev) < 0)
  3219. pr_err("%s can't register misc device [minor=%d]\n",
  3220. MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
  3221. init_waitqueue_head(&ctl_poll_wait);
  3222. }
  3223. /**
  3224. * mpt3sas_ctl_exit - exit point for ctl
  3225. *
  3226. */
  3227. void
  3228. mpt3sas_ctl_exit(ushort hbas_to_enumerate)
  3229. {
  3230. struct MPT3SAS_ADAPTER *ioc;
  3231. int i;
  3232. list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
  3233. /* free memory associated to diag buffers */
  3234. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  3235. if (!ioc->diag_buffer[i])
  3236. continue;
  3237. if (!(ioc->diag_buffer_status[i] &
  3238. MPT3_DIAG_BUFFER_IS_REGISTERED))
  3239. continue;
  3240. if ((ioc->diag_buffer_status[i] &
  3241. MPT3_DIAG_BUFFER_IS_RELEASED))
  3242. continue;
  3243. pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
  3244. ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
  3245. ioc->diag_buffer[i] = NULL;
  3246. ioc->diag_buffer_status[i] = 0;
  3247. }
  3248. kfree(ioc->event_log);
  3249. }
  3250. if (hbas_to_enumerate != 1)
  3251. misc_deregister(&ctl_dev);
  3252. if (hbas_to_enumerate != 2)
  3253. misc_deregister(&gen2_ctl_dev);
  3254. }