mpt2sas_ctl.c 72 KB

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