mpt2sas_ctl.c 76 KB

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