mpt2sas_ctl.c 85 KB

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