mpt2sas_ctl.c 85 KB

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