mpt3sas_transport.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. /*
  2. * SAS Transport Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt3sas/mpt3sas_transport.c
  5. * Copyright (C) 2012-2014 LSI Corporation
  6. * Copyright (C) 2013-2014 Avago Technologies
  7. * (mailto: MPT-FusionLinux.pdl@avagotech.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/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/sched.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/delay.h>
  49. #include <linux/pci.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_cmnd.h>
  52. #include <scsi/scsi_device.h>
  53. #include <scsi/scsi_host.h>
  54. #include <scsi/scsi_transport_sas.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include "mpt3sas_base.h"
  57. /**
  58. * _transport_sas_node_find_by_sas_address - sas node search
  59. * @ioc: per adapter object
  60. * @sas_address: sas address of expander or sas host
  61. * Context: Calling function should acquire ioc->sas_node_lock.
  62. *
  63. * Search for either hba phys or expander device based on handle, then returns
  64. * the sas_node object.
  65. */
  66. static struct _sas_node *
  67. _transport_sas_node_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
  68. u64 sas_address)
  69. {
  70. if (ioc->sas_hba.sas_address == sas_address)
  71. return &ioc->sas_hba;
  72. else
  73. return mpt3sas_scsih_expander_find_by_sas_address(ioc,
  74. sas_address);
  75. }
  76. /**
  77. * _transport_convert_phy_link_rate -
  78. * @link_rate: link rate returned from mpt firmware
  79. *
  80. * Convert link_rate from mpi fusion into sas_transport form.
  81. */
  82. static enum sas_linkrate
  83. _transport_convert_phy_link_rate(u8 link_rate)
  84. {
  85. enum sas_linkrate rc;
  86. switch (link_rate) {
  87. case MPI2_SAS_NEG_LINK_RATE_1_5:
  88. rc = SAS_LINK_RATE_1_5_GBPS;
  89. break;
  90. case MPI2_SAS_NEG_LINK_RATE_3_0:
  91. rc = SAS_LINK_RATE_3_0_GBPS;
  92. break;
  93. case MPI2_SAS_NEG_LINK_RATE_6_0:
  94. rc = SAS_LINK_RATE_6_0_GBPS;
  95. break;
  96. case MPI25_SAS_NEG_LINK_RATE_12_0:
  97. rc = SAS_LINK_RATE_12_0_GBPS;
  98. break;
  99. case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
  100. rc = SAS_PHY_DISABLED;
  101. break;
  102. case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
  103. rc = SAS_LINK_RATE_FAILED;
  104. break;
  105. case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
  106. rc = SAS_SATA_PORT_SELECTOR;
  107. break;
  108. case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
  109. rc = SAS_PHY_RESET_IN_PROGRESS;
  110. break;
  111. default:
  112. case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
  113. case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
  114. rc = SAS_LINK_RATE_UNKNOWN;
  115. break;
  116. }
  117. return rc;
  118. }
  119. /**
  120. * _transport_set_identify - set identify for phys and end devices
  121. * @ioc: per adapter object
  122. * @handle: device handle
  123. * @identify: sas identify info
  124. *
  125. * Populates sas identify info.
  126. *
  127. * Returns 0 for success, non-zero for failure.
  128. */
  129. static int
  130. _transport_set_identify(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  131. struct sas_identify *identify)
  132. {
  133. Mpi2SasDevicePage0_t sas_device_pg0;
  134. Mpi2ConfigReply_t mpi_reply;
  135. u32 device_info;
  136. u32 ioc_status;
  137. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  138. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  139. __func__, ioc->name);
  140. return -EFAULT;
  141. }
  142. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  143. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  144. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  145. ioc->name, __FILE__, __LINE__, __func__);
  146. return -ENXIO;
  147. }
  148. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  149. MPI2_IOCSTATUS_MASK;
  150. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  151. pr_err(MPT3SAS_FMT
  152. "handle(0x%04x), ioc_status(0x%04x)\nfailure at %s:%d/%s()!\n",
  153. ioc->name, handle, ioc_status,
  154. __FILE__, __LINE__, __func__);
  155. return -EIO;
  156. }
  157. memset(identify, 0, sizeof(struct sas_identify));
  158. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  159. /* sas_address */
  160. identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  161. /* phy number of the parent device this device is linked to */
  162. identify->phy_identifier = sas_device_pg0.PhyNum;
  163. /* device_type */
  164. switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  165. case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
  166. identify->device_type = SAS_PHY_UNUSED;
  167. break;
  168. case MPI2_SAS_DEVICE_INFO_END_DEVICE:
  169. identify->device_type = SAS_END_DEVICE;
  170. break;
  171. case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
  172. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  173. break;
  174. case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  175. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  176. break;
  177. }
  178. /* initiator_port_protocols */
  179. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
  180. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  181. if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
  182. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  183. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
  184. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  185. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
  186. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  187. /* target_port_protocols */
  188. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
  189. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  190. if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
  191. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  192. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
  193. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  194. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  195. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  196. return 0;
  197. }
  198. /**
  199. * mpt3sas_transport_done - internal transport layer callback handler.
  200. * @ioc: per adapter object
  201. * @smid: system request message index
  202. * @msix_index: MSIX table index supplied by the OS
  203. * @reply: reply message frame(lower 32bit addr)
  204. *
  205. * Callback handler when sending internal generated transport cmds.
  206. * The callback index passed is `ioc->transport_cb_idx`
  207. *
  208. * Return 1 meaning mf should be freed from _base_interrupt
  209. * 0 means the mf is freed from this function.
  210. */
  211. u8
  212. mpt3sas_transport_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  213. u32 reply)
  214. {
  215. MPI2DefaultReply_t *mpi_reply;
  216. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  217. if (ioc->transport_cmds.status == MPT3_CMD_NOT_USED)
  218. return 1;
  219. if (ioc->transport_cmds.smid != smid)
  220. return 1;
  221. ioc->transport_cmds.status |= MPT3_CMD_COMPLETE;
  222. if (mpi_reply) {
  223. memcpy(ioc->transport_cmds.reply, mpi_reply,
  224. mpi_reply->MsgLength*4);
  225. ioc->transport_cmds.status |= MPT3_CMD_REPLY_VALID;
  226. }
  227. ioc->transport_cmds.status &= ~MPT3_CMD_PENDING;
  228. complete(&ioc->transport_cmds.done);
  229. return 1;
  230. }
  231. /* report manufacture request structure */
  232. struct rep_manu_request {
  233. u8 smp_frame_type;
  234. u8 function;
  235. u8 reserved;
  236. u8 request_length;
  237. };
  238. /* report manufacture reply structure */
  239. struct rep_manu_reply {
  240. u8 smp_frame_type; /* 0x41 */
  241. u8 function; /* 0x01 */
  242. u8 function_result;
  243. u8 response_length;
  244. u16 expander_change_count;
  245. u8 reserved0[2];
  246. u8 sas_format;
  247. u8 reserved2[3];
  248. u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
  249. u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
  250. u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
  251. u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
  252. u16 component_id;
  253. u8 component_revision_id;
  254. u8 reserved3;
  255. u8 vendor_specific[8];
  256. };
  257. /**
  258. * transport_expander_report_manufacture - obtain SMP report_manufacture
  259. * @ioc: per adapter object
  260. * @sas_address: expander sas address
  261. * @edev: the sas_expander_device object
  262. *
  263. * Fills in the sas_expander_device object when SMP port is created.
  264. *
  265. * Returns 0 for success, non-zero for failure.
  266. */
  267. static int
  268. _transport_expander_report_manufacture(struct MPT3SAS_ADAPTER *ioc,
  269. u64 sas_address, struct sas_expander_device *edev)
  270. {
  271. Mpi2SmpPassthroughRequest_t *mpi_request;
  272. Mpi2SmpPassthroughReply_t *mpi_reply;
  273. struct rep_manu_reply *manufacture_reply;
  274. struct rep_manu_request *manufacture_request;
  275. int rc;
  276. u16 smid;
  277. u32 ioc_state;
  278. unsigned long timeleft;
  279. void *psge;
  280. u8 issue_reset = 0;
  281. void *data_out = NULL;
  282. dma_addr_t data_out_dma;
  283. dma_addr_t data_in_dma;
  284. size_t data_in_sz;
  285. size_t data_out_sz;
  286. u16 wait_state_count;
  287. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  288. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  289. __func__, ioc->name);
  290. return -EFAULT;
  291. }
  292. mutex_lock(&ioc->transport_cmds.mutex);
  293. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  294. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  295. ioc->name, __func__);
  296. rc = -EAGAIN;
  297. goto out;
  298. }
  299. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  300. wait_state_count = 0;
  301. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  302. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  303. if (wait_state_count++ == 10) {
  304. pr_err(MPT3SAS_FMT
  305. "%s: failed due to ioc not operational\n",
  306. ioc->name, __func__);
  307. rc = -EFAULT;
  308. goto out;
  309. }
  310. ssleep(1);
  311. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  312. pr_info(MPT3SAS_FMT
  313. "%s: waiting for operational state(count=%d)\n",
  314. ioc->name, __func__, wait_state_count);
  315. }
  316. if (wait_state_count)
  317. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  318. ioc->name, __func__);
  319. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  320. if (!smid) {
  321. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  322. ioc->name, __func__);
  323. rc = -EAGAIN;
  324. goto out;
  325. }
  326. rc = 0;
  327. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  328. ioc->transport_cmds.smid = smid;
  329. data_out_sz = sizeof(struct rep_manu_request);
  330. data_in_sz = sizeof(struct rep_manu_reply);
  331. data_out = pci_alloc_consistent(ioc->pdev, data_out_sz + data_in_sz,
  332. &data_out_dma);
  333. if (!data_out) {
  334. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  335. __LINE__, __func__);
  336. rc = -ENOMEM;
  337. mpt3sas_base_free_smid(ioc, smid);
  338. goto out;
  339. }
  340. data_in_dma = data_out_dma + sizeof(struct rep_manu_request);
  341. manufacture_request = data_out;
  342. manufacture_request->smp_frame_type = 0x40;
  343. manufacture_request->function = 1;
  344. manufacture_request->reserved = 0;
  345. manufacture_request->request_length = 0;
  346. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  347. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  348. mpi_request->PhysicalPort = 0xFF;
  349. mpi_request->SASAddress = cpu_to_le64(sas_address);
  350. mpi_request->RequestDataLength = cpu_to_le16(data_out_sz);
  351. psge = &mpi_request->SGL;
  352. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
  353. data_in_sz);
  354. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  355. "report_manufacture - send to sas_addr(0x%016llx)\n",
  356. ioc->name, (unsigned long long)sas_address));
  357. init_completion(&ioc->transport_cmds.done);
  358. mpt3sas_base_put_smid_default(ioc, smid);
  359. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  360. 10*HZ);
  361. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  362. pr_err(MPT3SAS_FMT "%s: timeout\n",
  363. ioc->name, __func__);
  364. _debug_dump_mf(mpi_request,
  365. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  366. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  367. issue_reset = 1;
  368. goto issue_host_reset;
  369. }
  370. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  371. "report_manufacture - complete\n", ioc->name));
  372. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  373. u8 *tmp;
  374. mpi_reply = ioc->transport_cmds.reply;
  375. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  376. "report_manufacture - reply data transfer size(%d)\n",
  377. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  378. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  379. sizeof(struct rep_manu_reply))
  380. goto out;
  381. manufacture_reply = data_out + sizeof(struct rep_manu_request);
  382. strncpy(edev->vendor_id, manufacture_reply->vendor_id,
  383. SAS_EXPANDER_VENDOR_ID_LEN);
  384. strncpy(edev->product_id, manufacture_reply->product_id,
  385. SAS_EXPANDER_PRODUCT_ID_LEN);
  386. strncpy(edev->product_rev, manufacture_reply->product_rev,
  387. SAS_EXPANDER_PRODUCT_REV_LEN);
  388. edev->level = manufacture_reply->sas_format & 1;
  389. if (edev->level) {
  390. strncpy(edev->component_vendor_id,
  391. manufacture_reply->component_vendor_id,
  392. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  393. tmp = (u8 *)&manufacture_reply->component_id;
  394. edev->component_id = tmp[0] << 8 | tmp[1];
  395. edev->component_revision_id =
  396. manufacture_reply->component_revision_id;
  397. }
  398. } else
  399. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  400. "report_manufacture - no reply\n", ioc->name));
  401. issue_host_reset:
  402. if (issue_reset)
  403. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  404. FORCE_BIG_HAMMER);
  405. out:
  406. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  407. if (data_out)
  408. pci_free_consistent(ioc->pdev, data_out_sz + data_in_sz,
  409. data_out, data_out_dma);
  410. mutex_unlock(&ioc->transport_cmds.mutex);
  411. return rc;
  412. }
  413. /**
  414. * _transport_delete_port - helper function to removing a port
  415. * @ioc: per adapter object
  416. * @mpt3sas_port: mpt3sas per port object
  417. *
  418. * Returns nothing.
  419. */
  420. static void
  421. _transport_delete_port(struct MPT3SAS_ADAPTER *ioc,
  422. struct _sas_port *mpt3sas_port)
  423. {
  424. u64 sas_address = mpt3sas_port->remote_identify.sas_address;
  425. enum sas_device_type device_type =
  426. mpt3sas_port->remote_identify.device_type;
  427. dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
  428. "remove: sas_addr(0x%016llx)\n",
  429. (unsigned long long) sas_address);
  430. ioc->logging_level |= MPT_DEBUG_TRANSPORT;
  431. if (device_type == SAS_END_DEVICE)
  432. mpt3sas_device_remove_by_sas_address(ioc, sas_address);
  433. else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
  434. device_type == SAS_FANOUT_EXPANDER_DEVICE)
  435. mpt3sas_expander_remove(ioc, sas_address);
  436. ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
  437. }
  438. /**
  439. * _transport_delete_phy - helper function to removing single phy from port
  440. * @ioc: per adapter object
  441. * @mpt3sas_port: mpt3sas per port object
  442. * @mpt3sas_phy: mpt3sas per phy object
  443. *
  444. * Returns nothing.
  445. */
  446. static void
  447. _transport_delete_phy(struct MPT3SAS_ADAPTER *ioc,
  448. struct _sas_port *mpt3sas_port, struct _sas_phy *mpt3sas_phy)
  449. {
  450. u64 sas_address = mpt3sas_port->remote_identify.sas_address;
  451. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  452. "remove: sas_addr(0x%016llx), phy(%d)\n",
  453. (unsigned long long) sas_address, mpt3sas_phy->phy_id);
  454. list_del(&mpt3sas_phy->port_siblings);
  455. mpt3sas_port->num_phys--;
  456. sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
  457. mpt3sas_phy->phy_belongs_to_port = 0;
  458. }
  459. /**
  460. * _transport_add_phy - helper function to adding single phy to port
  461. * @ioc: per adapter object
  462. * @mpt3sas_port: mpt3sas per port object
  463. * @mpt3sas_phy: mpt3sas per phy object
  464. *
  465. * Returns nothing.
  466. */
  467. static void
  468. _transport_add_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_port *mpt3sas_port,
  469. struct _sas_phy *mpt3sas_phy)
  470. {
  471. u64 sas_address = mpt3sas_port->remote_identify.sas_address;
  472. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  473. "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
  474. sas_address, mpt3sas_phy->phy_id);
  475. list_add_tail(&mpt3sas_phy->port_siblings, &mpt3sas_port->phy_list);
  476. mpt3sas_port->num_phys++;
  477. sas_port_add_phy(mpt3sas_port->port, mpt3sas_phy->phy);
  478. mpt3sas_phy->phy_belongs_to_port = 1;
  479. }
  480. /**
  481. * _transport_add_phy_to_an_existing_port - adding new phy to existing port
  482. * @ioc: per adapter object
  483. * @sas_node: sas node object (either expander or sas host)
  484. * @mpt3sas_phy: mpt3sas per phy object
  485. * @sas_address: sas address of device/expander were phy needs to be added to
  486. *
  487. * Returns nothing.
  488. */
  489. static void
  490. _transport_add_phy_to_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
  491. struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy,
  492. u64 sas_address)
  493. {
  494. struct _sas_port *mpt3sas_port;
  495. struct _sas_phy *phy_srch;
  496. if (mpt3sas_phy->phy_belongs_to_port == 1)
  497. return;
  498. list_for_each_entry(mpt3sas_port, &sas_node->sas_port_list,
  499. port_list) {
  500. if (mpt3sas_port->remote_identify.sas_address !=
  501. sas_address)
  502. continue;
  503. list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
  504. port_siblings) {
  505. if (phy_srch == mpt3sas_phy)
  506. return;
  507. }
  508. _transport_add_phy(ioc, mpt3sas_port, mpt3sas_phy);
  509. return;
  510. }
  511. }
  512. /**
  513. * _transport_del_phy_from_an_existing_port - delete phy from existing port
  514. * @ioc: per adapter object
  515. * @sas_node: sas node object (either expander or sas host)
  516. * @mpt3sas_phy: mpt3sas per phy object
  517. *
  518. * Returns nothing.
  519. */
  520. static void
  521. _transport_del_phy_from_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
  522. struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy)
  523. {
  524. struct _sas_port *mpt3sas_port, *next;
  525. struct _sas_phy *phy_srch;
  526. if (mpt3sas_phy->phy_belongs_to_port == 0)
  527. return;
  528. list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
  529. port_list) {
  530. list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
  531. port_siblings) {
  532. if (phy_srch != mpt3sas_phy)
  533. continue;
  534. if (mpt3sas_port->num_phys == 1)
  535. _transport_delete_port(ioc, mpt3sas_port);
  536. else
  537. _transport_delete_phy(ioc, mpt3sas_port,
  538. mpt3sas_phy);
  539. return;
  540. }
  541. }
  542. }
  543. /**
  544. * _transport_sanity_check - sanity check when adding a new port
  545. * @ioc: per adapter object
  546. * @sas_node: sas node object (either expander or sas host)
  547. * @sas_address: sas address of device being added
  548. *
  549. * See the explanation above from _transport_delete_duplicate_port
  550. */
  551. static void
  552. _transport_sanity_check(struct MPT3SAS_ADAPTER *ioc, struct _sas_node *sas_node,
  553. u64 sas_address)
  554. {
  555. int i;
  556. for (i = 0; i < sas_node->num_phys; i++) {
  557. if (sas_node->phy[i].remote_identify.sas_address != sas_address)
  558. continue;
  559. if (sas_node->phy[i].phy_belongs_to_port == 1)
  560. _transport_del_phy_from_an_existing_port(ioc, sas_node,
  561. &sas_node->phy[i]);
  562. }
  563. }
  564. /**
  565. * mpt3sas_transport_port_add - insert port to the list
  566. * @ioc: per adapter object
  567. * @handle: handle of attached device
  568. * @sas_address: sas address of parent expander or sas host
  569. * Context: This function will acquire ioc->sas_node_lock.
  570. *
  571. * Adding new port object to the sas_node->sas_port_list.
  572. *
  573. * Returns mpt3sas_port.
  574. */
  575. struct _sas_port *
  576. mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  577. u64 sas_address)
  578. {
  579. struct _sas_phy *mpt3sas_phy, *next;
  580. struct _sas_port *mpt3sas_port;
  581. unsigned long flags;
  582. struct _sas_node *sas_node;
  583. struct sas_rphy *rphy;
  584. int i;
  585. struct sas_port *port;
  586. mpt3sas_port = kzalloc(sizeof(struct _sas_port),
  587. GFP_KERNEL);
  588. if (!mpt3sas_port) {
  589. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  590. ioc->name, __FILE__, __LINE__, __func__);
  591. return NULL;
  592. }
  593. INIT_LIST_HEAD(&mpt3sas_port->port_list);
  594. INIT_LIST_HEAD(&mpt3sas_port->phy_list);
  595. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  596. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  597. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  598. if (!sas_node) {
  599. pr_err(MPT3SAS_FMT
  600. "%s: Could not find parent sas_address(0x%016llx)!\n",
  601. ioc->name, __func__, (unsigned long long)sas_address);
  602. goto out_fail;
  603. }
  604. if ((_transport_set_identify(ioc, handle,
  605. &mpt3sas_port->remote_identify))) {
  606. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  607. ioc->name, __FILE__, __LINE__, __func__);
  608. goto out_fail;
  609. }
  610. if (mpt3sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
  611. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  612. ioc->name, __FILE__, __LINE__, __func__);
  613. goto out_fail;
  614. }
  615. _transport_sanity_check(ioc, sas_node,
  616. mpt3sas_port->remote_identify.sas_address);
  617. for (i = 0; i < sas_node->num_phys; i++) {
  618. if (sas_node->phy[i].remote_identify.sas_address !=
  619. mpt3sas_port->remote_identify.sas_address)
  620. continue;
  621. list_add_tail(&sas_node->phy[i].port_siblings,
  622. &mpt3sas_port->phy_list);
  623. mpt3sas_port->num_phys++;
  624. }
  625. if (!mpt3sas_port->num_phys) {
  626. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  627. ioc->name, __FILE__, __LINE__, __func__);
  628. goto out_fail;
  629. }
  630. port = sas_port_alloc_num(sas_node->parent_dev);
  631. if ((sas_port_add(port))) {
  632. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  633. ioc->name, __FILE__, __LINE__, __func__);
  634. goto out_fail;
  635. }
  636. list_for_each_entry(mpt3sas_phy, &mpt3sas_port->phy_list,
  637. port_siblings) {
  638. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  639. dev_printk(KERN_INFO, &port->dev,
  640. "add: handle(0x%04x), sas_addr(0x%016llx), phy(%d)\n",
  641. handle, (unsigned long long)
  642. mpt3sas_port->remote_identify.sas_address,
  643. mpt3sas_phy->phy_id);
  644. sas_port_add_phy(port, mpt3sas_phy->phy);
  645. mpt3sas_phy->phy_belongs_to_port = 1;
  646. }
  647. mpt3sas_port->port = port;
  648. if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE)
  649. rphy = sas_end_device_alloc(port);
  650. else
  651. rphy = sas_expander_alloc(port,
  652. mpt3sas_port->remote_identify.device_type);
  653. rphy->identify = mpt3sas_port->remote_identify;
  654. if ((sas_rphy_add(rphy))) {
  655. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  656. ioc->name, __FILE__, __LINE__, __func__);
  657. }
  658. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  659. dev_printk(KERN_INFO, &rphy->dev,
  660. "add: handle(0x%04x), sas_addr(0x%016llx)\n",
  661. handle, (unsigned long long)
  662. mpt3sas_port->remote_identify.sas_address);
  663. mpt3sas_port->rphy = rphy;
  664. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  665. list_add_tail(&mpt3sas_port->port_list, &sas_node->sas_port_list);
  666. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  667. /* fill in report manufacture */
  668. if (mpt3sas_port->remote_identify.device_type ==
  669. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  670. mpt3sas_port->remote_identify.device_type ==
  671. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
  672. _transport_expander_report_manufacture(ioc,
  673. mpt3sas_port->remote_identify.sas_address,
  674. rphy_to_expander_device(rphy));
  675. return mpt3sas_port;
  676. out_fail:
  677. list_for_each_entry_safe(mpt3sas_phy, next, &mpt3sas_port->phy_list,
  678. port_siblings)
  679. list_del(&mpt3sas_phy->port_siblings);
  680. kfree(mpt3sas_port);
  681. return NULL;
  682. }
  683. /**
  684. * mpt3sas_transport_port_remove - remove port from the list
  685. * @ioc: per adapter object
  686. * @sas_address: sas address of attached device
  687. * @sas_address_parent: sas address of parent expander or sas host
  688. * Context: This function will acquire ioc->sas_node_lock.
  689. *
  690. * Removing object and freeing associated memory from the
  691. * ioc->sas_port_list.
  692. *
  693. * Return nothing.
  694. */
  695. void
  696. mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
  697. u64 sas_address_parent)
  698. {
  699. int i;
  700. unsigned long flags;
  701. struct _sas_port *mpt3sas_port, *next;
  702. struct _sas_node *sas_node;
  703. u8 found = 0;
  704. struct _sas_phy *mpt3sas_phy, *next_phy;
  705. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  706. sas_node = _transport_sas_node_find_by_sas_address(ioc,
  707. sas_address_parent);
  708. if (!sas_node) {
  709. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  710. return;
  711. }
  712. list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
  713. port_list) {
  714. if (mpt3sas_port->remote_identify.sas_address != sas_address)
  715. continue;
  716. found = 1;
  717. list_del(&mpt3sas_port->port_list);
  718. goto out;
  719. }
  720. out:
  721. if (!found) {
  722. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  723. return;
  724. }
  725. for (i = 0; i < sas_node->num_phys; i++) {
  726. if (sas_node->phy[i].remote_identify.sas_address == sas_address)
  727. memset(&sas_node->phy[i].remote_identify, 0 ,
  728. sizeof(struct sas_identify));
  729. }
  730. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  731. list_for_each_entry_safe(mpt3sas_phy, next_phy,
  732. &mpt3sas_port->phy_list, port_siblings) {
  733. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  734. dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
  735. "remove: sas_addr(0x%016llx), phy(%d)\n",
  736. (unsigned long long)
  737. mpt3sas_port->remote_identify.sas_address,
  738. mpt3sas_phy->phy_id);
  739. mpt3sas_phy->phy_belongs_to_port = 0;
  740. sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
  741. list_del(&mpt3sas_phy->port_siblings);
  742. }
  743. sas_port_delete(mpt3sas_port->port);
  744. kfree(mpt3sas_port);
  745. }
  746. /**
  747. * mpt3sas_transport_add_host_phy - report sas_host phy to transport
  748. * @ioc: per adapter object
  749. * @mpt3sas_phy: mpt3sas per phy object
  750. * @phy_pg0: sas phy page 0
  751. * @parent_dev: parent device class object
  752. *
  753. * Returns 0 for success, non-zero for failure.
  754. */
  755. int
  756. mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
  757. *mpt3sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
  758. {
  759. struct sas_phy *phy;
  760. int phy_index = mpt3sas_phy->phy_id;
  761. INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
  762. phy = sas_phy_alloc(parent_dev, phy_index);
  763. if (!phy) {
  764. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  765. ioc->name, __FILE__, __LINE__, __func__);
  766. return -1;
  767. }
  768. if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
  769. &mpt3sas_phy->identify))) {
  770. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  771. ioc->name, __FILE__, __LINE__, __func__);
  772. sas_phy_free(phy);
  773. return -1;
  774. }
  775. phy->identify = mpt3sas_phy->identify;
  776. mpt3sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
  777. if (mpt3sas_phy->attached_handle)
  778. _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
  779. &mpt3sas_phy->remote_identify);
  780. phy->identify.phy_identifier = mpt3sas_phy->phy_id;
  781. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  782. phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  783. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  784. phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  785. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  786. phy_pg0.HwLinkRate >> 4);
  787. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  788. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  789. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  790. phy_pg0.ProgrammedLinkRate >> 4);
  791. if ((sas_phy_add(phy))) {
  792. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  793. ioc->name, __FILE__, __LINE__, __func__);
  794. sas_phy_free(phy);
  795. return -1;
  796. }
  797. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  798. dev_printk(KERN_INFO, &phy->dev,
  799. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  800. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  801. mpt3sas_phy->handle, (unsigned long long)
  802. mpt3sas_phy->identify.sas_address,
  803. mpt3sas_phy->attached_handle,
  804. (unsigned long long)
  805. mpt3sas_phy->remote_identify.sas_address);
  806. mpt3sas_phy->phy = phy;
  807. return 0;
  808. }
  809. /**
  810. * mpt3sas_transport_add_expander_phy - report expander phy to transport
  811. * @ioc: per adapter object
  812. * @mpt3sas_phy: mpt3sas per phy object
  813. * @expander_pg1: expander page 1
  814. * @parent_dev: parent device class object
  815. *
  816. * Returns 0 for success, non-zero for failure.
  817. */
  818. int
  819. mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
  820. *mpt3sas_phy, Mpi2ExpanderPage1_t expander_pg1,
  821. struct device *parent_dev)
  822. {
  823. struct sas_phy *phy;
  824. int phy_index = mpt3sas_phy->phy_id;
  825. INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
  826. phy = sas_phy_alloc(parent_dev, phy_index);
  827. if (!phy) {
  828. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  829. ioc->name, __FILE__, __LINE__, __func__);
  830. return -1;
  831. }
  832. if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
  833. &mpt3sas_phy->identify))) {
  834. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  835. ioc->name, __FILE__, __LINE__, __func__);
  836. sas_phy_free(phy);
  837. return -1;
  838. }
  839. phy->identify = mpt3sas_phy->identify;
  840. mpt3sas_phy->attached_handle =
  841. le16_to_cpu(expander_pg1.AttachedDevHandle);
  842. if (mpt3sas_phy->attached_handle)
  843. _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
  844. &mpt3sas_phy->remote_identify);
  845. phy->identify.phy_identifier = mpt3sas_phy->phy_id;
  846. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  847. expander_pg1.NegotiatedLinkRate &
  848. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  849. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  850. expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  851. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  852. expander_pg1.HwLinkRate >> 4);
  853. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  854. expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  855. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  856. expander_pg1.ProgrammedLinkRate >> 4);
  857. if ((sas_phy_add(phy))) {
  858. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  859. ioc->name, __FILE__, __LINE__, __func__);
  860. sas_phy_free(phy);
  861. return -1;
  862. }
  863. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  864. dev_printk(KERN_INFO, &phy->dev,
  865. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  866. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  867. mpt3sas_phy->handle, (unsigned long long)
  868. mpt3sas_phy->identify.sas_address,
  869. mpt3sas_phy->attached_handle,
  870. (unsigned long long)
  871. mpt3sas_phy->remote_identify.sas_address);
  872. mpt3sas_phy->phy = phy;
  873. return 0;
  874. }
  875. /**
  876. * mpt3sas_transport_update_links - refreshing phy link changes
  877. * @ioc: per adapter object
  878. * @sas_address: sas address of parent expander or sas host
  879. * @handle: attached device handle
  880. * @phy_numberv: phy number
  881. * @link_rate: new link rate
  882. *
  883. * Returns nothing.
  884. */
  885. void
  886. mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER *ioc,
  887. u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
  888. {
  889. unsigned long flags;
  890. struct _sas_node *sas_node;
  891. struct _sas_phy *mpt3sas_phy;
  892. if (ioc->shost_recovery || ioc->pci_error_recovery)
  893. return;
  894. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  895. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  896. if (!sas_node) {
  897. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  898. return;
  899. }
  900. mpt3sas_phy = &sas_node->phy[phy_number];
  901. mpt3sas_phy->attached_handle = handle;
  902. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  903. if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
  904. _transport_set_identify(ioc, handle,
  905. &mpt3sas_phy->remote_identify);
  906. _transport_add_phy_to_an_existing_port(ioc, sas_node,
  907. mpt3sas_phy, mpt3sas_phy->remote_identify.sas_address);
  908. } else
  909. memset(&mpt3sas_phy->remote_identify, 0 , sizeof(struct
  910. sas_identify));
  911. if (mpt3sas_phy->phy)
  912. mpt3sas_phy->phy->negotiated_linkrate =
  913. _transport_convert_phy_link_rate(link_rate);
  914. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  915. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  916. "refresh: parent sas_addr(0x%016llx),\n"
  917. "\tlink_rate(0x%02x), phy(%d)\n"
  918. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  919. (unsigned long long)sas_address,
  920. link_rate, phy_number, handle, (unsigned long long)
  921. mpt3sas_phy->remote_identify.sas_address);
  922. }
  923. static inline void *
  924. phy_to_ioc(struct sas_phy *phy)
  925. {
  926. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  927. return shost_priv(shost);
  928. }
  929. static inline void *
  930. rphy_to_ioc(struct sas_rphy *rphy)
  931. {
  932. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  933. return shost_priv(shost);
  934. }
  935. /* report phy error log structure */
  936. struct phy_error_log_request {
  937. u8 smp_frame_type; /* 0x40 */
  938. u8 function; /* 0x11 */
  939. u8 allocated_response_length;
  940. u8 request_length; /* 02 */
  941. u8 reserved_1[5];
  942. u8 phy_identifier;
  943. u8 reserved_2[2];
  944. };
  945. /* report phy error log reply structure */
  946. struct phy_error_log_reply {
  947. u8 smp_frame_type; /* 0x41 */
  948. u8 function; /* 0x11 */
  949. u8 function_result;
  950. u8 response_length;
  951. __be16 expander_change_count;
  952. u8 reserved_1[3];
  953. u8 phy_identifier;
  954. u8 reserved_2[2];
  955. __be32 invalid_dword;
  956. __be32 running_disparity_error;
  957. __be32 loss_of_dword_sync;
  958. __be32 phy_reset_problem;
  959. };
  960. /**
  961. * _transport_get_expander_phy_error_log - return expander counters
  962. * @ioc: per adapter object
  963. * @phy: The sas phy object
  964. *
  965. * Returns 0 for success, non-zero for failure.
  966. *
  967. */
  968. static int
  969. _transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER *ioc,
  970. struct sas_phy *phy)
  971. {
  972. Mpi2SmpPassthroughRequest_t *mpi_request;
  973. Mpi2SmpPassthroughReply_t *mpi_reply;
  974. struct phy_error_log_request *phy_error_log_request;
  975. struct phy_error_log_reply *phy_error_log_reply;
  976. int rc;
  977. u16 smid;
  978. u32 ioc_state;
  979. unsigned long timeleft;
  980. void *psge;
  981. u8 issue_reset = 0;
  982. void *data_out = NULL;
  983. dma_addr_t data_out_dma;
  984. u32 sz;
  985. u16 wait_state_count;
  986. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  987. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  988. __func__, ioc->name);
  989. return -EFAULT;
  990. }
  991. mutex_lock(&ioc->transport_cmds.mutex);
  992. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  993. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  994. ioc->name, __func__);
  995. rc = -EAGAIN;
  996. goto out;
  997. }
  998. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  999. wait_state_count = 0;
  1000. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1001. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1002. if (wait_state_count++ == 10) {
  1003. pr_err(MPT3SAS_FMT
  1004. "%s: failed due to ioc not operational\n",
  1005. ioc->name, __func__);
  1006. rc = -EFAULT;
  1007. goto out;
  1008. }
  1009. ssleep(1);
  1010. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1011. pr_info(MPT3SAS_FMT
  1012. "%s: waiting for operational state(count=%d)\n",
  1013. ioc->name, __func__, wait_state_count);
  1014. }
  1015. if (wait_state_count)
  1016. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1017. ioc->name, __func__);
  1018. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1019. if (!smid) {
  1020. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1021. ioc->name, __func__);
  1022. rc = -EAGAIN;
  1023. goto out;
  1024. }
  1025. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1026. ioc->transport_cmds.smid = smid;
  1027. sz = sizeof(struct phy_error_log_request) +
  1028. sizeof(struct phy_error_log_reply);
  1029. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1030. if (!data_out) {
  1031. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  1032. __LINE__, __func__);
  1033. rc = -ENOMEM;
  1034. mpt3sas_base_free_smid(ioc, smid);
  1035. goto out;
  1036. }
  1037. rc = -EINVAL;
  1038. memset(data_out, 0, sz);
  1039. phy_error_log_request = data_out;
  1040. phy_error_log_request->smp_frame_type = 0x40;
  1041. phy_error_log_request->function = 0x11;
  1042. phy_error_log_request->request_length = 2;
  1043. phy_error_log_request->allocated_response_length = 0;
  1044. phy_error_log_request->phy_identifier = phy->number;
  1045. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1046. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1047. mpi_request->PhysicalPort = 0xFF;
  1048. mpi_request->VF_ID = 0; /* TODO */
  1049. mpi_request->VP_ID = 0;
  1050. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1051. mpi_request->RequestDataLength =
  1052. cpu_to_le16(sizeof(struct phy_error_log_request));
  1053. psge = &mpi_request->SGL;
  1054. ioc->build_sg(ioc, psge, data_out_dma,
  1055. sizeof(struct phy_error_log_request),
  1056. data_out_dma + sizeof(struct phy_error_log_request),
  1057. sizeof(struct phy_error_log_reply));
  1058. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1059. "phy_error_log - send to sas_addr(0x%016llx), phy(%d)\n",
  1060. ioc->name, (unsigned long long)phy->identify.sas_address,
  1061. phy->number));
  1062. init_completion(&ioc->transport_cmds.done);
  1063. mpt3sas_base_put_smid_default(ioc, smid);
  1064. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1065. 10*HZ);
  1066. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1067. pr_err(MPT3SAS_FMT "%s: timeout\n",
  1068. ioc->name, __func__);
  1069. _debug_dump_mf(mpi_request,
  1070. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1071. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1072. issue_reset = 1;
  1073. goto issue_host_reset;
  1074. }
  1075. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1076. "phy_error_log - complete\n", ioc->name));
  1077. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1078. mpi_reply = ioc->transport_cmds.reply;
  1079. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1080. "phy_error_log - reply data transfer size(%d)\n",
  1081. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1082. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1083. sizeof(struct phy_error_log_reply))
  1084. goto out;
  1085. phy_error_log_reply = data_out +
  1086. sizeof(struct phy_error_log_request);
  1087. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1088. "phy_error_log - function_result(%d)\n",
  1089. ioc->name, phy_error_log_reply->function_result));
  1090. phy->invalid_dword_count =
  1091. be32_to_cpu(phy_error_log_reply->invalid_dword);
  1092. phy->running_disparity_error_count =
  1093. be32_to_cpu(phy_error_log_reply->running_disparity_error);
  1094. phy->loss_of_dword_sync_count =
  1095. be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
  1096. phy->phy_reset_problem_count =
  1097. be32_to_cpu(phy_error_log_reply->phy_reset_problem);
  1098. rc = 0;
  1099. } else
  1100. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1101. "phy_error_log - no reply\n", ioc->name));
  1102. issue_host_reset:
  1103. if (issue_reset)
  1104. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1105. FORCE_BIG_HAMMER);
  1106. out:
  1107. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1108. if (data_out)
  1109. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1110. mutex_unlock(&ioc->transport_cmds.mutex);
  1111. return rc;
  1112. }
  1113. /**
  1114. * _transport_get_linkerrors - return phy counters for both hba and expanders
  1115. * @phy: The sas phy object
  1116. *
  1117. * Returns 0 for success, non-zero for failure.
  1118. *
  1119. */
  1120. static int
  1121. _transport_get_linkerrors(struct sas_phy *phy)
  1122. {
  1123. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1124. unsigned long flags;
  1125. Mpi2ConfigReply_t mpi_reply;
  1126. Mpi2SasPhyPage1_t phy_pg1;
  1127. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1128. if (_transport_sas_node_find_by_sas_address(ioc,
  1129. phy->identify.sas_address) == NULL) {
  1130. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1131. return -EINVAL;
  1132. }
  1133. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1134. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1135. return _transport_get_expander_phy_error_log(ioc, phy);
  1136. /* get hba phy error logs */
  1137. if ((mpt3sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
  1138. phy->number))) {
  1139. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1140. ioc->name, __FILE__, __LINE__, __func__);
  1141. return -ENXIO;
  1142. }
  1143. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1144. pr_info(MPT3SAS_FMT
  1145. "phy(%d), ioc_status (0x%04x), loginfo(0x%08x)\n",
  1146. ioc->name, phy->number,
  1147. le16_to_cpu(mpi_reply.IOCStatus),
  1148. le32_to_cpu(mpi_reply.IOCLogInfo));
  1149. phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
  1150. phy->running_disparity_error_count =
  1151. le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
  1152. phy->loss_of_dword_sync_count =
  1153. le32_to_cpu(phy_pg1.LossDwordSynchCount);
  1154. phy->phy_reset_problem_count =
  1155. le32_to_cpu(phy_pg1.PhyResetProblemCount);
  1156. return 0;
  1157. }
  1158. /**
  1159. * _transport_get_enclosure_identifier -
  1160. * @phy: The sas phy object
  1161. *
  1162. * Obtain the enclosure logical id for an expander.
  1163. * Returns 0 for success, non-zero for failure.
  1164. */
  1165. static int
  1166. _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1167. {
  1168. struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1169. struct _sas_device *sas_device;
  1170. unsigned long flags;
  1171. int rc;
  1172. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1173. sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
  1174. rphy->identify.sas_address);
  1175. if (sas_device) {
  1176. *identifier = sas_device->enclosure_logical_id;
  1177. rc = 0;
  1178. } else {
  1179. *identifier = 0;
  1180. rc = -ENXIO;
  1181. }
  1182. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1183. return rc;
  1184. }
  1185. /**
  1186. * _transport_get_bay_identifier -
  1187. * @phy: The sas phy object
  1188. *
  1189. * Returns the slot id for a device that resides inside an enclosure.
  1190. */
  1191. static int
  1192. _transport_get_bay_identifier(struct sas_rphy *rphy)
  1193. {
  1194. struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1195. struct _sas_device *sas_device;
  1196. unsigned long flags;
  1197. int rc;
  1198. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1199. sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
  1200. rphy->identify.sas_address);
  1201. if (sas_device)
  1202. rc = sas_device->slot;
  1203. else
  1204. rc = -ENXIO;
  1205. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1206. return rc;
  1207. }
  1208. /* phy control request structure */
  1209. struct phy_control_request {
  1210. u8 smp_frame_type; /* 0x40 */
  1211. u8 function; /* 0x91 */
  1212. u8 allocated_response_length;
  1213. u8 request_length; /* 0x09 */
  1214. u16 expander_change_count;
  1215. u8 reserved_1[3];
  1216. u8 phy_identifier;
  1217. u8 phy_operation;
  1218. u8 reserved_2[13];
  1219. u64 attached_device_name;
  1220. u8 programmed_min_physical_link_rate;
  1221. u8 programmed_max_physical_link_rate;
  1222. u8 reserved_3[6];
  1223. };
  1224. /* phy control reply structure */
  1225. struct phy_control_reply {
  1226. u8 smp_frame_type; /* 0x41 */
  1227. u8 function; /* 0x11 */
  1228. u8 function_result;
  1229. u8 response_length;
  1230. };
  1231. #define SMP_PHY_CONTROL_LINK_RESET (0x01)
  1232. #define SMP_PHY_CONTROL_HARD_RESET (0x02)
  1233. #define SMP_PHY_CONTROL_DISABLE (0x03)
  1234. /**
  1235. * _transport_expander_phy_control - expander phy control
  1236. * @ioc: per adapter object
  1237. * @phy: The sas phy object
  1238. *
  1239. * Returns 0 for success, non-zero for failure.
  1240. *
  1241. */
  1242. static int
  1243. _transport_expander_phy_control(struct MPT3SAS_ADAPTER *ioc,
  1244. struct sas_phy *phy, u8 phy_operation)
  1245. {
  1246. Mpi2SmpPassthroughRequest_t *mpi_request;
  1247. Mpi2SmpPassthroughReply_t *mpi_reply;
  1248. struct phy_control_request *phy_control_request;
  1249. struct phy_control_reply *phy_control_reply;
  1250. int rc;
  1251. u16 smid;
  1252. u32 ioc_state;
  1253. unsigned long timeleft;
  1254. void *psge;
  1255. u32 sgl_flags;
  1256. u8 issue_reset = 0;
  1257. void *data_out = NULL;
  1258. dma_addr_t data_out_dma;
  1259. u32 sz;
  1260. u16 wait_state_count;
  1261. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1262. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1263. __func__, ioc->name);
  1264. return -EFAULT;
  1265. }
  1266. mutex_lock(&ioc->transport_cmds.mutex);
  1267. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  1268. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  1269. ioc->name, __func__);
  1270. rc = -EAGAIN;
  1271. goto out;
  1272. }
  1273. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  1274. wait_state_count = 0;
  1275. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1276. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1277. if (wait_state_count++ == 10) {
  1278. pr_err(MPT3SAS_FMT
  1279. "%s: failed due to ioc not operational\n",
  1280. ioc->name, __func__);
  1281. rc = -EFAULT;
  1282. goto out;
  1283. }
  1284. ssleep(1);
  1285. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1286. pr_info(MPT3SAS_FMT
  1287. "%s: waiting for operational state(count=%d)\n",
  1288. ioc->name, __func__, wait_state_count);
  1289. }
  1290. if (wait_state_count)
  1291. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1292. ioc->name, __func__);
  1293. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1294. if (!smid) {
  1295. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1296. ioc->name, __func__);
  1297. rc = -EAGAIN;
  1298. goto out;
  1299. }
  1300. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1301. ioc->transport_cmds.smid = smid;
  1302. sz = sizeof(struct phy_control_request) +
  1303. sizeof(struct phy_control_reply);
  1304. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1305. if (!data_out) {
  1306. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  1307. __LINE__, __func__);
  1308. rc = -ENOMEM;
  1309. mpt3sas_base_free_smid(ioc, smid);
  1310. goto out;
  1311. }
  1312. rc = -EINVAL;
  1313. memset(data_out, 0, sz);
  1314. phy_control_request = data_out;
  1315. phy_control_request->smp_frame_type = 0x40;
  1316. phy_control_request->function = 0x91;
  1317. phy_control_request->request_length = 9;
  1318. phy_control_request->allocated_response_length = 0;
  1319. phy_control_request->phy_identifier = phy->number;
  1320. phy_control_request->phy_operation = phy_operation;
  1321. phy_control_request->programmed_min_physical_link_rate =
  1322. phy->minimum_linkrate << 4;
  1323. phy_control_request->programmed_max_physical_link_rate =
  1324. phy->maximum_linkrate << 4;
  1325. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1326. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1327. mpi_request->PhysicalPort = 0xFF;
  1328. mpi_request->VF_ID = 0; /* TODO */
  1329. mpi_request->VP_ID = 0;
  1330. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1331. mpi_request->RequestDataLength =
  1332. cpu_to_le16(sizeof(struct phy_error_log_request));
  1333. psge = &mpi_request->SGL;
  1334. /* WRITE sgel first */
  1335. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1336. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1337. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1338. ioc->base_add_sg_single(psge, sgl_flags |
  1339. sizeof(struct phy_control_request), data_out_dma);
  1340. /* incr sgel */
  1341. psge += ioc->sge_size;
  1342. /* READ sgel last */
  1343. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1344. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1345. MPI2_SGE_FLAGS_END_OF_LIST);
  1346. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1347. ioc->base_add_sg_single(psge, sgl_flags |
  1348. sizeof(struct phy_control_reply), data_out_dma +
  1349. sizeof(struct phy_control_request));
  1350. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1351. "phy_control - send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n",
  1352. ioc->name, (unsigned long long)phy->identify.sas_address,
  1353. phy->number, phy_operation));
  1354. init_completion(&ioc->transport_cmds.done);
  1355. mpt3sas_base_put_smid_default(ioc, smid);
  1356. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1357. 10*HZ);
  1358. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1359. pr_err(MPT3SAS_FMT "%s: timeout\n",
  1360. ioc->name, __func__);
  1361. _debug_dump_mf(mpi_request,
  1362. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1363. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1364. issue_reset = 1;
  1365. goto issue_host_reset;
  1366. }
  1367. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1368. "phy_control - complete\n", ioc->name));
  1369. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1370. mpi_reply = ioc->transport_cmds.reply;
  1371. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1372. "phy_control - reply data transfer size(%d)\n",
  1373. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1374. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1375. sizeof(struct phy_control_reply))
  1376. goto out;
  1377. phy_control_reply = data_out +
  1378. sizeof(struct phy_control_request);
  1379. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1380. "phy_control - function_result(%d)\n",
  1381. ioc->name, phy_control_reply->function_result));
  1382. rc = 0;
  1383. } else
  1384. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1385. "phy_control - no reply\n", ioc->name));
  1386. issue_host_reset:
  1387. if (issue_reset)
  1388. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1389. FORCE_BIG_HAMMER);
  1390. out:
  1391. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1392. if (data_out)
  1393. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1394. mutex_unlock(&ioc->transport_cmds.mutex);
  1395. return rc;
  1396. }
  1397. /**
  1398. * _transport_phy_reset -
  1399. * @phy: The sas phy object
  1400. * @hard_reset:
  1401. *
  1402. * Returns 0 for success, non-zero for failure.
  1403. */
  1404. static int
  1405. _transport_phy_reset(struct sas_phy *phy, int hard_reset)
  1406. {
  1407. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1408. Mpi2SasIoUnitControlReply_t mpi_reply;
  1409. Mpi2SasIoUnitControlRequest_t mpi_request;
  1410. unsigned long flags;
  1411. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1412. if (_transport_sas_node_find_by_sas_address(ioc,
  1413. phy->identify.sas_address) == NULL) {
  1414. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1415. return -EINVAL;
  1416. }
  1417. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1418. /* handle expander phys */
  1419. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1420. return _transport_expander_phy_control(ioc, phy,
  1421. (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
  1422. SMP_PHY_CONTROL_LINK_RESET);
  1423. /* handle hba phys */
  1424. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  1425. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  1426. mpi_request.Operation = hard_reset ?
  1427. MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
  1428. mpi_request.PhyNum = phy->number;
  1429. if ((mpt3sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
  1430. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1431. ioc->name, __FILE__, __LINE__, __func__);
  1432. return -ENXIO;
  1433. }
  1434. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1435. pr_info(MPT3SAS_FMT
  1436. "phy(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
  1437. ioc->name, phy->number, le16_to_cpu(mpi_reply.IOCStatus),
  1438. le32_to_cpu(mpi_reply.IOCLogInfo));
  1439. return 0;
  1440. }
  1441. /**
  1442. * _transport_phy_enable - enable/disable phys
  1443. * @phy: The sas phy object
  1444. * @enable: enable phy when true
  1445. *
  1446. * Only support sas_host direct attached phys.
  1447. * Returns 0 for success, non-zero for failure.
  1448. */
  1449. static int
  1450. _transport_phy_enable(struct sas_phy *phy, int enable)
  1451. {
  1452. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1453. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1454. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  1455. Mpi2ConfigReply_t mpi_reply;
  1456. u16 ioc_status;
  1457. u16 sz;
  1458. int rc = 0;
  1459. unsigned long flags;
  1460. int i, discovery_active;
  1461. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1462. if (_transport_sas_node_find_by_sas_address(ioc,
  1463. phy->identify.sas_address) == NULL) {
  1464. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1465. return -EINVAL;
  1466. }
  1467. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1468. /* handle expander phys */
  1469. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1470. return _transport_expander_phy_control(ioc, phy,
  1471. (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
  1472. SMP_PHY_CONTROL_DISABLE);
  1473. /* handle hba phys */
  1474. /* read sas_iounit page 0 */
  1475. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  1476. sizeof(Mpi2SasIOUnit0PhyData_t));
  1477. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  1478. if (!sas_iounit_pg0) {
  1479. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1480. ioc->name, __FILE__, __LINE__, __func__);
  1481. rc = -ENOMEM;
  1482. goto out;
  1483. }
  1484. if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  1485. sas_iounit_pg0, sz))) {
  1486. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1487. ioc->name, __FILE__, __LINE__, __func__);
  1488. rc = -ENXIO;
  1489. goto out;
  1490. }
  1491. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1492. MPI2_IOCSTATUS_MASK;
  1493. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1494. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1495. ioc->name, __FILE__, __LINE__, __func__);
  1496. rc = -EIO;
  1497. goto out;
  1498. }
  1499. /* unable to enable/disable phys when when discovery is active */
  1500. for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
  1501. if (sas_iounit_pg0->PhyData[i].PortFlags &
  1502. MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
  1503. pr_err(MPT3SAS_FMT "discovery is active on " \
  1504. "port = %d, phy = %d: unable to enable/disable "
  1505. "phys, try again later!\n", ioc->name,
  1506. sas_iounit_pg0->PhyData[i].Port, i);
  1507. discovery_active = 1;
  1508. }
  1509. }
  1510. if (discovery_active) {
  1511. rc = -EAGAIN;
  1512. goto out;
  1513. }
  1514. /* read sas_iounit page 1 */
  1515. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1516. sizeof(Mpi2SasIOUnit1PhyData_t));
  1517. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1518. if (!sas_iounit_pg1) {
  1519. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1520. ioc->name, __FILE__, __LINE__, __func__);
  1521. rc = -ENOMEM;
  1522. goto out;
  1523. }
  1524. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1525. sas_iounit_pg1, sz))) {
  1526. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1527. ioc->name, __FILE__, __LINE__, __func__);
  1528. rc = -ENXIO;
  1529. goto out;
  1530. }
  1531. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1532. MPI2_IOCSTATUS_MASK;
  1533. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1534. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1535. ioc->name, __FILE__, __LINE__, __func__);
  1536. rc = -EIO;
  1537. goto out;
  1538. }
  1539. /* copy Port/PortFlags/PhyFlags from page 0 */
  1540. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  1541. sas_iounit_pg1->PhyData[i].Port =
  1542. sas_iounit_pg0->PhyData[i].Port;
  1543. sas_iounit_pg1->PhyData[i].PortFlags =
  1544. (sas_iounit_pg0->PhyData[i].PortFlags &
  1545. MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
  1546. sas_iounit_pg1->PhyData[i].PhyFlags =
  1547. (sas_iounit_pg0->PhyData[i].PhyFlags &
  1548. (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
  1549. MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
  1550. }
  1551. if (enable)
  1552. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1553. &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1554. else
  1555. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1556. |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1557. mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
  1558. /* link reset */
  1559. if (enable)
  1560. _transport_phy_reset(phy, 0);
  1561. out:
  1562. kfree(sas_iounit_pg1);
  1563. kfree(sas_iounit_pg0);
  1564. return rc;
  1565. }
  1566. /**
  1567. * _transport_phy_speed - set phy min/max link rates
  1568. * @phy: The sas phy object
  1569. * @rates: rates defined in sas_phy_linkrates
  1570. *
  1571. * Only support sas_host direct attached phys.
  1572. * Returns 0 for success, non-zero for failure.
  1573. */
  1574. static int
  1575. _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
  1576. {
  1577. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1578. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1579. Mpi2SasPhyPage0_t phy_pg0;
  1580. Mpi2ConfigReply_t mpi_reply;
  1581. u16 ioc_status;
  1582. u16 sz;
  1583. int i;
  1584. int rc = 0;
  1585. unsigned long flags;
  1586. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1587. if (_transport_sas_node_find_by_sas_address(ioc,
  1588. phy->identify.sas_address) == NULL) {
  1589. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1590. return -EINVAL;
  1591. }
  1592. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1593. if (!rates->minimum_linkrate)
  1594. rates->minimum_linkrate = phy->minimum_linkrate;
  1595. else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
  1596. rates->minimum_linkrate = phy->minimum_linkrate_hw;
  1597. if (!rates->maximum_linkrate)
  1598. rates->maximum_linkrate = phy->maximum_linkrate;
  1599. else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
  1600. rates->maximum_linkrate = phy->maximum_linkrate_hw;
  1601. /* handle expander phys */
  1602. if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
  1603. phy->minimum_linkrate = rates->minimum_linkrate;
  1604. phy->maximum_linkrate = rates->maximum_linkrate;
  1605. return _transport_expander_phy_control(ioc, phy,
  1606. SMP_PHY_CONTROL_LINK_RESET);
  1607. }
  1608. /* handle hba phys */
  1609. /* sas_iounit page 1 */
  1610. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1611. sizeof(Mpi2SasIOUnit1PhyData_t));
  1612. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1613. if (!sas_iounit_pg1) {
  1614. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1615. ioc->name, __FILE__, __LINE__, __func__);
  1616. rc = -ENOMEM;
  1617. goto out;
  1618. }
  1619. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1620. sas_iounit_pg1, sz))) {
  1621. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1622. ioc->name, __FILE__, __LINE__, __func__);
  1623. rc = -ENXIO;
  1624. goto out;
  1625. }
  1626. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1627. MPI2_IOCSTATUS_MASK;
  1628. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1629. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1630. ioc->name, __FILE__, __LINE__, __func__);
  1631. rc = -EIO;
  1632. goto out;
  1633. }
  1634. for (i = 0; i < ioc->sas_hba.num_phys; i++) {
  1635. if (phy->number != i) {
  1636. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1637. (ioc->sas_hba.phy[i].phy->minimum_linkrate +
  1638. (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
  1639. } else {
  1640. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1641. (rates->minimum_linkrate +
  1642. (rates->maximum_linkrate << 4));
  1643. }
  1644. }
  1645. if (mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  1646. sz)) {
  1647. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1648. ioc->name, __FILE__, __LINE__, __func__);
  1649. rc = -ENXIO;
  1650. goto out;
  1651. }
  1652. /* link reset */
  1653. _transport_phy_reset(phy, 0);
  1654. /* read phy page 0, then update the rates in the sas transport phy */
  1655. if (!mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  1656. phy->number)) {
  1657. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  1658. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  1659. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  1660. phy_pg0.ProgrammedLinkRate >> 4);
  1661. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  1662. phy_pg0.NegotiatedLinkRate &
  1663. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  1664. }
  1665. out:
  1666. kfree(sas_iounit_pg1);
  1667. return rc;
  1668. }
  1669. /**
  1670. * _transport_smp_handler - transport portal for smp passthru
  1671. * @shost: shost object
  1672. * @rphy: sas transport rphy object
  1673. * @req:
  1674. *
  1675. * This used primarily for smp_utils.
  1676. * Example:
  1677. * smp_rep_general /sys/class/bsg/expander-5:0
  1678. */
  1679. static int
  1680. _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1681. struct request *req)
  1682. {
  1683. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1684. Mpi2SmpPassthroughRequest_t *mpi_request;
  1685. Mpi2SmpPassthroughReply_t *mpi_reply;
  1686. int rc;
  1687. u16 smid;
  1688. u32 ioc_state;
  1689. unsigned long timeleft;
  1690. void *psge;
  1691. u8 issue_reset = 0;
  1692. dma_addr_t dma_addr_in = 0;
  1693. dma_addr_t dma_addr_out = 0;
  1694. dma_addr_t pci_dma_in = 0;
  1695. dma_addr_t pci_dma_out = 0;
  1696. void *pci_addr_in = NULL;
  1697. void *pci_addr_out = NULL;
  1698. u16 wait_state_count;
  1699. struct request *rsp = req->next_rq;
  1700. struct bio_vec bvec;
  1701. struct bvec_iter iter;
  1702. if (!rsp) {
  1703. pr_err(MPT3SAS_FMT "%s: the smp response space is missing\n",
  1704. ioc->name, __func__);
  1705. return -EINVAL;
  1706. }
  1707. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1708. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1709. __func__, ioc->name);
  1710. return -EFAULT;
  1711. }
  1712. rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
  1713. if (rc)
  1714. return rc;
  1715. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  1716. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n", ioc->name,
  1717. __func__);
  1718. rc = -EAGAIN;
  1719. goto out;
  1720. }
  1721. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  1722. /* Check if the request is split across multiple segments */
  1723. if (bio_multiple_segments(req->bio)) {
  1724. u32 offset = 0;
  1725. /* Allocate memory and copy the request */
  1726. pci_addr_out = pci_alloc_consistent(ioc->pdev,
  1727. blk_rq_bytes(req), &pci_dma_out);
  1728. if (!pci_addr_out) {
  1729. pr_info(MPT3SAS_FMT "%s(): PCI Addr out = NULL\n",
  1730. ioc->name, __func__);
  1731. rc = -ENOMEM;
  1732. goto out;
  1733. }
  1734. bio_for_each_segment(bvec, req->bio, iter) {
  1735. memcpy(pci_addr_out + offset,
  1736. page_address(bvec.bv_page) + bvec.bv_offset,
  1737. bvec.bv_len);
  1738. offset += bvec.bv_len;
  1739. }
  1740. } else {
  1741. dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
  1742. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  1743. if (!dma_addr_out) {
  1744. pr_info(MPT3SAS_FMT "%s(): DMA Addr out = NULL\n",
  1745. ioc->name, __func__);
  1746. rc = -ENOMEM;
  1747. goto free_pci;
  1748. }
  1749. }
  1750. /* Check if the response needs to be populated across
  1751. * multiple segments */
  1752. if (bio_multiple_segments(rsp->bio)) {
  1753. pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
  1754. &pci_dma_in);
  1755. if (!pci_addr_in) {
  1756. pr_info(MPT3SAS_FMT "%s(): PCI Addr in = NULL\n",
  1757. ioc->name, __func__);
  1758. rc = -ENOMEM;
  1759. goto unmap;
  1760. }
  1761. } else {
  1762. dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
  1763. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  1764. if (!dma_addr_in) {
  1765. pr_info(MPT3SAS_FMT "%s(): DMA Addr in = NULL\n",
  1766. ioc->name, __func__);
  1767. rc = -ENOMEM;
  1768. goto unmap;
  1769. }
  1770. }
  1771. wait_state_count = 0;
  1772. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1773. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1774. if (wait_state_count++ == 10) {
  1775. pr_err(MPT3SAS_FMT
  1776. "%s: failed due to ioc not operational\n",
  1777. ioc->name, __func__);
  1778. rc = -EFAULT;
  1779. goto unmap;
  1780. }
  1781. ssleep(1);
  1782. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1783. pr_info(MPT3SAS_FMT
  1784. "%s: waiting for operational state(count=%d)\n",
  1785. ioc->name, __func__, wait_state_count);
  1786. }
  1787. if (wait_state_count)
  1788. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1789. ioc->name, __func__);
  1790. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1791. if (!smid) {
  1792. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1793. ioc->name, __func__);
  1794. rc = -EAGAIN;
  1795. goto unmap;
  1796. }
  1797. rc = 0;
  1798. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1799. ioc->transport_cmds.smid = smid;
  1800. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1801. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1802. mpi_request->PhysicalPort = 0xFF;
  1803. mpi_request->SASAddress = (rphy) ?
  1804. cpu_to_le64(rphy->identify.sas_address) :
  1805. cpu_to_le64(ioc->sas_hba.sas_address);
  1806. mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1807. psge = &mpi_request->SGL;
  1808. if (bio_multiple_segments(req->bio))
  1809. ioc->build_sg(ioc, psge, pci_dma_out, (blk_rq_bytes(req) - 4),
  1810. pci_dma_in, (blk_rq_bytes(rsp) + 4));
  1811. else
  1812. ioc->build_sg(ioc, psge, dma_addr_out, (blk_rq_bytes(req) - 4),
  1813. dma_addr_in, (blk_rq_bytes(rsp) + 4));
  1814. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1815. "%s - sending smp request\n", ioc->name, __func__));
  1816. init_completion(&ioc->transport_cmds.done);
  1817. mpt3sas_base_put_smid_default(ioc, smid);
  1818. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1819. 10*HZ);
  1820. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1821. pr_err(MPT3SAS_FMT "%s : timeout\n",
  1822. __func__, ioc->name);
  1823. _debug_dump_mf(mpi_request,
  1824. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1825. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1826. issue_reset = 1;
  1827. goto issue_host_reset;
  1828. }
  1829. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1830. "%s - complete\n", ioc->name, __func__));
  1831. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1832. mpi_reply = ioc->transport_cmds.reply;
  1833. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1834. "%s - reply data transfer size(%d)\n",
  1835. ioc->name, __func__,
  1836. le16_to_cpu(mpi_reply->ResponseDataLength)));
  1837. memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
  1838. req->sense_len = sizeof(*mpi_reply);
  1839. req->resid_len = 0;
  1840. rsp->resid_len -=
  1841. le16_to_cpu(mpi_reply->ResponseDataLength);
  1842. /* check if the resp needs to be copied from the allocated
  1843. * pci mem */
  1844. if (bio_multiple_segments(rsp->bio)) {
  1845. u32 offset = 0;
  1846. u32 bytes_to_copy =
  1847. le16_to_cpu(mpi_reply->ResponseDataLength);
  1848. bio_for_each_segment(bvec, rsp->bio, iter) {
  1849. if (bytes_to_copy <= bvec.bv_len) {
  1850. memcpy(page_address(bvec.bv_page) +
  1851. bvec.bv_offset, pci_addr_in +
  1852. offset, bytes_to_copy);
  1853. break;
  1854. } else {
  1855. memcpy(page_address(bvec.bv_page) +
  1856. bvec.bv_offset, pci_addr_in +
  1857. offset, bvec.bv_len);
  1858. bytes_to_copy -= bvec.bv_len;
  1859. }
  1860. offset += bvec.bv_len;
  1861. }
  1862. }
  1863. } else {
  1864. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1865. "%s - no reply\n", ioc->name, __func__));
  1866. rc = -ENXIO;
  1867. }
  1868. issue_host_reset:
  1869. if (issue_reset) {
  1870. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1871. FORCE_BIG_HAMMER);
  1872. rc = -ETIMEDOUT;
  1873. }
  1874. unmap:
  1875. if (dma_addr_out)
  1876. pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
  1877. PCI_DMA_BIDIRECTIONAL);
  1878. if (dma_addr_in)
  1879. pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
  1880. PCI_DMA_BIDIRECTIONAL);
  1881. free_pci:
  1882. if (pci_addr_out)
  1883. pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
  1884. pci_dma_out);
  1885. if (pci_addr_in)
  1886. pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
  1887. pci_dma_in);
  1888. out:
  1889. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1890. mutex_unlock(&ioc->transport_cmds.mutex);
  1891. return rc;
  1892. }
  1893. struct sas_function_template mpt3sas_transport_functions = {
  1894. .get_linkerrors = _transport_get_linkerrors,
  1895. .get_enclosure_identifier = _transport_get_enclosure_identifier,
  1896. .get_bay_identifier = _transport_get_bay_identifier,
  1897. .phy_reset = _transport_phy_reset,
  1898. .phy_enable = _transport_phy_enable,
  1899. .set_phy_speed = _transport_phy_speed,
  1900. .smp_handler = _transport_smp_handler,
  1901. };
  1902. struct scsi_transport_template *mpt3sas_transport_template;