mpt3sas_ctl.c 91 KB

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