mpt2sas_ctl.c 86 KB

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