mpt3sas_ctl.c 91 KB

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