mpt2sas_ctl.c 70 KB

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