mpt3sas_transport.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128
  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 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. if (mpt3sas_phy->phy)
  911. mpt3sas_phy->phy->negotiated_linkrate =
  912. _transport_convert_phy_link_rate(link_rate);
  913. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  914. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  915. "refresh: parent sas_addr(0x%016llx),\n"
  916. "\tlink_rate(0x%02x), phy(%d)\n"
  917. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  918. (unsigned long long)sas_address,
  919. link_rate, phy_number, handle, (unsigned long long)
  920. mpt3sas_phy->remote_identify.sas_address);
  921. }
  922. static inline void *
  923. phy_to_ioc(struct sas_phy *phy)
  924. {
  925. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  926. return shost_priv(shost);
  927. }
  928. static inline void *
  929. rphy_to_ioc(struct sas_rphy *rphy)
  930. {
  931. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  932. return shost_priv(shost);
  933. }
  934. /* report phy error log structure */
  935. struct phy_error_log_request {
  936. u8 smp_frame_type; /* 0x40 */
  937. u8 function; /* 0x11 */
  938. u8 allocated_response_length;
  939. u8 request_length; /* 02 */
  940. u8 reserved_1[5];
  941. u8 phy_identifier;
  942. u8 reserved_2[2];
  943. };
  944. /* report phy error log reply structure */
  945. struct phy_error_log_reply {
  946. u8 smp_frame_type; /* 0x41 */
  947. u8 function; /* 0x11 */
  948. u8 function_result;
  949. u8 response_length;
  950. __be16 expander_change_count;
  951. u8 reserved_1[3];
  952. u8 phy_identifier;
  953. u8 reserved_2[2];
  954. __be32 invalid_dword;
  955. __be32 running_disparity_error;
  956. __be32 loss_of_dword_sync;
  957. __be32 phy_reset_problem;
  958. };
  959. /**
  960. * _transport_get_expander_phy_error_log - return expander counters
  961. * @ioc: per adapter object
  962. * @phy: The sas phy object
  963. *
  964. * Returns 0 for success, non-zero for failure.
  965. *
  966. */
  967. static int
  968. _transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER *ioc,
  969. struct sas_phy *phy)
  970. {
  971. Mpi2SmpPassthroughRequest_t *mpi_request;
  972. Mpi2SmpPassthroughReply_t *mpi_reply;
  973. struct phy_error_log_request *phy_error_log_request;
  974. struct phy_error_log_reply *phy_error_log_reply;
  975. int rc;
  976. u16 smid;
  977. u32 ioc_state;
  978. unsigned long timeleft;
  979. void *psge;
  980. u8 issue_reset = 0;
  981. void *data_out = NULL;
  982. dma_addr_t data_out_dma;
  983. u32 sz;
  984. u16 wait_state_count;
  985. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  986. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  987. __func__, ioc->name);
  988. return -EFAULT;
  989. }
  990. mutex_lock(&ioc->transport_cmds.mutex);
  991. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  992. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  993. ioc->name, __func__);
  994. rc = -EAGAIN;
  995. goto out;
  996. }
  997. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  998. wait_state_count = 0;
  999. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1000. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1001. if (wait_state_count++ == 10) {
  1002. pr_err(MPT3SAS_FMT
  1003. "%s: failed due to ioc not operational\n",
  1004. ioc->name, __func__);
  1005. rc = -EFAULT;
  1006. goto out;
  1007. }
  1008. ssleep(1);
  1009. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1010. pr_info(MPT3SAS_FMT
  1011. "%s: waiting for operational state(count=%d)\n",
  1012. ioc->name, __func__, wait_state_count);
  1013. }
  1014. if (wait_state_count)
  1015. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1016. ioc->name, __func__);
  1017. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1018. if (!smid) {
  1019. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1020. ioc->name, __func__);
  1021. rc = -EAGAIN;
  1022. goto out;
  1023. }
  1024. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1025. ioc->transport_cmds.smid = smid;
  1026. sz = sizeof(struct phy_error_log_request) +
  1027. sizeof(struct phy_error_log_reply);
  1028. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1029. if (!data_out) {
  1030. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  1031. __LINE__, __func__);
  1032. rc = -ENOMEM;
  1033. mpt3sas_base_free_smid(ioc, smid);
  1034. goto out;
  1035. }
  1036. rc = -EINVAL;
  1037. memset(data_out, 0, sz);
  1038. phy_error_log_request = data_out;
  1039. phy_error_log_request->smp_frame_type = 0x40;
  1040. phy_error_log_request->function = 0x11;
  1041. phy_error_log_request->request_length = 2;
  1042. phy_error_log_request->allocated_response_length = 0;
  1043. phy_error_log_request->phy_identifier = phy->number;
  1044. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1045. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1046. mpi_request->PhysicalPort = 0xFF;
  1047. mpi_request->VF_ID = 0; /* TODO */
  1048. mpi_request->VP_ID = 0;
  1049. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1050. mpi_request->RequestDataLength =
  1051. cpu_to_le16(sizeof(struct phy_error_log_request));
  1052. psge = &mpi_request->SGL;
  1053. ioc->build_sg(ioc, psge, data_out_dma,
  1054. sizeof(struct phy_error_log_request),
  1055. data_out_dma + sizeof(struct phy_error_log_request),
  1056. sizeof(struct phy_error_log_reply));
  1057. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1058. "phy_error_log - send to sas_addr(0x%016llx), phy(%d)\n",
  1059. ioc->name, (unsigned long long)phy->identify.sas_address,
  1060. phy->number));
  1061. init_completion(&ioc->transport_cmds.done);
  1062. mpt3sas_base_put_smid_default(ioc, smid);
  1063. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1064. 10*HZ);
  1065. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1066. pr_err(MPT3SAS_FMT "%s: timeout\n",
  1067. ioc->name, __func__);
  1068. _debug_dump_mf(mpi_request,
  1069. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1070. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1071. issue_reset = 1;
  1072. goto issue_host_reset;
  1073. }
  1074. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1075. "phy_error_log - complete\n", ioc->name));
  1076. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1077. mpi_reply = ioc->transport_cmds.reply;
  1078. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1079. "phy_error_log - reply data transfer size(%d)\n",
  1080. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1081. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1082. sizeof(struct phy_error_log_reply))
  1083. goto out;
  1084. phy_error_log_reply = data_out +
  1085. sizeof(struct phy_error_log_request);
  1086. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1087. "phy_error_log - function_result(%d)\n",
  1088. ioc->name, phy_error_log_reply->function_result));
  1089. phy->invalid_dword_count =
  1090. be32_to_cpu(phy_error_log_reply->invalid_dword);
  1091. phy->running_disparity_error_count =
  1092. be32_to_cpu(phy_error_log_reply->running_disparity_error);
  1093. phy->loss_of_dword_sync_count =
  1094. be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
  1095. phy->phy_reset_problem_count =
  1096. be32_to_cpu(phy_error_log_reply->phy_reset_problem);
  1097. rc = 0;
  1098. } else
  1099. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1100. "phy_error_log - no reply\n", ioc->name));
  1101. issue_host_reset:
  1102. if (issue_reset)
  1103. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1104. FORCE_BIG_HAMMER);
  1105. out:
  1106. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1107. if (data_out)
  1108. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1109. mutex_unlock(&ioc->transport_cmds.mutex);
  1110. return rc;
  1111. }
  1112. /**
  1113. * _transport_get_linkerrors - return phy counters for both hba and expanders
  1114. * @phy: The sas phy object
  1115. *
  1116. * Returns 0 for success, non-zero for failure.
  1117. *
  1118. */
  1119. static int
  1120. _transport_get_linkerrors(struct sas_phy *phy)
  1121. {
  1122. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1123. unsigned long flags;
  1124. Mpi2ConfigReply_t mpi_reply;
  1125. Mpi2SasPhyPage1_t phy_pg1;
  1126. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1127. if (_transport_sas_node_find_by_sas_address(ioc,
  1128. phy->identify.sas_address) == NULL) {
  1129. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1130. return -EINVAL;
  1131. }
  1132. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1133. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1134. return _transport_get_expander_phy_error_log(ioc, phy);
  1135. /* get hba phy error logs */
  1136. if ((mpt3sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
  1137. phy->number))) {
  1138. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1139. ioc->name, __FILE__, __LINE__, __func__);
  1140. return -ENXIO;
  1141. }
  1142. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1143. pr_info(MPT3SAS_FMT
  1144. "phy(%d), ioc_status (0x%04x), loginfo(0x%08x)\n",
  1145. ioc->name, phy->number,
  1146. le16_to_cpu(mpi_reply.IOCStatus),
  1147. le32_to_cpu(mpi_reply.IOCLogInfo));
  1148. phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
  1149. phy->running_disparity_error_count =
  1150. le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
  1151. phy->loss_of_dword_sync_count =
  1152. le32_to_cpu(phy_pg1.LossDwordSynchCount);
  1153. phy->phy_reset_problem_count =
  1154. le32_to_cpu(phy_pg1.PhyResetProblemCount);
  1155. return 0;
  1156. }
  1157. /**
  1158. * _transport_get_enclosure_identifier -
  1159. * @phy: The sas phy object
  1160. *
  1161. * Obtain the enclosure logical id for an expander.
  1162. * Returns 0 for success, non-zero for failure.
  1163. */
  1164. static int
  1165. _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1166. {
  1167. struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1168. struct _sas_device *sas_device;
  1169. unsigned long flags;
  1170. int rc;
  1171. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1172. sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
  1173. rphy->identify.sas_address);
  1174. if (sas_device) {
  1175. *identifier = sas_device->enclosure_logical_id;
  1176. rc = 0;
  1177. } else {
  1178. *identifier = 0;
  1179. rc = -ENXIO;
  1180. }
  1181. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1182. return rc;
  1183. }
  1184. /**
  1185. * _transport_get_bay_identifier -
  1186. * @phy: The sas phy object
  1187. *
  1188. * Returns the slot id for a device that resides inside an enclosure.
  1189. */
  1190. static int
  1191. _transport_get_bay_identifier(struct sas_rphy *rphy)
  1192. {
  1193. struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1194. struct _sas_device *sas_device;
  1195. unsigned long flags;
  1196. int rc;
  1197. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1198. sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
  1199. rphy->identify.sas_address);
  1200. if (sas_device)
  1201. rc = sas_device->slot;
  1202. else
  1203. rc = -ENXIO;
  1204. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1205. return rc;
  1206. }
  1207. /* phy control request structure */
  1208. struct phy_control_request {
  1209. u8 smp_frame_type; /* 0x40 */
  1210. u8 function; /* 0x91 */
  1211. u8 allocated_response_length;
  1212. u8 request_length; /* 0x09 */
  1213. u16 expander_change_count;
  1214. u8 reserved_1[3];
  1215. u8 phy_identifier;
  1216. u8 phy_operation;
  1217. u8 reserved_2[13];
  1218. u64 attached_device_name;
  1219. u8 programmed_min_physical_link_rate;
  1220. u8 programmed_max_physical_link_rate;
  1221. u8 reserved_3[6];
  1222. };
  1223. /* phy control reply structure */
  1224. struct phy_control_reply {
  1225. u8 smp_frame_type; /* 0x41 */
  1226. u8 function; /* 0x11 */
  1227. u8 function_result;
  1228. u8 response_length;
  1229. };
  1230. #define SMP_PHY_CONTROL_LINK_RESET (0x01)
  1231. #define SMP_PHY_CONTROL_HARD_RESET (0x02)
  1232. #define SMP_PHY_CONTROL_DISABLE (0x03)
  1233. /**
  1234. * _transport_expander_phy_control - expander phy control
  1235. * @ioc: per adapter object
  1236. * @phy: The sas phy object
  1237. *
  1238. * Returns 0 for success, non-zero for failure.
  1239. *
  1240. */
  1241. static int
  1242. _transport_expander_phy_control(struct MPT3SAS_ADAPTER *ioc,
  1243. struct sas_phy *phy, u8 phy_operation)
  1244. {
  1245. Mpi2SmpPassthroughRequest_t *mpi_request;
  1246. Mpi2SmpPassthroughReply_t *mpi_reply;
  1247. struct phy_control_request *phy_control_request;
  1248. struct phy_control_reply *phy_control_reply;
  1249. int rc;
  1250. u16 smid;
  1251. u32 ioc_state;
  1252. unsigned long timeleft;
  1253. void *psge;
  1254. u32 sgl_flags;
  1255. u8 issue_reset = 0;
  1256. void *data_out = NULL;
  1257. dma_addr_t data_out_dma;
  1258. u32 sz;
  1259. u16 wait_state_count;
  1260. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1261. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1262. __func__, ioc->name);
  1263. return -EFAULT;
  1264. }
  1265. mutex_lock(&ioc->transport_cmds.mutex);
  1266. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  1267. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  1268. ioc->name, __func__);
  1269. rc = -EAGAIN;
  1270. goto out;
  1271. }
  1272. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  1273. wait_state_count = 0;
  1274. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1275. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1276. if (wait_state_count++ == 10) {
  1277. pr_err(MPT3SAS_FMT
  1278. "%s: failed due to ioc not operational\n",
  1279. ioc->name, __func__);
  1280. rc = -EFAULT;
  1281. goto out;
  1282. }
  1283. ssleep(1);
  1284. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1285. pr_info(MPT3SAS_FMT
  1286. "%s: waiting for operational state(count=%d)\n",
  1287. ioc->name, __func__, wait_state_count);
  1288. }
  1289. if (wait_state_count)
  1290. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1291. ioc->name, __func__);
  1292. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1293. if (!smid) {
  1294. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1295. ioc->name, __func__);
  1296. rc = -EAGAIN;
  1297. goto out;
  1298. }
  1299. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1300. ioc->transport_cmds.smid = smid;
  1301. sz = sizeof(struct phy_control_request) +
  1302. sizeof(struct phy_control_reply);
  1303. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1304. if (!data_out) {
  1305. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  1306. __LINE__, __func__);
  1307. rc = -ENOMEM;
  1308. mpt3sas_base_free_smid(ioc, smid);
  1309. goto out;
  1310. }
  1311. rc = -EINVAL;
  1312. memset(data_out, 0, sz);
  1313. phy_control_request = data_out;
  1314. phy_control_request->smp_frame_type = 0x40;
  1315. phy_control_request->function = 0x91;
  1316. phy_control_request->request_length = 9;
  1317. phy_control_request->allocated_response_length = 0;
  1318. phy_control_request->phy_identifier = phy->number;
  1319. phy_control_request->phy_operation = phy_operation;
  1320. phy_control_request->programmed_min_physical_link_rate =
  1321. phy->minimum_linkrate << 4;
  1322. phy_control_request->programmed_max_physical_link_rate =
  1323. phy->maximum_linkrate << 4;
  1324. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1325. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1326. mpi_request->PhysicalPort = 0xFF;
  1327. mpi_request->VF_ID = 0; /* TODO */
  1328. mpi_request->VP_ID = 0;
  1329. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1330. mpi_request->RequestDataLength =
  1331. cpu_to_le16(sizeof(struct phy_error_log_request));
  1332. psge = &mpi_request->SGL;
  1333. /* WRITE sgel first */
  1334. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1335. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1336. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1337. ioc->base_add_sg_single(psge, sgl_flags |
  1338. sizeof(struct phy_control_request), data_out_dma);
  1339. /* incr sgel */
  1340. psge += ioc->sge_size;
  1341. /* READ sgel last */
  1342. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1343. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1344. MPI2_SGE_FLAGS_END_OF_LIST);
  1345. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1346. ioc->base_add_sg_single(psge, sgl_flags |
  1347. sizeof(struct phy_control_reply), data_out_dma +
  1348. sizeof(struct phy_control_request));
  1349. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1350. "phy_control - send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n",
  1351. ioc->name, (unsigned long long)phy->identify.sas_address,
  1352. phy->number, phy_operation));
  1353. init_completion(&ioc->transport_cmds.done);
  1354. mpt3sas_base_put_smid_default(ioc, smid);
  1355. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1356. 10*HZ);
  1357. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1358. pr_err(MPT3SAS_FMT "%s: timeout\n",
  1359. ioc->name, __func__);
  1360. _debug_dump_mf(mpi_request,
  1361. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1362. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1363. issue_reset = 1;
  1364. goto issue_host_reset;
  1365. }
  1366. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1367. "phy_control - complete\n", ioc->name));
  1368. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1369. mpi_reply = ioc->transport_cmds.reply;
  1370. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1371. "phy_control - reply data transfer size(%d)\n",
  1372. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1373. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1374. sizeof(struct phy_control_reply))
  1375. goto out;
  1376. phy_control_reply = data_out +
  1377. sizeof(struct phy_control_request);
  1378. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1379. "phy_control - function_result(%d)\n",
  1380. ioc->name, phy_control_reply->function_result));
  1381. rc = 0;
  1382. } else
  1383. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1384. "phy_control - no reply\n", ioc->name));
  1385. issue_host_reset:
  1386. if (issue_reset)
  1387. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1388. FORCE_BIG_HAMMER);
  1389. out:
  1390. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1391. if (data_out)
  1392. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1393. mutex_unlock(&ioc->transport_cmds.mutex);
  1394. return rc;
  1395. }
  1396. /**
  1397. * _transport_phy_reset -
  1398. * @phy: The sas phy object
  1399. * @hard_reset:
  1400. *
  1401. * Returns 0 for success, non-zero for failure.
  1402. */
  1403. static int
  1404. _transport_phy_reset(struct sas_phy *phy, int hard_reset)
  1405. {
  1406. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1407. Mpi2SasIoUnitControlReply_t mpi_reply;
  1408. Mpi2SasIoUnitControlRequest_t mpi_request;
  1409. unsigned long flags;
  1410. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1411. if (_transport_sas_node_find_by_sas_address(ioc,
  1412. phy->identify.sas_address) == NULL) {
  1413. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1414. return -EINVAL;
  1415. }
  1416. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1417. /* handle expander phys */
  1418. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1419. return _transport_expander_phy_control(ioc, phy,
  1420. (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
  1421. SMP_PHY_CONTROL_LINK_RESET);
  1422. /* handle hba phys */
  1423. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  1424. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  1425. mpi_request.Operation = hard_reset ?
  1426. MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
  1427. mpi_request.PhyNum = phy->number;
  1428. if ((mpt3sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
  1429. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1430. ioc->name, __FILE__, __LINE__, __func__);
  1431. return -ENXIO;
  1432. }
  1433. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1434. pr_info(MPT3SAS_FMT
  1435. "phy(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
  1436. ioc->name, phy->number, le16_to_cpu(mpi_reply.IOCStatus),
  1437. le32_to_cpu(mpi_reply.IOCLogInfo));
  1438. return 0;
  1439. }
  1440. /**
  1441. * _transport_phy_enable - enable/disable phys
  1442. * @phy: The sas phy object
  1443. * @enable: enable phy when true
  1444. *
  1445. * Only support sas_host direct attached phys.
  1446. * Returns 0 for success, non-zero for failure.
  1447. */
  1448. static int
  1449. _transport_phy_enable(struct sas_phy *phy, int enable)
  1450. {
  1451. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1452. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1453. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  1454. Mpi2ConfigReply_t mpi_reply;
  1455. u16 ioc_status;
  1456. u16 sz;
  1457. int rc = 0;
  1458. unsigned long flags;
  1459. int i, discovery_active;
  1460. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1461. if (_transport_sas_node_find_by_sas_address(ioc,
  1462. phy->identify.sas_address) == NULL) {
  1463. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1464. return -EINVAL;
  1465. }
  1466. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1467. /* handle expander phys */
  1468. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1469. return _transport_expander_phy_control(ioc, phy,
  1470. (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
  1471. SMP_PHY_CONTROL_DISABLE);
  1472. /* handle hba phys */
  1473. /* read sas_iounit page 0 */
  1474. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  1475. sizeof(Mpi2SasIOUnit0PhyData_t));
  1476. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  1477. if (!sas_iounit_pg0) {
  1478. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1479. ioc->name, __FILE__, __LINE__, __func__);
  1480. rc = -ENOMEM;
  1481. goto out;
  1482. }
  1483. if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  1484. sas_iounit_pg0, sz))) {
  1485. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1486. ioc->name, __FILE__, __LINE__, __func__);
  1487. rc = -ENXIO;
  1488. goto out;
  1489. }
  1490. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1491. MPI2_IOCSTATUS_MASK;
  1492. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1493. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1494. ioc->name, __FILE__, __LINE__, __func__);
  1495. rc = -EIO;
  1496. goto out;
  1497. }
  1498. /* unable to enable/disable phys when when discovery is active */
  1499. for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
  1500. if (sas_iounit_pg0->PhyData[i].PortFlags &
  1501. MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
  1502. pr_err(MPT3SAS_FMT "discovery is active on " \
  1503. "port = %d, phy = %d: unable to enable/disable "
  1504. "phys, try again later!\n", ioc->name,
  1505. sas_iounit_pg0->PhyData[i].Port, i);
  1506. discovery_active = 1;
  1507. }
  1508. }
  1509. if (discovery_active) {
  1510. rc = -EAGAIN;
  1511. goto out;
  1512. }
  1513. /* read sas_iounit page 1 */
  1514. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1515. sizeof(Mpi2SasIOUnit1PhyData_t));
  1516. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1517. if (!sas_iounit_pg1) {
  1518. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1519. ioc->name, __FILE__, __LINE__, __func__);
  1520. rc = -ENOMEM;
  1521. goto out;
  1522. }
  1523. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1524. sas_iounit_pg1, sz))) {
  1525. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1526. ioc->name, __FILE__, __LINE__, __func__);
  1527. rc = -ENXIO;
  1528. goto out;
  1529. }
  1530. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1531. MPI2_IOCSTATUS_MASK;
  1532. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1533. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1534. ioc->name, __FILE__, __LINE__, __func__);
  1535. rc = -EIO;
  1536. goto out;
  1537. }
  1538. /* copy Port/PortFlags/PhyFlags from page 0 */
  1539. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  1540. sas_iounit_pg1->PhyData[i].Port =
  1541. sas_iounit_pg0->PhyData[i].Port;
  1542. sas_iounit_pg1->PhyData[i].PortFlags =
  1543. (sas_iounit_pg0->PhyData[i].PortFlags &
  1544. MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
  1545. sas_iounit_pg1->PhyData[i].PhyFlags =
  1546. (sas_iounit_pg0->PhyData[i].PhyFlags &
  1547. (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
  1548. MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
  1549. }
  1550. if (enable)
  1551. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1552. &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1553. else
  1554. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1555. |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1556. mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
  1557. /* link reset */
  1558. if (enable)
  1559. _transport_phy_reset(phy, 0);
  1560. out:
  1561. kfree(sas_iounit_pg1);
  1562. kfree(sas_iounit_pg0);
  1563. return rc;
  1564. }
  1565. /**
  1566. * _transport_phy_speed - set phy min/max link rates
  1567. * @phy: The sas phy object
  1568. * @rates: rates defined in sas_phy_linkrates
  1569. *
  1570. * Only support sas_host direct attached phys.
  1571. * Returns 0 for success, non-zero for failure.
  1572. */
  1573. static int
  1574. _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
  1575. {
  1576. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1577. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1578. Mpi2SasPhyPage0_t phy_pg0;
  1579. Mpi2ConfigReply_t mpi_reply;
  1580. u16 ioc_status;
  1581. u16 sz;
  1582. int i;
  1583. int rc = 0;
  1584. unsigned long flags;
  1585. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1586. if (_transport_sas_node_find_by_sas_address(ioc,
  1587. phy->identify.sas_address) == NULL) {
  1588. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1589. return -EINVAL;
  1590. }
  1591. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1592. if (!rates->minimum_linkrate)
  1593. rates->minimum_linkrate = phy->minimum_linkrate;
  1594. else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
  1595. rates->minimum_linkrate = phy->minimum_linkrate_hw;
  1596. if (!rates->maximum_linkrate)
  1597. rates->maximum_linkrate = phy->maximum_linkrate;
  1598. else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
  1599. rates->maximum_linkrate = phy->maximum_linkrate_hw;
  1600. /* handle expander phys */
  1601. if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
  1602. phy->minimum_linkrate = rates->minimum_linkrate;
  1603. phy->maximum_linkrate = rates->maximum_linkrate;
  1604. return _transport_expander_phy_control(ioc, phy,
  1605. SMP_PHY_CONTROL_LINK_RESET);
  1606. }
  1607. /* handle hba phys */
  1608. /* sas_iounit page 1 */
  1609. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1610. sizeof(Mpi2SasIOUnit1PhyData_t));
  1611. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1612. if (!sas_iounit_pg1) {
  1613. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1614. ioc->name, __FILE__, __LINE__, __func__);
  1615. rc = -ENOMEM;
  1616. goto out;
  1617. }
  1618. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1619. sas_iounit_pg1, sz))) {
  1620. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1621. ioc->name, __FILE__, __LINE__, __func__);
  1622. rc = -ENXIO;
  1623. goto out;
  1624. }
  1625. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1626. MPI2_IOCSTATUS_MASK;
  1627. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1628. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1629. ioc->name, __FILE__, __LINE__, __func__);
  1630. rc = -EIO;
  1631. goto out;
  1632. }
  1633. for (i = 0; i < ioc->sas_hba.num_phys; i++) {
  1634. if (phy->number != i) {
  1635. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1636. (ioc->sas_hba.phy[i].phy->minimum_linkrate +
  1637. (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
  1638. } else {
  1639. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1640. (rates->minimum_linkrate +
  1641. (rates->maximum_linkrate << 4));
  1642. }
  1643. }
  1644. if (mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  1645. sz)) {
  1646. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1647. ioc->name, __FILE__, __LINE__, __func__);
  1648. rc = -ENXIO;
  1649. goto out;
  1650. }
  1651. /* link reset */
  1652. _transport_phy_reset(phy, 0);
  1653. /* read phy page 0, then update the rates in the sas transport phy */
  1654. if (!mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  1655. phy->number)) {
  1656. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  1657. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  1658. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  1659. phy_pg0.ProgrammedLinkRate >> 4);
  1660. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  1661. phy_pg0.NegotiatedLinkRate &
  1662. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  1663. }
  1664. out:
  1665. kfree(sas_iounit_pg1);
  1666. return rc;
  1667. }
  1668. /**
  1669. * _transport_smp_handler - transport portal for smp passthru
  1670. * @shost: shost object
  1671. * @rphy: sas transport rphy object
  1672. * @req:
  1673. *
  1674. * This used primarily for smp_utils.
  1675. * Example:
  1676. * smp_rep_general /sys/class/bsg/expander-5:0
  1677. */
  1678. static int
  1679. _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1680. struct request *req)
  1681. {
  1682. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1683. Mpi2SmpPassthroughRequest_t *mpi_request;
  1684. Mpi2SmpPassthroughReply_t *mpi_reply;
  1685. int rc, i;
  1686. u16 smid;
  1687. u32 ioc_state;
  1688. unsigned long timeleft;
  1689. void *psge;
  1690. u8 issue_reset = 0;
  1691. dma_addr_t dma_addr_in = 0;
  1692. dma_addr_t dma_addr_out = 0;
  1693. dma_addr_t pci_dma_in = 0;
  1694. dma_addr_t pci_dma_out = 0;
  1695. void *pci_addr_in = NULL;
  1696. void *pci_addr_out = NULL;
  1697. u16 wait_state_count;
  1698. struct request *rsp = req->next_rq;
  1699. struct bio_vec *bvec = NULL;
  1700. if (!rsp) {
  1701. pr_err(MPT3SAS_FMT "%s: the smp response space is missing\n",
  1702. ioc->name, __func__);
  1703. return -EINVAL;
  1704. }
  1705. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1706. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1707. __func__, ioc->name);
  1708. return -EFAULT;
  1709. }
  1710. rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
  1711. if (rc)
  1712. return rc;
  1713. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  1714. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n", ioc->name,
  1715. __func__);
  1716. rc = -EAGAIN;
  1717. goto out;
  1718. }
  1719. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  1720. /* Check if the request is split across multiple segments */
  1721. if (req->bio->bi_vcnt > 1) {
  1722. u32 offset = 0;
  1723. /* Allocate memory and copy the request */
  1724. pci_addr_out = pci_alloc_consistent(ioc->pdev,
  1725. blk_rq_bytes(req), &pci_dma_out);
  1726. if (!pci_addr_out) {
  1727. pr_info(MPT3SAS_FMT "%s(): PCI Addr out = NULL\n",
  1728. ioc->name, __func__);
  1729. rc = -ENOMEM;
  1730. goto out;
  1731. }
  1732. bio_for_each_segment(bvec, req->bio, i) {
  1733. memcpy(pci_addr_out + offset,
  1734. page_address(bvec->bv_page) + bvec->bv_offset,
  1735. bvec->bv_len);
  1736. offset += bvec->bv_len;
  1737. }
  1738. } else {
  1739. dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
  1740. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  1741. if (!dma_addr_out) {
  1742. pr_info(MPT3SAS_FMT "%s(): DMA Addr out = NULL\n",
  1743. ioc->name, __func__);
  1744. rc = -ENOMEM;
  1745. goto free_pci;
  1746. }
  1747. }
  1748. /* Check if the response needs to be populated across
  1749. * multiple segments */
  1750. if (rsp->bio->bi_vcnt > 1) {
  1751. pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
  1752. &pci_dma_in);
  1753. if (!pci_addr_in) {
  1754. pr_info(MPT3SAS_FMT "%s(): PCI Addr in = NULL\n",
  1755. ioc->name, __func__);
  1756. rc = -ENOMEM;
  1757. goto unmap;
  1758. }
  1759. } else {
  1760. dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
  1761. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  1762. if (!dma_addr_in) {
  1763. pr_info(MPT3SAS_FMT "%s(): DMA Addr in = NULL\n",
  1764. ioc->name, __func__);
  1765. rc = -ENOMEM;
  1766. goto unmap;
  1767. }
  1768. }
  1769. wait_state_count = 0;
  1770. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1771. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1772. if (wait_state_count++ == 10) {
  1773. pr_err(MPT3SAS_FMT
  1774. "%s: failed due to ioc not operational\n",
  1775. ioc->name, __func__);
  1776. rc = -EFAULT;
  1777. goto unmap;
  1778. }
  1779. ssleep(1);
  1780. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1781. pr_info(MPT3SAS_FMT
  1782. "%s: waiting for operational state(count=%d)\n",
  1783. ioc->name, __func__, wait_state_count);
  1784. }
  1785. if (wait_state_count)
  1786. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1787. ioc->name, __func__);
  1788. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1789. if (!smid) {
  1790. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1791. ioc->name, __func__);
  1792. rc = -EAGAIN;
  1793. goto unmap;
  1794. }
  1795. rc = 0;
  1796. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1797. ioc->transport_cmds.smid = smid;
  1798. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1799. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1800. mpi_request->PhysicalPort = 0xFF;
  1801. mpi_request->SASAddress = (rphy) ?
  1802. cpu_to_le64(rphy->identify.sas_address) :
  1803. cpu_to_le64(ioc->sas_hba.sas_address);
  1804. mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1805. psge = &mpi_request->SGL;
  1806. if (req->bio->bi_vcnt > 1)
  1807. ioc->build_sg(ioc, psge, pci_dma_out, (blk_rq_bytes(req) - 4),
  1808. pci_dma_in, (blk_rq_bytes(rsp) + 4));
  1809. else
  1810. ioc->build_sg(ioc, psge, dma_addr_out, (blk_rq_bytes(req) - 4),
  1811. dma_addr_in, (blk_rq_bytes(rsp) + 4));
  1812. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1813. "%s - sending smp request\n", ioc->name, __func__));
  1814. init_completion(&ioc->transport_cmds.done);
  1815. mpt3sas_base_put_smid_default(ioc, smid);
  1816. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1817. 10*HZ);
  1818. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1819. pr_err(MPT3SAS_FMT "%s : timeout\n",
  1820. __func__, ioc->name);
  1821. _debug_dump_mf(mpi_request,
  1822. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1823. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1824. issue_reset = 1;
  1825. goto issue_host_reset;
  1826. }
  1827. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1828. "%s - complete\n", ioc->name, __func__));
  1829. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1830. mpi_reply = ioc->transport_cmds.reply;
  1831. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1832. "%s - reply data transfer size(%d)\n",
  1833. ioc->name, __func__,
  1834. le16_to_cpu(mpi_reply->ResponseDataLength)));
  1835. memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
  1836. req->sense_len = sizeof(*mpi_reply);
  1837. req->resid_len = 0;
  1838. rsp->resid_len -=
  1839. le16_to_cpu(mpi_reply->ResponseDataLength);
  1840. /* check if the resp needs to be copied from the allocated
  1841. * pci mem */
  1842. if (rsp->bio->bi_vcnt > 1) {
  1843. u32 offset = 0;
  1844. u32 bytes_to_copy =
  1845. le16_to_cpu(mpi_reply->ResponseDataLength);
  1846. bio_for_each_segment(bvec, rsp->bio, i) {
  1847. if (bytes_to_copy <= bvec->bv_len) {
  1848. memcpy(page_address(bvec->bv_page) +
  1849. bvec->bv_offset, pci_addr_in +
  1850. offset, bytes_to_copy);
  1851. break;
  1852. } else {
  1853. memcpy(page_address(bvec->bv_page) +
  1854. bvec->bv_offset, pci_addr_in +
  1855. offset, bvec->bv_len);
  1856. bytes_to_copy -= bvec->bv_len;
  1857. }
  1858. offset += bvec->bv_len;
  1859. }
  1860. }
  1861. } else {
  1862. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1863. "%s - no reply\n", ioc->name, __func__));
  1864. rc = -ENXIO;
  1865. }
  1866. issue_host_reset:
  1867. if (issue_reset) {
  1868. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1869. FORCE_BIG_HAMMER);
  1870. rc = -ETIMEDOUT;
  1871. }
  1872. unmap:
  1873. if (dma_addr_out)
  1874. pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
  1875. PCI_DMA_BIDIRECTIONAL);
  1876. if (dma_addr_in)
  1877. pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
  1878. PCI_DMA_BIDIRECTIONAL);
  1879. free_pci:
  1880. if (pci_addr_out)
  1881. pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
  1882. pci_dma_out);
  1883. if (pci_addr_in)
  1884. pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
  1885. pci_dma_in);
  1886. out:
  1887. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1888. mutex_unlock(&ioc->transport_cmds.mutex);
  1889. return rc;
  1890. }
  1891. struct sas_function_template mpt3sas_transport_functions = {
  1892. .get_linkerrors = _transport_get_linkerrors,
  1893. .get_enclosure_identifier = _transport_get_enclosure_identifier,
  1894. .get_bay_identifier = _transport_get_bay_identifier,
  1895. .phy_reset = _transport_phy_reset,
  1896. .phy_enable = _transport_phy_enable,
  1897. .set_phy_speed = _transport_phy_speed,
  1898. .smp_handler = _transport_smp_handler,
  1899. };
  1900. struct scsi_transport_template *mpt3sas_transport_template;