mpt2sas_ctl.c 76 KB

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