mpt2sas_ctl.c 76 KB

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