ibmvscsi.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. /* ------------------------------------------------------------
  2. * ibmvscsi.c
  3. * (C) Copyright IBM Corporation 1994, 2004
  4. * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
  5. * Santiago Leon (santil@us.ibm.com)
  6. * Dave Boutcher (sleddog@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. *
  23. * ------------------------------------------------------------
  24. * Emulation of a SCSI host adapter for Virtual I/O devices
  25. *
  26. * This driver supports the SCSI adapter implemented by the IBM
  27. * Power5 firmware. That SCSI adapter is not a physical adapter,
  28. * but allows Linux SCSI peripheral drivers to directly
  29. * access devices in another logical partition on the physical system.
  30. *
  31. * The virtual adapter(s) are present in the open firmware device
  32. * tree just like real adapters.
  33. *
  34. * One of the capabilities provided on these systems is the ability
  35. * to DMA between partitions. The architecture states that for VSCSI,
  36. * the server side is allowed to DMA to and from the client. The client
  37. * is never trusted to DMA to or from the server directly.
  38. *
  39. * Messages are sent between partitions on a "Command/Response Queue"
  40. * (CRQ), which is just a buffer of 16 byte entries in the receiver's
  41. * Senders cannot access the buffer directly, but send messages by
  42. * making a hypervisor call and passing in the 16 bytes. The hypervisor
  43. * puts the message in the next 16 byte space in round-robin fashion,
  44. * turns on the high order bit of the message (the valid bit), and
  45. * generates an interrupt to the receiver (if interrupts are turned on.)
  46. * The receiver just turns off the valid bit when they have copied out
  47. * the message.
  48. *
  49. * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
  50. * (IU) (as defined in the T10 standard available at www.t10.org), gets
  51. * a DMA address for the message, and sends it to the server as the
  52. * payload of a CRQ message. The server DMAs the SRP IU and processes it,
  53. * including doing any additional data transfers. When it is done, it
  54. * DMAs the SRP response back to the same address as the request came from,
  55. * and sends a CRQ message back to inform the client that the request has
  56. * completed.
  57. *
  58. * TODO: This is currently pretty tied to the IBM pSeries hypervisor
  59. * interfaces. It would be really nice to abstract this above an RDMA
  60. * layer.
  61. */
  62. #include <linux/module.h>
  63. #include <linux/moduleparam.h>
  64. #include <linux/dma-mapping.h>
  65. #include <linux/delay.h>
  66. #include <linux/slab.h>
  67. #include <linux/of.h>
  68. #include <linux/pm.h>
  69. #include <linux/kthread.h>
  70. #include <asm/firmware.h>
  71. #include <asm/vio.h>
  72. #include <scsi/scsi.h>
  73. #include <scsi/scsi_cmnd.h>
  74. #include <scsi/scsi_host.h>
  75. #include <scsi/scsi_device.h>
  76. #include <scsi/scsi_transport_srp.h>
  77. #include "ibmvscsi.h"
  78. /* The values below are somewhat arbitrary default values, but
  79. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  80. * Note that there are 3 bits of channel value, 6 bits of id, and
  81. * 5 bits of LUN.
  82. */
  83. static int max_id = 64;
  84. static int max_channel = 3;
  85. static int init_timeout = 300;
  86. static int login_timeout = 60;
  87. static int info_timeout = 30;
  88. static int abort_timeout = 60;
  89. static int reset_timeout = 60;
  90. static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
  91. static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
  92. static int fast_fail = 1;
  93. static int client_reserve = 1;
  94. static struct scsi_transport_template *ibmvscsi_transport_template;
  95. #define IBMVSCSI_VERSION "1.5.9"
  96. static struct ibmvscsi_ops *ibmvscsi_ops;
  97. MODULE_DESCRIPTION("IBM Virtual SCSI");
  98. MODULE_AUTHOR("Dave Boutcher");
  99. MODULE_LICENSE("GPL");
  100. MODULE_VERSION(IBMVSCSI_VERSION);
  101. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  102. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  103. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  104. MODULE_PARM_DESC(max_channel, "Largest channel value");
  105. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  106. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  107. module_param_named(max_requests, max_requests, int, S_IRUGO);
  108. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  109. module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
  110. MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
  111. module_param_named(client_reserve, client_reserve, int, S_IRUGO );
  112. MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
  113. /* ------------------------------------------------------------
  114. * Routines for the event pool and event structs
  115. */
  116. /**
  117. * initialize_event_pool: - Allocates and initializes the event pool for a host
  118. * @pool: event_pool to be initialized
  119. * @size: Number of events in pool
  120. * @hostdata: ibmvscsi_host_data who owns the event pool
  121. *
  122. * Returns zero on success.
  123. */
  124. static int initialize_event_pool(struct event_pool *pool,
  125. int size, struct ibmvscsi_host_data *hostdata)
  126. {
  127. int i;
  128. pool->size = size;
  129. pool->next = 0;
  130. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  131. if (!pool->events)
  132. return -ENOMEM;
  133. pool->iu_storage =
  134. dma_alloc_coherent(hostdata->dev,
  135. pool->size * sizeof(*pool->iu_storage),
  136. &pool->iu_token, 0);
  137. if (!pool->iu_storage) {
  138. kfree(pool->events);
  139. return -ENOMEM;
  140. }
  141. for (i = 0; i < pool->size; ++i) {
  142. struct srp_event_struct *evt = &pool->events[i];
  143. memset(&evt->crq, 0x00, sizeof(evt->crq));
  144. atomic_set(&evt->free, 1);
  145. evt->crq.valid = 0x80;
  146. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  147. evt->crq.IU_data_ptr = pool->iu_token +
  148. sizeof(*evt->xfer_iu) * i;
  149. evt->xfer_iu = pool->iu_storage + i;
  150. evt->hostdata = hostdata;
  151. evt->ext_list = NULL;
  152. evt->ext_list_token = 0;
  153. }
  154. return 0;
  155. }
  156. /**
  157. * release_event_pool: - Frees memory of an event pool of a host
  158. * @pool: event_pool to be released
  159. * @hostdata: ibmvscsi_host_data who owns the even pool
  160. *
  161. * Returns zero on success.
  162. */
  163. static void release_event_pool(struct event_pool *pool,
  164. struct ibmvscsi_host_data *hostdata)
  165. {
  166. int i, in_use = 0;
  167. for (i = 0; i < pool->size; ++i) {
  168. if (atomic_read(&pool->events[i].free) != 1)
  169. ++in_use;
  170. if (pool->events[i].ext_list) {
  171. dma_free_coherent(hostdata->dev,
  172. SG_ALL * sizeof(struct srp_direct_buf),
  173. pool->events[i].ext_list,
  174. pool->events[i].ext_list_token);
  175. }
  176. }
  177. if (in_use)
  178. dev_warn(hostdata->dev, "releasing event pool with %d "
  179. "events still in use?\n", in_use);
  180. kfree(pool->events);
  181. dma_free_coherent(hostdata->dev,
  182. pool->size * sizeof(*pool->iu_storage),
  183. pool->iu_storage, pool->iu_token);
  184. }
  185. /**
  186. * valid_event_struct: - Determines if event is valid.
  187. * @pool: event_pool that contains the event
  188. * @evt: srp_event_struct to be checked for validity
  189. *
  190. * Returns zero if event is invalid, one otherwise.
  191. */
  192. static int valid_event_struct(struct event_pool *pool,
  193. struct srp_event_struct *evt)
  194. {
  195. int index = evt - pool->events;
  196. if (index < 0 || index >= pool->size) /* outside of bounds */
  197. return 0;
  198. if (evt != pool->events + index) /* unaligned */
  199. return 0;
  200. return 1;
  201. }
  202. /**
  203. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  204. * @pool: event_pool that contains the event
  205. * @evt: srp_event_struct to be modified
  206. *
  207. */
  208. static void free_event_struct(struct event_pool *pool,
  209. struct srp_event_struct *evt)
  210. {
  211. if (!valid_event_struct(pool, evt)) {
  212. dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
  213. "(not in pool %p)\n", evt, pool->events);
  214. return;
  215. }
  216. if (atomic_inc_return(&evt->free) != 1) {
  217. dev_err(evt->hostdata->dev, "Freeing event_struct %p "
  218. "which is not in use!\n", evt);
  219. return;
  220. }
  221. }
  222. /**
  223. * get_evt_struct: - Gets the next free event in pool
  224. * @pool: event_pool that contains the events to be searched
  225. *
  226. * Returns the next event in "free" state, and NULL if none are free.
  227. * Note that no synchronization is done here, we assume the host_lock
  228. * will syncrhonze things.
  229. */
  230. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  231. {
  232. int i;
  233. int poolsize = pool->size;
  234. int offset = pool->next;
  235. for (i = 0; i < poolsize; i++) {
  236. offset = (offset + 1) % poolsize;
  237. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  238. pool->next = offset;
  239. return &pool->events[offset];
  240. }
  241. }
  242. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  243. return NULL;
  244. }
  245. /**
  246. * init_event_struct: Initialize fields in an event struct that are always
  247. * required.
  248. * @evt: The event
  249. * @done: Routine to call when the event is responded to
  250. * @format: SRP or MAD format
  251. * @timeout: timeout value set in the CRQ
  252. */
  253. static void init_event_struct(struct srp_event_struct *evt_struct,
  254. void (*done) (struct srp_event_struct *),
  255. u8 format,
  256. int timeout)
  257. {
  258. evt_struct->cmnd = NULL;
  259. evt_struct->cmnd_done = NULL;
  260. evt_struct->sync_srp = NULL;
  261. evt_struct->crq.format = format;
  262. evt_struct->crq.timeout = timeout;
  263. evt_struct->done = done;
  264. }
  265. /* ------------------------------------------------------------
  266. * Routines for receiving SCSI responses from the hosting partition
  267. */
  268. /**
  269. * set_srp_direction: Set the fields in the srp related to data
  270. * direction and number of buffers based on the direction in
  271. * the scsi_cmnd and the number of buffers
  272. */
  273. static void set_srp_direction(struct scsi_cmnd *cmd,
  274. struct srp_cmd *srp_cmd,
  275. int numbuf)
  276. {
  277. u8 fmt;
  278. if (numbuf == 0)
  279. return;
  280. if (numbuf == 1)
  281. fmt = SRP_DATA_DESC_DIRECT;
  282. else {
  283. fmt = SRP_DATA_DESC_INDIRECT;
  284. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  285. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  286. srp_cmd->data_out_desc_cnt = numbuf;
  287. else
  288. srp_cmd->data_in_desc_cnt = numbuf;
  289. }
  290. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  291. srp_cmd->buf_fmt = fmt << 4;
  292. else
  293. srp_cmd->buf_fmt = fmt;
  294. }
  295. /**
  296. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  297. * @cmd: srp_cmd whose additional_data member will be unmapped
  298. * @dev: device for which the memory is mapped
  299. *
  300. */
  301. static void unmap_cmd_data(struct srp_cmd *cmd,
  302. struct srp_event_struct *evt_struct,
  303. struct device *dev)
  304. {
  305. u8 out_fmt, in_fmt;
  306. out_fmt = cmd->buf_fmt >> 4;
  307. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  308. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  309. return;
  310. if (evt_struct->cmnd)
  311. scsi_dma_unmap(evt_struct->cmnd);
  312. }
  313. static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
  314. struct srp_direct_buf *md)
  315. {
  316. int i;
  317. struct scatterlist *sg;
  318. u64 total_length = 0;
  319. scsi_for_each_sg(cmd, sg, nseg, i) {
  320. struct srp_direct_buf *descr = md + i;
  321. descr->va = sg_dma_address(sg);
  322. descr->len = sg_dma_len(sg);
  323. descr->key = 0;
  324. total_length += sg_dma_len(sg);
  325. }
  326. return total_length;
  327. }
  328. /**
  329. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  330. * @cmd: Scsi_Cmnd with the scatterlist
  331. * @srp_cmd: srp_cmd that contains the memory descriptor
  332. * @dev: device for which to map dma memory
  333. *
  334. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  335. * Returns 1 on success.
  336. */
  337. static int map_sg_data(struct scsi_cmnd *cmd,
  338. struct srp_event_struct *evt_struct,
  339. struct srp_cmd *srp_cmd, struct device *dev)
  340. {
  341. int sg_mapped;
  342. u64 total_length = 0;
  343. struct srp_direct_buf *data =
  344. (struct srp_direct_buf *) srp_cmd->add_data;
  345. struct srp_indirect_buf *indirect =
  346. (struct srp_indirect_buf *) data;
  347. sg_mapped = scsi_dma_map(cmd);
  348. if (!sg_mapped)
  349. return 1;
  350. else if (sg_mapped < 0)
  351. return 0;
  352. set_srp_direction(cmd, srp_cmd, sg_mapped);
  353. /* special case; we can use a single direct descriptor */
  354. if (sg_mapped == 1) {
  355. map_sg_list(cmd, sg_mapped, data);
  356. return 1;
  357. }
  358. indirect->table_desc.va = 0;
  359. indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
  360. indirect->table_desc.key = 0;
  361. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  362. total_length = map_sg_list(cmd, sg_mapped,
  363. &indirect->desc_list[0]);
  364. indirect->len = total_length;
  365. return 1;
  366. }
  367. /* get indirect table */
  368. if (!evt_struct->ext_list) {
  369. evt_struct->ext_list = (struct srp_direct_buf *)
  370. dma_alloc_coherent(dev,
  371. SG_ALL * sizeof(struct srp_direct_buf),
  372. &evt_struct->ext_list_token, 0);
  373. if (!evt_struct->ext_list) {
  374. if (!firmware_has_feature(FW_FEATURE_CMO))
  375. sdev_printk(KERN_ERR, cmd->device,
  376. "Can't allocate memory "
  377. "for indirect table\n");
  378. scsi_dma_unmap(cmd);
  379. return 0;
  380. }
  381. }
  382. total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
  383. indirect->len = total_length;
  384. indirect->table_desc.va = evt_struct->ext_list_token;
  385. indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
  386. memcpy(indirect->desc_list, evt_struct->ext_list,
  387. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  388. return 1;
  389. }
  390. /**
  391. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  392. * @cmd: struct scsi_cmnd with the memory to be mapped
  393. * @srp_cmd: srp_cmd that contains the memory descriptor
  394. * @dev: dma device for which to map dma memory
  395. *
  396. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  397. * Returns 1 on success.
  398. */
  399. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  400. struct srp_event_struct *evt_struct,
  401. struct srp_cmd *srp_cmd, struct device *dev)
  402. {
  403. switch (cmd->sc_data_direction) {
  404. case DMA_FROM_DEVICE:
  405. case DMA_TO_DEVICE:
  406. break;
  407. case DMA_NONE:
  408. return 1;
  409. case DMA_BIDIRECTIONAL:
  410. sdev_printk(KERN_ERR, cmd->device,
  411. "Can't map DMA_BIDIRECTIONAL to read/write\n");
  412. return 0;
  413. default:
  414. sdev_printk(KERN_ERR, cmd->device,
  415. "Unknown data direction 0x%02x; can't map!\n",
  416. cmd->sc_data_direction);
  417. return 0;
  418. }
  419. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  420. }
  421. /**
  422. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  423. * @hostdata: the adapter
  424. */
  425. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  426. {
  427. struct srp_event_struct *evt;
  428. unsigned long flags;
  429. spin_lock_irqsave(hostdata->host->host_lock, flags);
  430. while (!list_empty(&hostdata->sent)) {
  431. evt = list_first_entry(&hostdata->sent, struct srp_event_struct, list);
  432. list_del(&evt->list);
  433. del_timer(&evt->timer);
  434. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  435. if (evt->cmnd) {
  436. evt->cmnd->result = (error_code << 16);
  437. unmap_cmd_data(&evt->iu.srp.cmd, evt,
  438. evt->hostdata->dev);
  439. if (evt->cmnd_done)
  440. evt->cmnd_done(evt->cmnd);
  441. } else if (evt->done && evt->crq.format != VIOSRP_MAD_FORMAT &&
  442. evt->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  443. evt->done(evt);
  444. free_event_struct(&evt->hostdata->pool, evt);
  445. spin_lock_irqsave(hostdata->host->host_lock, flags);
  446. }
  447. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  448. }
  449. /**
  450. * ibmvscsi_reset_host - Reset the connection to the server
  451. * @hostdata: struct ibmvscsi_host_data to reset
  452. */
  453. static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
  454. {
  455. scsi_block_requests(hostdata->host);
  456. atomic_set(&hostdata->request_limit, 0);
  457. purge_requests(hostdata, DID_ERROR);
  458. hostdata->reset_crq = 1;
  459. wake_up(&hostdata->work_wait_q);
  460. }
  461. /**
  462. * ibmvscsi_timeout - Internal command timeout handler
  463. * @evt_struct: struct srp_event_struct that timed out
  464. *
  465. * Called when an internally generated command times out
  466. */
  467. static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
  468. {
  469. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  470. dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
  471. evt_struct->iu.srp.cmd.opcode);
  472. ibmvscsi_reset_host(hostdata);
  473. }
  474. /* ------------------------------------------------------------
  475. * Routines for sending and receiving SRPs
  476. */
  477. /**
  478. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  479. * @evt_struct: evt_struct to be sent
  480. * @hostdata: ibmvscsi_host_data of host
  481. * @timeout: timeout in seconds - 0 means do not time command
  482. *
  483. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  484. * Note that this routine assumes that host_lock is held for synchronization
  485. */
  486. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  487. struct ibmvscsi_host_data *hostdata,
  488. unsigned long timeout)
  489. {
  490. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  491. int request_status = 0;
  492. int rc;
  493. int srp_req = 0;
  494. /* If we have exhausted our request limit, just fail this request,
  495. * unless it is for a reset or abort.
  496. * Note that there are rare cases involving driver generated requests
  497. * (such as task management requests) that the mid layer may think we
  498. * can handle more requests (can_queue) when we actually can't
  499. */
  500. if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
  501. srp_req = 1;
  502. request_status =
  503. atomic_dec_if_positive(&hostdata->request_limit);
  504. /* If request limit was -1 when we started, it is now even
  505. * less than that
  506. */
  507. if (request_status < -1)
  508. goto send_error;
  509. /* Otherwise, we may have run out of requests. */
  510. /* If request limit was 0 when we started the adapter is in the
  511. * process of performing a login with the server adapter, or
  512. * we may have run out of requests.
  513. */
  514. else if (request_status == -1 &&
  515. evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  516. goto send_busy;
  517. /* Abort and reset calls should make it through.
  518. * Nothing except abort and reset should use the last two
  519. * slots unless we had two or less to begin with.
  520. */
  521. else if (request_status < 2 &&
  522. evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
  523. /* In the case that we have less than two requests
  524. * available, check the server limit as a combination
  525. * of the request limit and the number of requests
  526. * in-flight (the size of the send list). If the
  527. * server limit is greater than 2, return busy so
  528. * that the last two are reserved for reset and abort.
  529. */
  530. int server_limit = request_status;
  531. struct srp_event_struct *tmp_evt;
  532. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  533. server_limit++;
  534. }
  535. if (server_limit > 2)
  536. goto send_busy;
  537. }
  538. }
  539. /* Copy the IU into the transfer area */
  540. *evt_struct->xfer_iu = evt_struct->iu;
  541. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  542. /* Add this to the sent list. We need to do this
  543. * before we actually send
  544. * in case it comes back REALLY fast
  545. */
  546. list_add_tail(&evt_struct->list, &hostdata->sent);
  547. init_timer(&evt_struct->timer);
  548. if (timeout) {
  549. evt_struct->timer.data = (unsigned long) evt_struct;
  550. evt_struct->timer.expires = jiffies + (timeout * HZ);
  551. evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
  552. add_timer(&evt_struct->timer);
  553. }
  554. if ((rc =
  555. ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  556. list_del(&evt_struct->list);
  557. del_timer(&evt_struct->timer);
  558. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  559. * Firmware will send a CRQ with a transport event (0xFF) to
  560. * tell this client what has happened to the transport. This
  561. * will be handled in ibmvscsi_handle_crq()
  562. */
  563. if (rc == H_CLOSED) {
  564. dev_warn(hostdata->dev, "send warning. "
  565. "Receive queue closed, will retry.\n");
  566. goto send_busy;
  567. }
  568. dev_err(hostdata->dev, "send error %d\n", rc);
  569. if (srp_req)
  570. atomic_inc(&hostdata->request_limit);
  571. goto send_error;
  572. }
  573. return 0;
  574. send_busy:
  575. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  576. free_event_struct(&hostdata->pool, evt_struct);
  577. if (srp_req && request_status != -1)
  578. atomic_inc(&hostdata->request_limit);
  579. return SCSI_MLQUEUE_HOST_BUSY;
  580. send_error:
  581. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  582. if (evt_struct->cmnd != NULL) {
  583. evt_struct->cmnd->result = DID_ERROR << 16;
  584. evt_struct->cmnd_done(evt_struct->cmnd);
  585. } else if (evt_struct->done)
  586. evt_struct->done(evt_struct);
  587. free_event_struct(&hostdata->pool, evt_struct);
  588. return 0;
  589. }
  590. /**
  591. * handle_cmd_rsp: - Handle responses from commands
  592. * @evt_struct: srp_event_struct to be handled
  593. *
  594. * Used as a callback by when sending scsi cmds.
  595. * Gets called by ibmvscsi_handle_crq()
  596. */
  597. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  598. {
  599. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  600. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  601. if (unlikely(rsp->opcode != SRP_RSP)) {
  602. if (printk_ratelimit())
  603. dev_warn(evt_struct->hostdata->dev,
  604. "bad SRP RSP type %d\n", rsp->opcode);
  605. }
  606. if (cmnd) {
  607. cmnd->result |= rsp->status;
  608. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  609. memcpy(cmnd->sense_buffer,
  610. rsp->data,
  611. rsp->sense_data_len);
  612. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  613. evt_struct,
  614. evt_struct->hostdata->dev);
  615. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  616. scsi_set_resid(cmnd, rsp->data_out_res_cnt);
  617. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  618. scsi_set_resid(cmnd, rsp->data_in_res_cnt);
  619. }
  620. if (evt_struct->cmnd_done)
  621. evt_struct->cmnd_done(cmnd);
  622. }
  623. /**
  624. * lun_from_dev: - Returns the lun of the scsi device
  625. * @dev: struct scsi_device
  626. *
  627. */
  628. static inline u16 lun_from_dev(struct scsi_device *dev)
  629. {
  630. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  631. }
  632. /**
  633. * ibmvscsi_queue: - The queuecommand function of the scsi template
  634. * @cmd: struct scsi_cmnd to be executed
  635. * @done: Callback function to be called when cmd is completed
  636. */
  637. static int ibmvscsi_queuecommand_lck(struct scsi_cmnd *cmnd,
  638. void (*done) (struct scsi_cmnd *))
  639. {
  640. struct srp_cmd *srp_cmd;
  641. struct srp_event_struct *evt_struct;
  642. struct srp_indirect_buf *indirect;
  643. struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
  644. u16 lun = lun_from_dev(cmnd->device);
  645. u8 out_fmt, in_fmt;
  646. cmnd->result = (DID_OK << 16);
  647. evt_struct = get_event_struct(&hostdata->pool);
  648. if (!evt_struct)
  649. return SCSI_MLQUEUE_HOST_BUSY;
  650. /* Set up the actual SRP IU */
  651. srp_cmd = &evt_struct->iu.srp.cmd;
  652. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  653. srp_cmd->opcode = SRP_CMD;
  654. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
  655. srp_cmd->lun = ((u64) lun) << 48;
  656. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  657. if (!firmware_has_feature(FW_FEATURE_CMO))
  658. sdev_printk(KERN_ERR, cmnd->device,
  659. "couldn't convert cmd to srp_cmd\n");
  660. free_event_struct(&hostdata->pool, evt_struct);
  661. return SCSI_MLQUEUE_HOST_BUSY;
  662. }
  663. init_event_struct(evt_struct,
  664. handle_cmd_rsp,
  665. VIOSRP_SRP_FORMAT,
  666. cmnd->request->timeout/HZ);
  667. evt_struct->cmnd = cmnd;
  668. evt_struct->cmnd_done = done;
  669. /* Fix up dma address of the buffer itself */
  670. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  671. out_fmt = srp_cmd->buf_fmt >> 4;
  672. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  673. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  674. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  675. indirect->table_desc.va == 0) {
  676. indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
  677. offsetof(struct srp_cmd, add_data) +
  678. offsetof(struct srp_indirect_buf, desc_list);
  679. }
  680. return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
  681. }
  682. static DEF_SCSI_QCMD(ibmvscsi_queuecommand)
  683. /* ------------------------------------------------------------
  684. * Routines for driver initialization
  685. */
  686. /**
  687. * map_persist_bufs: - Pre-map persistent data for adapter logins
  688. * @hostdata: ibmvscsi_host_data of host
  689. *
  690. * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
  691. * Return 1 on error, 0 on success.
  692. */
  693. static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
  694. {
  695. hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
  696. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  697. if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
  698. dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
  699. return 1;
  700. }
  701. hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
  702. &hostdata->madapter_info,
  703. sizeof(hostdata->madapter_info),
  704. DMA_BIDIRECTIONAL);
  705. if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
  706. dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
  707. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  708. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  709. return 1;
  710. }
  711. return 0;
  712. }
  713. /**
  714. * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
  715. * @hostdata: ibmvscsi_host_data of host
  716. *
  717. * Unmap the capabilities and adapter info DMA buffers
  718. */
  719. static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
  720. {
  721. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  722. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  723. dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
  724. sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
  725. }
  726. /**
  727. * login_rsp: - Handle response to SRP login request
  728. * @evt_struct: srp_event_struct with the response
  729. *
  730. * Used as a "done" callback by when sending srp_login. Gets called
  731. * by ibmvscsi_handle_crq()
  732. */
  733. static void login_rsp(struct srp_event_struct *evt_struct)
  734. {
  735. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  736. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  737. case SRP_LOGIN_RSP: /* it worked! */
  738. break;
  739. case SRP_LOGIN_REJ: /* refused! */
  740. dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
  741. evt_struct->xfer_iu->srp.login_rej.reason);
  742. /* Login failed. */
  743. atomic_set(&hostdata->request_limit, -1);
  744. return;
  745. default:
  746. dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
  747. evt_struct->xfer_iu->srp.login_rsp.opcode);
  748. /* Login failed. */
  749. atomic_set(&hostdata->request_limit, -1);
  750. return;
  751. }
  752. dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
  753. hostdata->client_migrated = 0;
  754. /* Now we know what the real request-limit is.
  755. * This value is set rather than added to request_limit because
  756. * request_limit could have been set to -1 by this client.
  757. */
  758. atomic_set(&hostdata->request_limit,
  759. evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
  760. /* If we had any pending I/Os, kick them */
  761. scsi_unblock_requests(hostdata->host);
  762. }
  763. /**
  764. * send_srp_login: - Sends the srp login
  765. * @hostdata: ibmvscsi_host_data of host
  766. *
  767. * Returns zero if successful.
  768. */
  769. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  770. {
  771. int rc;
  772. unsigned long flags;
  773. struct srp_login_req *login;
  774. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  775. BUG_ON(!evt_struct);
  776. init_event_struct(evt_struct, login_rsp,
  777. VIOSRP_SRP_FORMAT, login_timeout);
  778. login = &evt_struct->iu.srp.login_req;
  779. memset(login, 0, sizeof(*login));
  780. login->opcode = SRP_LOGIN_REQ;
  781. login->req_it_iu_len = sizeof(union srp_iu);
  782. login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
  783. spin_lock_irqsave(hostdata->host->host_lock, flags);
  784. /* Start out with a request limit of 0, since this is negotiated in
  785. * the login request we are just sending and login requests always
  786. * get sent by the driver regardless of request_limit.
  787. */
  788. atomic_set(&hostdata->request_limit, 0);
  789. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
  790. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  791. dev_info(hostdata->dev, "sent SRP login\n");
  792. return rc;
  793. };
  794. /**
  795. * capabilities_rsp: - Handle response to MAD adapter capabilities request
  796. * @evt_struct: srp_event_struct with the response
  797. *
  798. * Used as a "done" callback by when sending adapter_info.
  799. */
  800. static void capabilities_rsp(struct srp_event_struct *evt_struct)
  801. {
  802. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  803. if (evt_struct->xfer_iu->mad.capabilities.common.status) {
  804. dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
  805. evt_struct->xfer_iu->mad.capabilities.common.status);
  806. } else {
  807. if (hostdata->caps.migration.common.server_support != SERVER_SUPPORTS_CAP)
  808. dev_info(hostdata->dev, "Partition migration not supported\n");
  809. if (client_reserve) {
  810. if (hostdata->caps.reserve.common.server_support ==
  811. SERVER_SUPPORTS_CAP)
  812. dev_info(hostdata->dev, "Client reserve enabled\n");
  813. else
  814. dev_info(hostdata->dev, "Client reserve not supported\n");
  815. }
  816. }
  817. send_srp_login(hostdata);
  818. }
  819. /**
  820. * send_mad_capabilities: - Sends the mad capabilities request
  821. * and stores the result so it can be retrieved with
  822. * @hostdata: ibmvscsi_host_data of host
  823. */
  824. static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
  825. {
  826. struct viosrp_capabilities *req;
  827. struct srp_event_struct *evt_struct;
  828. unsigned long flags;
  829. struct device_node *of_node = hostdata->dev->of_node;
  830. const char *location;
  831. evt_struct = get_event_struct(&hostdata->pool);
  832. BUG_ON(!evt_struct);
  833. init_event_struct(evt_struct, capabilities_rsp,
  834. VIOSRP_MAD_FORMAT, info_timeout);
  835. req = &evt_struct->iu.mad.capabilities;
  836. memset(req, 0, sizeof(*req));
  837. hostdata->caps.flags = CAP_LIST_SUPPORTED;
  838. if (hostdata->client_migrated)
  839. hostdata->caps.flags |= CLIENT_MIGRATED;
  840. strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
  841. sizeof(hostdata->caps.name));
  842. hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';
  843. location = of_get_property(of_node, "ibm,loc-code", NULL);
  844. location = location ? location : dev_name(hostdata->dev);
  845. strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
  846. hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';
  847. req->common.type = VIOSRP_CAPABILITIES_TYPE;
  848. req->buffer = hostdata->caps_addr;
  849. hostdata->caps.migration.common.cap_type = MIGRATION_CAPABILITIES;
  850. hostdata->caps.migration.common.length = sizeof(hostdata->caps.migration);
  851. hostdata->caps.migration.common.server_support = SERVER_SUPPORTS_CAP;
  852. hostdata->caps.migration.ecl = 1;
  853. if (client_reserve) {
  854. hostdata->caps.reserve.common.cap_type = RESERVATION_CAPABILITIES;
  855. hostdata->caps.reserve.common.length = sizeof(hostdata->caps.reserve);
  856. hostdata->caps.reserve.common.server_support = SERVER_SUPPORTS_CAP;
  857. hostdata->caps.reserve.type = CLIENT_RESERVE_SCSI_2;
  858. req->common.length = sizeof(hostdata->caps);
  859. } else
  860. req->common.length = sizeof(hostdata->caps) - sizeof(hostdata->caps.reserve);
  861. spin_lock_irqsave(hostdata->host->host_lock, flags);
  862. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  863. dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
  864. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  865. };
  866. /**
  867. * fast_fail_rsp: - Handle response to MAD enable fast fail
  868. * @evt_struct: srp_event_struct with the response
  869. *
  870. * Used as a "done" callback by when sending enable fast fail. Gets called
  871. * by ibmvscsi_handle_crq()
  872. */
  873. static void fast_fail_rsp(struct srp_event_struct *evt_struct)
  874. {
  875. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  876. u8 status = evt_struct->xfer_iu->mad.fast_fail.common.status;
  877. if (status == VIOSRP_MAD_NOT_SUPPORTED)
  878. dev_err(hostdata->dev, "fast_fail not supported in server\n");
  879. else if (status == VIOSRP_MAD_FAILED)
  880. dev_err(hostdata->dev, "fast_fail request failed\n");
  881. else if (status != VIOSRP_MAD_SUCCESS)
  882. dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
  883. send_mad_capabilities(hostdata);
  884. }
  885. /**
  886. * init_host - Start host initialization
  887. * @hostdata: ibmvscsi_host_data of host
  888. *
  889. * Returns zero if successful.
  890. */
  891. static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
  892. {
  893. int rc;
  894. unsigned long flags;
  895. struct viosrp_fast_fail *fast_fail_mad;
  896. struct srp_event_struct *evt_struct;
  897. if (!fast_fail) {
  898. send_mad_capabilities(hostdata);
  899. return 0;
  900. }
  901. evt_struct = get_event_struct(&hostdata->pool);
  902. BUG_ON(!evt_struct);
  903. init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
  904. fast_fail_mad = &evt_struct->iu.mad.fast_fail;
  905. memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
  906. fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
  907. fast_fail_mad->common.length = sizeof(*fast_fail_mad);
  908. spin_lock_irqsave(hostdata->host->host_lock, flags);
  909. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  910. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  911. return rc;
  912. }
  913. /**
  914. * adapter_info_rsp: - Handle response to MAD adapter info request
  915. * @evt_struct: srp_event_struct with the response
  916. *
  917. * Used as a "done" callback by when sending adapter_info. Gets called
  918. * by ibmvscsi_handle_crq()
  919. */
  920. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  921. {
  922. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  923. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  924. dev_err(hostdata->dev, "error %d getting adapter info\n",
  925. evt_struct->xfer_iu->mad.adapter_info.common.status);
  926. } else {
  927. dev_info(hostdata->dev, "host srp version: %s, "
  928. "host partition %s (%d), OS %d, max io %u\n",
  929. hostdata->madapter_info.srp_version,
  930. hostdata->madapter_info.partition_name,
  931. hostdata->madapter_info.partition_number,
  932. hostdata->madapter_info.os_type,
  933. hostdata->madapter_info.port_max_txu[0]);
  934. if (hostdata->madapter_info.port_max_txu[0])
  935. hostdata->host->max_sectors =
  936. hostdata->madapter_info.port_max_txu[0] >> 9;
  937. if (hostdata->madapter_info.os_type == 3 &&
  938. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  939. dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
  940. hostdata->madapter_info.srp_version);
  941. dev_err(hostdata->dev, "limiting scatterlists to %d\n",
  942. MAX_INDIRECT_BUFS);
  943. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  944. }
  945. if (hostdata->madapter_info.os_type == 3) {
  946. enable_fast_fail(hostdata);
  947. return;
  948. }
  949. }
  950. send_srp_login(hostdata);
  951. }
  952. /**
  953. * send_mad_adapter_info: - Sends the mad adapter info request
  954. * and stores the result so it can be retrieved with
  955. * sysfs. We COULD consider causing a failure if the
  956. * returned SRP version doesn't match ours.
  957. * @hostdata: ibmvscsi_host_data of host
  958. *
  959. * Returns zero if successful.
  960. */
  961. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  962. {
  963. struct viosrp_adapter_info *req;
  964. struct srp_event_struct *evt_struct;
  965. unsigned long flags;
  966. evt_struct = get_event_struct(&hostdata->pool);
  967. BUG_ON(!evt_struct);
  968. init_event_struct(evt_struct,
  969. adapter_info_rsp,
  970. VIOSRP_MAD_FORMAT,
  971. info_timeout);
  972. req = &evt_struct->iu.mad.adapter_info;
  973. memset(req, 0x00, sizeof(*req));
  974. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  975. req->common.length = sizeof(hostdata->madapter_info);
  976. req->buffer = hostdata->adapter_info_addr;
  977. spin_lock_irqsave(hostdata->host->host_lock, flags);
  978. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  979. dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
  980. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  981. };
  982. /**
  983. * init_adapter: Start virtual adapter initialization sequence
  984. *
  985. */
  986. static void init_adapter(struct ibmvscsi_host_data *hostdata)
  987. {
  988. send_mad_adapter_info(hostdata);
  989. }
  990. /**
  991. * sync_completion: Signal that a synchronous command has completed
  992. * Note that after returning from this call, the evt_struct is freed.
  993. * the caller waiting on this completion shouldn't touch the evt_struct
  994. * again.
  995. */
  996. static void sync_completion(struct srp_event_struct *evt_struct)
  997. {
  998. /* copy the response back */
  999. if (evt_struct->sync_srp)
  1000. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  1001. complete(&evt_struct->comp);
  1002. }
  1003. /**
  1004. * ibmvscsi_abort: Abort a command...from scsi host template
  1005. * send this over to the server and wait synchronously for the response
  1006. */
  1007. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  1008. {
  1009. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1010. struct srp_tsk_mgmt *tsk_mgmt;
  1011. struct srp_event_struct *evt;
  1012. struct srp_event_struct *tmp_evt, *found_evt;
  1013. union viosrp_iu srp_rsp;
  1014. int rsp_rc;
  1015. unsigned long flags;
  1016. u16 lun = lun_from_dev(cmd->device);
  1017. unsigned long wait_switch = 0;
  1018. /* First, find this command in our sent list so we can figure
  1019. * out the correct tag
  1020. */
  1021. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1022. wait_switch = jiffies + (init_timeout * HZ);
  1023. do {
  1024. found_evt = NULL;
  1025. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1026. if (tmp_evt->cmnd == cmd) {
  1027. found_evt = tmp_evt;
  1028. break;
  1029. }
  1030. }
  1031. if (!found_evt) {
  1032. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1033. return SUCCESS;
  1034. }
  1035. evt = get_event_struct(&hostdata->pool);
  1036. if (evt == NULL) {
  1037. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1038. sdev_printk(KERN_ERR, cmd->device,
  1039. "failed to allocate abort event\n");
  1040. return FAILED;
  1041. }
  1042. init_event_struct(evt,
  1043. sync_completion,
  1044. VIOSRP_SRP_FORMAT,
  1045. abort_timeout);
  1046. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1047. /* Set up an abort SRP command */
  1048. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1049. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1050. tsk_mgmt->lun = ((u64) lun) << 48;
  1051. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  1052. tsk_mgmt->task_tag = (u64) found_evt;
  1053. evt->sync_srp = &srp_rsp;
  1054. init_completion(&evt->comp);
  1055. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
  1056. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1057. break;
  1058. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1059. msleep(10);
  1060. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1061. } while (time_before(jiffies, wait_switch));
  1062. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1063. if (rsp_rc != 0) {
  1064. sdev_printk(KERN_ERR, cmd->device,
  1065. "failed to send abort() event. rc=%d\n", rsp_rc);
  1066. return FAILED;
  1067. }
  1068. sdev_printk(KERN_INFO, cmd->device,
  1069. "aborting command. lun 0x%llx, tag 0x%llx\n",
  1070. (((u64) lun) << 48), (u64) found_evt);
  1071. wait_for_completion(&evt->comp);
  1072. /* make sure we got a good response */
  1073. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1074. if (printk_ratelimit())
  1075. sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
  1076. srp_rsp.srp.rsp.opcode);
  1077. return FAILED;
  1078. }
  1079. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1080. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1081. else
  1082. rsp_rc = srp_rsp.srp.rsp.status;
  1083. if (rsp_rc) {
  1084. if (printk_ratelimit())
  1085. sdev_printk(KERN_WARNING, cmd->device,
  1086. "abort code %d for task tag 0x%llx\n",
  1087. rsp_rc, tsk_mgmt->task_tag);
  1088. return FAILED;
  1089. }
  1090. /* Because we dropped the spinlock above, it's possible
  1091. * The event is no longer in our list. Make sure it didn't
  1092. * complete while we were aborting
  1093. */
  1094. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1095. found_evt = NULL;
  1096. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1097. if (tmp_evt->cmnd == cmd) {
  1098. found_evt = tmp_evt;
  1099. break;
  1100. }
  1101. }
  1102. if (found_evt == NULL) {
  1103. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1104. sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
  1105. tsk_mgmt->task_tag);
  1106. return SUCCESS;
  1107. }
  1108. sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
  1109. tsk_mgmt->task_tag);
  1110. cmd->result = (DID_ABORT << 16);
  1111. list_del(&found_evt->list);
  1112. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  1113. found_evt->hostdata->dev);
  1114. free_event_struct(&found_evt->hostdata->pool, found_evt);
  1115. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1116. atomic_inc(&hostdata->request_limit);
  1117. return SUCCESS;
  1118. }
  1119. /**
  1120. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  1121. * template send this over to the server and wait synchronously for the
  1122. * response
  1123. */
  1124. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  1125. {
  1126. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1127. struct srp_tsk_mgmt *tsk_mgmt;
  1128. struct srp_event_struct *evt;
  1129. struct srp_event_struct *tmp_evt, *pos;
  1130. union viosrp_iu srp_rsp;
  1131. int rsp_rc;
  1132. unsigned long flags;
  1133. u16 lun = lun_from_dev(cmd->device);
  1134. unsigned long wait_switch = 0;
  1135. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1136. wait_switch = jiffies + (init_timeout * HZ);
  1137. do {
  1138. evt = get_event_struct(&hostdata->pool);
  1139. if (evt == NULL) {
  1140. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1141. sdev_printk(KERN_ERR, cmd->device,
  1142. "failed to allocate reset event\n");
  1143. return FAILED;
  1144. }
  1145. init_event_struct(evt,
  1146. sync_completion,
  1147. VIOSRP_SRP_FORMAT,
  1148. reset_timeout);
  1149. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1150. /* Set up a lun reset SRP command */
  1151. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1152. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1153. tsk_mgmt->lun = ((u64) lun) << 48;
  1154. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  1155. evt->sync_srp = &srp_rsp;
  1156. init_completion(&evt->comp);
  1157. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
  1158. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1159. break;
  1160. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1161. msleep(10);
  1162. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1163. } while (time_before(jiffies, wait_switch));
  1164. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1165. if (rsp_rc != 0) {
  1166. sdev_printk(KERN_ERR, cmd->device,
  1167. "failed to send reset event. rc=%d\n", rsp_rc);
  1168. return FAILED;
  1169. }
  1170. sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
  1171. (((u64) lun) << 48));
  1172. wait_for_completion(&evt->comp);
  1173. /* make sure we got a good response */
  1174. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1175. if (printk_ratelimit())
  1176. sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
  1177. srp_rsp.srp.rsp.opcode);
  1178. return FAILED;
  1179. }
  1180. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1181. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1182. else
  1183. rsp_rc = srp_rsp.srp.rsp.status;
  1184. if (rsp_rc) {
  1185. if (printk_ratelimit())
  1186. sdev_printk(KERN_WARNING, cmd->device,
  1187. "reset code %d for task tag 0x%llx\n",
  1188. rsp_rc, tsk_mgmt->task_tag);
  1189. return FAILED;
  1190. }
  1191. /* We need to find all commands for this LUN that have not yet been
  1192. * responded to, and fail them with DID_RESET
  1193. */
  1194. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1195. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  1196. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  1197. if (tmp_evt->cmnd)
  1198. tmp_evt->cmnd->result = (DID_RESET << 16);
  1199. list_del(&tmp_evt->list);
  1200. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  1201. tmp_evt->hostdata->dev);
  1202. free_event_struct(&tmp_evt->hostdata->pool,
  1203. tmp_evt);
  1204. atomic_inc(&hostdata->request_limit);
  1205. if (tmp_evt->cmnd_done)
  1206. tmp_evt->cmnd_done(tmp_evt->cmnd);
  1207. else if (tmp_evt->done)
  1208. tmp_evt->done(tmp_evt);
  1209. }
  1210. }
  1211. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1212. return SUCCESS;
  1213. }
  1214. /**
  1215. * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
  1216. * @cmd: struct scsi_cmnd having problems
  1217. */
  1218. static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1219. {
  1220. unsigned long wait_switch = 0;
  1221. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1222. dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
  1223. ibmvscsi_reset_host(hostdata);
  1224. for (wait_switch = jiffies + (init_timeout * HZ);
  1225. time_before(jiffies, wait_switch) &&
  1226. atomic_read(&hostdata->request_limit) < 2;) {
  1227. msleep(10);
  1228. }
  1229. if (atomic_read(&hostdata->request_limit) <= 0)
  1230. return FAILED;
  1231. return SUCCESS;
  1232. }
  1233. /**
  1234. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1235. * @crq: Command/Response queue
  1236. * @hostdata: ibmvscsi_host_data of host
  1237. *
  1238. */
  1239. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1240. struct ibmvscsi_host_data *hostdata)
  1241. {
  1242. long rc;
  1243. unsigned long flags;
  1244. struct srp_event_struct *evt_struct =
  1245. (struct srp_event_struct *)crq->IU_data_ptr;
  1246. switch (crq->valid) {
  1247. case 0xC0: /* initialization */
  1248. switch (crq->format) {
  1249. case 0x01: /* Initialization message */
  1250. dev_info(hostdata->dev, "partner initialized\n");
  1251. /* Send back a response */
  1252. if ((rc = ibmvscsi_ops->send_crq(hostdata,
  1253. 0xC002000000000000LL, 0)) == 0) {
  1254. /* Now login */
  1255. init_adapter(hostdata);
  1256. } else {
  1257. dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1258. }
  1259. break;
  1260. case 0x02: /* Initialization response */
  1261. dev_info(hostdata->dev, "partner initialization complete\n");
  1262. /* Now login */
  1263. init_adapter(hostdata);
  1264. break;
  1265. default:
  1266. dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
  1267. }
  1268. return;
  1269. case 0xFF: /* Hypervisor telling us the connection is closed */
  1270. scsi_block_requests(hostdata->host);
  1271. atomic_set(&hostdata->request_limit, 0);
  1272. if (crq->format == 0x06) {
  1273. /* We need to re-setup the interpartition connection */
  1274. dev_info(hostdata->dev, "Re-enabling adapter!\n");
  1275. hostdata->client_migrated = 1;
  1276. hostdata->reenable_crq = 1;
  1277. purge_requests(hostdata, DID_REQUEUE);
  1278. wake_up(&hostdata->work_wait_q);
  1279. } else {
  1280. dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
  1281. crq->format);
  1282. ibmvscsi_reset_host(hostdata);
  1283. }
  1284. return;
  1285. case 0x80: /* real payload */
  1286. break;
  1287. default:
  1288. dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
  1289. crq->valid);
  1290. return;
  1291. }
  1292. /* The only kind of payload CRQs we should get are responses to
  1293. * things we send. Make sure this response is to something we
  1294. * actually sent
  1295. */
  1296. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1297. dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
  1298. (void *)crq->IU_data_ptr);
  1299. return;
  1300. }
  1301. if (atomic_read(&evt_struct->free)) {
  1302. dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
  1303. (void *)crq->IU_data_ptr);
  1304. return;
  1305. }
  1306. if (crq->format == VIOSRP_SRP_FORMAT)
  1307. atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
  1308. &hostdata->request_limit);
  1309. del_timer(&evt_struct->timer);
  1310. if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
  1311. evt_struct->cmnd->result = DID_ERROR << 16;
  1312. if (evt_struct->done)
  1313. evt_struct->done(evt_struct);
  1314. else
  1315. dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
  1316. /*
  1317. * Lock the host_lock before messing with these structures, since we
  1318. * are running in a task context
  1319. */
  1320. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1321. list_del(&evt_struct->list);
  1322. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1323. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1324. }
  1325. /**
  1326. * ibmvscsi_get_host_config: Send the command to the server to get host
  1327. * configuration data. The data is opaque to us.
  1328. */
  1329. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1330. unsigned char *buffer, int length)
  1331. {
  1332. struct viosrp_host_config *host_config;
  1333. struct srp_event_struct *evt_struct;
  1334. unsigned long flags;
  1335. dma_addr_t addr;
  1336. int rc;
  1337. evt_struct = get_event_struct(&hostdata->pool);
  1338. if (!evt_struct) {
  1339. dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
  1340. return -1;
  1341. }
  1342. init_event_struct(evt_struct,
  1343. sync_completion,
  1344. VIOSRP_MAD_FORMAT,
  1345. info_timeout);
  1346. host_config = &evt_struct->iu.mad.host_config;
  1347. /* The transport length field is only 16-bit */
  1348. length = min(0xffff, length);
  1349. /* Set up a lun reset SRP command */
  1350. memset(host_config, 0x00, sizeof(*host_config));
  1351. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1352. host_config->common.length = length;
  1353. host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
  1354. length,
  1355. DMA_BIDIRECTIONAL);
  1356. if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
  1357. if (!firmware_has_feature(FW_FEATURE_CMO))
  1358. dev_err(hostdata->dev,
  1359. "dma_mapping error getting host config\n");
  1360. free_event_struct(&hostdata->pool, evt_struct);
  1361. return -1;
  1362. }
  1363. init_completion(&evt_struct->comp);
  1364. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1365. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  1366. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1367. if (rc == 0)
  1368. wait_for_completion(&evt_struct->comp);
  1369. dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
  1370. return rc;
  1371. }
  1372. /**
  1373. * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
  1374. * @sdev: struct scsi_device device to configure
  1375. *
  1376. * Enable allow_restart for a device if it is a disk. Adjust the
  1377. * queue_depth here also as is required by the documentation for
  1378. * struct scsi_host_template.
  1379. */
  1380. static int ibmvscsi_slave_configure(struct scsi_device *sdev)
  1381. {
  1382. struct Scsi_Host *shost = sdev->host;
  1383. unsigned long lock_flags = 0;
  1384. spin_lock_irqsave(shost->host_lock, lock_flags);
  1385. if (sdev->type == TYPE_DISK) {
  1386. sdev->allow_restart = 1;
  1387. blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
  1388. }
  1389. scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
  1390. spin_unlock_irqrestore(shost->host_lock, lock_flags);
  1391. return 0;
  1392. }
  1393. /**
  1394. * ibmvscsi_change_queue_depth - Change the device's queue depth
  1395. * @sdev: scsi device struct
  1396. * @qdepth: depth to set
  1397. * @reason: calling context
  1398. *
  1399. * Return value:
  1400. * actual depth set
  1401. **/
  1402. static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth,
  1403. int reason)
  1404. {
  1405. if (reason != SCSI_QDEPTH_DEFAULT)
  1406. return -EOPNOTSUPP;
  1407. if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
  1408. qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
  1409. scsi_adjust_queue_depth(sdev, 0, qdepth);
  1410. return sdev->queue_depth;
  1411. }
  1412. /* ------------------------------------------------------------
  1413. * sysfs attributes
  1414. */
  1415. static ssize_t show_host_vhost_loc(struct device *dev,
  1416. struct device_attribute *attr, char *buf)
  1417. {
  1418. struct Scsi_Host *shost = class_to_shost(dev);
  1419. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1420. int len;
  1421. len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
  1422. hostdata->caps.loc);
  1423. return len;
  1424. }
  1425. static struct device_attribute ibmvscsi_host_vhost_loc = {
  1426. .attr = {
  1427. .name = "vhost_loc",
  1428. .mode = S_IRUGO,
  1429. },
  1430. .show = show_host_vhost_loc,
  1431. };
  1432. static ssize_t show_host_vhost_name(struct device *dev,
  1433. struct device_attribute *attr, char *buf)
  1434. {
  1435. struct Scsi_Host *shost = class_to_shost(dev);
  1436. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1437. int len;
  1438. len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
  1439. hostdata->caps.name);
  1440. return len;
  1441. }
  1442. static struct device_attribute ibmvscsi_host_vhost_name = {
  1443. .attr = {
  1444. .name = "vhost_name",
  1445. .mode = S_IRUGO,
  1446. },
  1447. .show = show_host_vhost_name,
  1448. };
  1449. static ssize_t show_host_srp_version(struct device *dev,
  1450. struct device_attribute *attr, char *buf)
  1451. {
  1452. struct Scsi_Host *shost = class_to_shost(dev);
  1453. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1454. int len;
  1455. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1456. hostdata->madapter_info.srp_version);
  1457. return len;
  1458. }
  1459. static struct device_attribute ibmvscsi_host_srp_version = {
  1460. .attr = {
  1461. .name = "srp_version",
  1462. .mode = S_IRUGO,
  1463. },
  1464. .show = show_host_srp_version,
  1465. };
  1466. static ssize_t show_host_partition_name(struct device *dev,
  1467. struct device_attribute *attr,
  1468. char *buf)
  1469. {
  1470. struct Scsi_Host *shost = class_to_shost(dev);
  1471. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1472. int len;
  1473. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1474. hostdata->madapter_info.partition_name);
  1475. return len;
  1476. }
  1477. static struct device_attribute ibmvscsi_host_partition_name = {
  1478. .attr = {
  1479. .name = "partition_name",
  1480. .mode = S_IRUGO,
  1481. },
  1482. .show = show_host_partition_name,
  1483. };
  1484. static ssize_t show_host_partition_number(struct device *dev,
  1485. struct device_attribute *attr,
  1486. char *buf)
  1487. {
  1488. struct Scsi_Host *shost = class_to_shost(dev);
  1489. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1490. int len;
  1491. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1492. hostdata->madapter_info.partition_number);
  1493. return len;
  1494. }
  1495. static struct device_attribute ibmvscsi_host_partition_number = {
  1496. .attr = {
  1497. .name = "partition_number",
  1498. .mode = S_IRUGO,
  1499. },
  1500. .show = show_host_partition_number,
  1501. };
  1502. static ssize_t show_host_mad_version(struct device *dev,
  1503. struct device_attribute *attr, char *buf)
  1504. {
  1505. struct Scsi_Host *shost = class_to_shost(dev);
  1506. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1507. int len;
  1508. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1509. hostdata->madapter_info.mad_version);
  1510. return len;
  1511. }
  1512. static struct device_attribute ibmvscsi_host_mad_version = {
  1513. .attr = {
  1514. .name = "mad_version",
  1515. .mode = S_IRUGO,
  1516. },
  1517. .show = show_host_mad_version,
  1518. };
  1519. static ssize_t show_host_os_type(struct device *dev,
  1520. struct device_attribute *attr, char *buf)
  1521. {
  1522. struct Scsi_Host *shost = class_to_shost(dev);
  1523. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1524. int len;
  1525. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1526. return len;
  1527. }
  1528. static struct device_attribute ibmvscsi_host_os_type = {
  1529. .attr = {
  1530. .name = "os_type",
  1531. .mode = S_IRUGO,
  1532. },
  1533. .show = show_host_os_type,
  1534. };
  1535. static ssize_t show_host_config(struct device *dev,
  1536. struct device_attribute *attr, char *buf)
  1537. {
  1538. struct Scsi_Host *shost = class_to_shost(dev);
  1539. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1540. /* returns null-terminated host config data */
  1541. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1542. return strlen(buf);
  1543. else
  1544. return 0;
  1545. }
  1546. static struct device_attribute ibmvscsi_host_config = {
  1547. .attr = {
  1548. .name = "config",
  1549. .mode = S_IRUGO,
  1550. },
  1551. .show = show_host_config,
  1552. };
  1553. static struct device_attribute *ibmvscsi_attrs[] = {
  1554. &ibmvscsi_host_vhost_loc,
  1555. &ibmvscsi_host_vhost_name,
  1556. &ibmvscsi_host_srp_version,
  1557. &ibmvscsi_host_partition_name,
  1558. &ibmvscsi_host_partition_number,
  1559. &ibmvscsi_host_mad_version,
  1560. &ibmvscsi_host_os_type,
  1561. &ibmvscsi_host_config,
  1562. NULL
  1563. };
  1564. /* ------------------------------------------------------------
  1565. * SCSI driver registration
  1566. */
  1567. static struct scsi_host_template driver_template = {
  1568. .module = THIS_MODULE,
  1569. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1570. .proc_name = "ibmvscsi",
  1571. .queuecommand = ibmvscsi_queuecommand,
  1572. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1573. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1574. .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
  1575. .slave_configure = ibmvscsi_slave_configure,
  1576. .change_queue_depth = ibmvscsi_change_queue_depth,
  1577. .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
  1578. .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
  1579. .this_id = -1,
  1580. .sg_tablesize = SG_ALL,
  1581. .use_clustering = ENABLE_CLUSTERING,
  1582. .shost_attrs = ibmvscsi_attrs,
  1583. };
  1584. /**
  1585. * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
  1586. *
  1587. * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
  1588. *
  1589. * Return value:
  1590. * Number of bytes of IO data the driver will need to perform well.
  1591. */
  1592. static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
  1593. {
  1594. /* iu_storage data allocated in initialize_event_pool */
  1595. unsigned long desired_io = max_events * sizeof(union viosrp_iu);
  1596. /* add io space for sg data */
  1597. desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
  1598. IBMVSCSI_CMDS_PER_LUN_DEFAULT);
  1599. return desired_io;
  1600. }
  1601. static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
  1602. {
  1603. int rc;
  1604. char *action = "reset";
  1605. if (hostdata->reset_crq) {
  1606. smp_rmb();
  1607. hostdata->reset_crq = 0;
  1608. rc = ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata);
  1609. if (!rc)
  1610. rc = ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0);
  1611. vio_enable_interrupts(to_vio_dev(hostdata->dev));
  1612. } else if (hostdata->reenable_crq) {
  1613. smp_rmb();
  1614. action = "enable";
  1615. rc = ibmvscsi_ops->reenable_crq_queue(&hostdata->queue, hostdata);
  1616. hostdata->reenable_crq = 0;
  1617. if (!rc)
  1618. rc = ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0);
  1619. } else
  1620. return;
  1621. if (rc) {
  1622. atomic_set(&hostdata->request_limit, -1);
  1623. dev_err(hostdata->dev, "error after %s\n", action);
  1624. }
  1625. scsi_unblock_requests(hostdata->host);
  1626. }
  1627. static int ibmvscsi_work_to_do(struct ibmvscsi_host_data *hostdata)
  1628. {
  1629. if (kthread_should_stop())
  1630. return 1;
  1631. else if (hostdata->reset_crq) {
  1632. smp_rmb();
  1633. return 1;
  1634. } else if (hostdata->reenable_crq) {
  1635. smp_rmb();
  1636. return 1;
  1637. }
  1638. return 0;
  1639. }
  1640. static int ibmvscsi_work(void *data)
  1641. {
  1642. struct ibmvscsi_host_data *hostdata = data;
  1643. int rc;
  1644. set_user_nice(current, -20);
  1645. while (1) {
  1646. rc = wait_event_interruptible(hostdata->work_wait_q,
  1647. ibmvscsi_work_to_do(hostdata));
  1648. BUG_ON(rc);
  1649. if (kthread_should_stop())
  1650. break;
  1651. ibmvscsi_do_work(hostdata);
  1652. }
  1653. return 0;
  1654. }
  1655. /**
  1656. * Called by bus code for each adapter
  1657. */
  1658. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1659. {
  1660. struct ibmvscsi_host_data *hostdata;
  1661. struct Scsi_Host *host;
  1662. struct device *dev = &vdev->dev;
  1663. struct srp_rport_identifiers ids;
  1664. struct srp_rport *rport;
  1665. unsigned long wait_switch = 0;
  1666. int rc;
  1667. dev_set_drvdata(&vdev->dev, NULL);
  1668. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1669. if (!host) {
  1670. dev_err(&vdev->dev, "couldn't allocate host data\n");
  1671. goto scsi_host_alloc_failed;
  1672. }
  1673. host->transportt = ibmvscsi_transport_template;
  1674. hostdata = shost_priv(host);
  1675. memset(hostdata, 0x00, sizeof(*hostdata));
  1676. INIT_LIST_HEAD(&hostdata->sent);
  1677. init_waitqueue_head(&hostdata->work_wait_q);
  1678. hostdata->host = host;
  1679. hostdata->dev = dev;
  1680. atomic_set(&hostdata->request_limit, -1);
  1681. hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
  1682. if (map_persist_bufs(hostdata)) {
  1683. dev_err(&vdev->dev, "couldn't map persistent buffers\n");
  1684. goto persist_bufs_failed;
  1685. }
  1686. hostdata->work_thread = kthread_run(ibmvscsi_work, hostdata, "%s_%d",
  1687. "ibmvscsi", host->host_no);
  1688. if (IS_ERR(hostdata->work_thread)) {
  1689. dev_err(&vdev->dev, "couldn't initialize kthread. rc=%ld\n",
  1690. PTR_ERR(hostdata->work_thread));
  1691. goto init_crq_failed;
  1692. }
  1693. rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
  1694. if (rc != 0 && rc != H_RESOURCE) {
  1695. dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
  1696. goto kill_kthread;
  1697. }
  1698. if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
  1699. dev_err(&vdev->dev, "couldn't initialize event pool\n");
  1700. goto init_pool_failed;
  1701. }
  1702. host->max_lun = 8;
  1703. host->max_id = max_id;
  1704. host->max_channel = max_channel;
  1705. host->max_cmd_len = 16;
  1706. if (scsi_add_host(hostdata->host, hostdata->dev))
  1707. goto add_host_failed;
  1708. /* we don't have a proper target_port_id so let's use the fake one */
  1709. memcpy(ids.port_id, hostdata->madapter_info.partition_name,
  1710. sizeof(ids.port_id));
  1711. ids.roles = SRP_RPORT_ROLE_TARGET;
  1712. rport = srp_rport_add(host, &ids);
  1713. if (IS_ERR(rport))
  1714. goto add_srp_port_failed;
  1715. /* Try to send an initialization message. Note that this is allowed
  1716. * to fail if the other end is not acive. In that case we don't
  1717. * want to scan
  1718. */
  1719. if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
  1720. || rc == H_RESOURCE) {
  1721. /*
  1722. * Wait around max init_timeout secs for the adapter to finish
  1723. * initializing. When we are done initializing, we will have a
  1724. * valid request_limit. We don't want Linux scanning before
  1725. * we are ready.
  1726. */
  1727. for (wait_switch = jiffies + (init_timeout * HZ);
  1728. time_before(jiffies, wait_switch) &&
  1729. atomic_read(&hostdata->request_limit) < 2;) {
  1730. msleep(10);
  1731. }
  1732. /* if we now have a valid request_limit, initiate a scan */
  1733. if (atomic_read(&hostdata->request_limit) > 0)
  1734. scsi_scan_host(host);
  1735. }
  1736. dev_set_drvdata(&vdev->dev, hostdata);
  1737. return 0;
  1738. add_srp_port_failed:
  1739. scsi_remove_host(hostdata->host);
  1740. add_host_failed:
  1741. release_event_pool(&hostdata->pool, hostdata);
  1742. init_pool_failed:
  1743. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
  1744. kill_kthread:
  1745. kthread_stop(hostdata->work_thread);
  1746. init_crq_failed:
  1747. unmap_persist_bufs(hostdata);
  1748. persist_bufs_failed:
  1749. scsi_host_put(host);
  1750. scsi_host_alloc_failed:
  1751. return -1;
  1752. }
  1753. static int ibmvscsi_remove(struct vio_dev *vdev)
  1754. {
  1755. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
  1756. unmap_persist_bufs(hostdata);
  1757. release_event_pool(&hostdata->pool, hostdata);
  1758. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
  1759. max_events);
  1760. kthread_stop(hostdata->work_thread);
  1761. srp_remove_host(hostdata->host);
  1762. scsi_remove_host(hostdata->host);
  1763. scsi_host_put(hostdata->host);
  1764. return 0;
  1765. }
  1766. /**
  1767. * ibmvscsi_resume: Resume from suspend
  1768. * @dev: device struct
  1769. *
  1770. * We may have lost an interrupt across suspend/resume, so kick the
  1771. * interrupt handler
  1772. */
  1773. static int ibmvscsi_resume(struct device *dev)
  1774. {
  1775. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(dev);
  1776. return ibmvscsi_ops->resume(hostdata);
  1777. }
  1778. /**
  1779. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1780. * support.
  1781. */
  1782. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1783. {"vscsi", "IBM,v-scsi"},
  1784. { "", "" }
  1785. };
  1786. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1787. static struct dev_pm_ops ibmvscsi_pm_ops = {
  1788. .resume = ibmvscsi_resume
  1789. };
  1790. static struct vio_driver ibmvscsi_driver = {
  1791. .id_table = ibmvscsi_device_table,
  1792. .probe = ibmvscsi_probe,
  1793. .remove = ibmvscsi_remove,
  1794. .get_desired_dma = ibmvscsi_get_desired_dma,
  1795. .name = "ibmvscsi",
  1796. .pm = &ibmvscsi_pm_ops,
  1797. };
  1798. static struct srp_function_template ibmvscsi_transport_functions = {
  1799. };
  1800. int __init ibmvscsi_module_init(void)
  1801. {
  1802. int ret;
  1803. /* Ensure we have two requests to do error recovery */
  1804. driver_template.can_queue = max_requests;
  1805. max_events = max_requests + 2;
  1806. if (firmware_has_feature(FW_FEATURE_VIO))
  1807. ibmvscsi_ops = &rpavscsi_ops;
  1808. else
  1809. return -ENODEV;
  1810. ibmvscsi_transport_template =
  1811. srp_attach_transport(&ibmvscsi_transport_functions);
  1812. if (!ibmvscsi_transport_template)
  1813. return -ENOMEM;
  1814. ret = vio_register_driver(&ibmvscsi_driver);
  1815. if (ret)
  1816. srp_release_transport(ibmvscsi_transport_template);
  1817. return ret;
  1818. }
  1819. void __exit ibmvscsi_module_exit(void)
  1820. {
  1821. vio_unregister_driver(&ibmvscsi_driver);
  1822. srp_release_transport(ibmvscsi_transport_template);
  1823. }
  1824. module_init(ibmvscsi_module_init);
  1825. module_exit(ibmvscsi_module_exit);