mpt2sas_ctl.c 86 KB

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