mpt2sas_ctl.c 76 KB

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