mpt2sas_ctl.c 82 KB

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