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