mpt2sas_ctl.c 84 KB

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