mpt2sas_transport.c 65 KB

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