ibmvscsi.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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-robbin 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. * Note that some of the underlying infrastructure is different between
  59. * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
  60. * the older iSeries hypervisor models. To support both, some low level
  61. * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
  62. * The Makefile should pick one, not two, not zero, of these.
  63. *
  64. * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
  65. * interfaces. It would be really nice to abstract this above an RDMA
  66. * layer.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/moduleparam.h>
  70. #include <linux/dma-mapping.h>
  71. #include <linux/delay.h>
  72. #include <asm/vio.h>
  73. #include <scsi/scsi.h>
  74. #include <scsi/scsi_cmnd.h>
  75. #include <scsi/scsi_host.h>
  76. #include <scsi/scsi_device.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 = 5;
  86. static int max_requests = 50;
  87. #define IBMVSCSI_VERSION "1.5.5"
  88. MODULE_DESCRIPTION("IBM Virtual SCSI");
  89. MODULE_AUTHOR("Dave Boutcher");
  90. MODULE_LICENSE("GPL");
  91. MODULE_VERSION(IBMVSCSI_VERSION);
  92. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  93. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  94. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  95. MODULE_PARM_DESC(max_channel, "Largest channel value");
  96. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  97. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  98. module_param_named(max_requests, max_requests, int, S_IRUGO | S_IWUSR);
  99. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  100. /* ------------------------------------------------------------
  101. * Routines for the event pool and event structs
  102. */
  103. /**
  104. * initialize_event_pool: - Allocates and initializes the event pool for a host
  105. * @pool: event_pool to be initialized
  106. * @size: Number of events in pool
  107. * @hostdata: ibmvscsi_host_data who owns the event pool
  108. *
  109. * Returns zero on success.
  110. */
  111. static int initialize_event_pool(struct event_pool *pool,
  112. int size, struct ibmvscsi_host_data *hostdata)
  113. {
  114. int i;
  115. pool->size = size;
  116. pool->next = 0;
  117. pool->events = kmalloc(pool->size * sizeof(*pool->events), GFP_KERNEL);
  118. if (!pool->events)
  119. return -ENOMEM;
  120. memset(pool->events, 0x00, pool->size * sizeof(*pool->events));
  121. pool->iu_storage =
  122. dma_alloc_coherent(hostdata->dev,
  123. pool->size * sizeof(*pool->iu_storage),
  124. &pool->iu_token, 0);
  125. if (!pool->iu_storage) {
  126. kfree(pool->events);
  127. return -ENOMEM;
  128. }
  129. for (i = 0; i < pool->size; ++i) {
  130. struct srp_event_struct *evt = &pool->events[i];
  131. memset(&evt->crq, 0x00, sizeof(evt->crq));
  132. atomic_set(&evt->free, 1);
  133. evt->crq.valid = 0x80;
  134. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  135. evt->crq.IU_data_ptr = pool->iu_token +
  136. sizeof(*evt->xfer_iu) * i;
  137. evt->xfer_iu = pool->iu_storage + i;
  138. evt->hostdata = hostdata;
  139. }
  140. return 0;
  141. }
  142. /**
  143. * release_event_pool: - Frees memory of an event pool of a host
  144. * @pool: event_pool to be released
  145. * @hostdata: ibmvscsi_host_data who owns the even pool
  146. *
  147. * Returns zero on success.
  148. */
  149. static void release_event_pool(struct event_pool *pool,
  150. struct ibmvscsi_host_data *hostdata)
  151. {
  152. int i, in_use = 0;
  153. for (i = 0; i < pool->size; ++i)
  154. if (atomic_read(&pool->events[i].free) != 1)
  155. ++in_use;
  156. if (in_use)
  157. printk(KERN_WARNING
  158. "ibmvscsi: releasing event pool with %d "
  159. "events still in use?\n", in_use);
  160. kfree(pool->events);
  161. dma_free_coherent(hostdata->dev,
  162. pool->size * sizeof(*pool->iu_storage),
  163. pool->iu_storage, pool->iu_token);
  164. }
  165. /**
  166. * valid_event_struct: - Determines if event is valid.
  167. * @pool: event_pool that contains the event
  168. * @evt: srp_event_struct to be checked for validity
  169. *
  170. * Returns zero if event is invalid, one otherwise.
  171. */
  172. static int valid_event_struct(struct event_pool *pool,
  173. struct srp_event_struct *evt)
  174. {
  175. int index = evt - pool->events;
  176. if (index < 0 || index >= pool->size) /* outside of bounds */
  177. return 0;
  178. if (evt != pool->events + index) /* unaligned */
  179. return 0;
  180. return 1;
  181. }
  182. /**
  183. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  184. * @pool: event_pool that contains the event
  185. * @evt: srp_event_struct to be modified
  186. *
  187. */
  188. static void free_event_struct(struct event_pool *pool,
  189. struct srp_event_struct *evt)
  190. {
  191. if (!valid_event_struct(pool, evt)) {
  192. printk(KERN_ERR
  193. "ibmvscsi: Freeing invalid event_struct %p "
  194. "(not in pool %p)\n", evt, pool->events);
  195. return;
  196. }
  197. if (atomic_inc_return(&evt->free) != 1) {
  198. printk(KERN_ERR
  199. "ibmvscsi: Freeing event_struct %p "
  200. "which is not in use!\n", evt);
  201. return;
  202. }
  203. }
  204. /**
  205. * get_evt_struct: - Gets the next free event in pool
  206. * @pool: event_pool that contains the events to be searched
  207. *
  208. * Returns the next event in "free" state, and NULL if none are free.
  209. * Note that no synchronization is done here, we assume the host_lock
  210. * will syncrhonze things.
  211. */
  212. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  213. {
  214. int i;
  215. int poolsize = pool->size;
  216. int offset = pool->next;
  217. for (i = 0; i < poolsize; i++) {
  218. offset = (offset + 1) % poolsize;
  219. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  220. pool->next = offset;
  221. return &pool->events[offset];
  222. }
  223. }
  224. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  225. return NULL;
  226. }
  227. /**
  228. * init_event_struct: Initialize fields in an event struct that are always
  229. * required.
  230. * @evt: The event
  231. * @done: Routine to call when the event is responded to
  232. * @format: SRP or MAD format
  233. * @timeout: timeout value set in the CRQ
  234. */
  235. static void init_event_struct(struct srp_event_struct *evt_struct,
  236. void (*done) (struct srp_event_struct *),
  237. u8 format,
  238. int timeout)
  239. {
  240. evt_struct->cmnd = NULL;
  241. evt_struct->cmnd_done = NULL;
  242. evt_struct->sync_srp = NULL;
  243. evt_struct->crq.format = format;
  244. evt_struct->crq.timeout = timeout;
  245. evt_struct->done = done;
  246. }
  247. /* ------------------------------------------------------------
  248. * Routines for receiving SCSI responses from the hosting partition
  249. */
  250. /**
  251. * set_srp_direction: Set the fields in the srp related to data
  252. * direction and number of buffers based on the direction in
  253. * the scsi_cmnd and the number of buffers
  254. */
  255. static void set_srp_direction(struct scsi_cmnd *cmd,
  256. struct srp_cmd *srp_cmd,
  257. int numbuf)
  258. {
  259. if (numbuf == 0)
  260. return;
  261. if (numbuf == 1) {
  262. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  263. srp_cmd->data_out_format = SRP_DIRECT_BUFFER;
  264. else
  265. srp_cmd->data_in_format = SRP_DIRECT_BUFFER;
  266. } else {
  267. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  268. srp_cmd->data_out_format = SRP_INDIRECT_BUFFER;
  269. srp_cmd->data_out_count = numbuf;
  270. } else {
  271. srp_cmd->data_in_format = SRP_INDIRECT_BUFFER;
  272. srp_cmd->data_in_count = numbuf;
  273. }
  274. }
  275. }
  276. /**
  277. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  278. * @cmd: srp_cmd whose additional_data member will be unmapped
  279. * @dev: device for which the memory is mapped
  280. *
  281. */
  282. static void unmap_cmd_data(struct srp_cmd *cmd, struct device *dev)
  283. {
  284. int i;
  285. if ((cmd->data_out_format == SRP_NO_BUFFER) &&
  286. (cmd->data_in_format == SRP_NO_BUFFER))
  287. return;
  288. else if ((cmd->data_out_format == SRP_DIRECT_BUFFER) ||
  289. (cmd->data_in_format == SRP_DIRECT_BUFFER)) {
  290. struct memory_descriptor *data =
  291. (struct memory_descriptor *)cmd->additional_data;
  292. dma_unmap_single(dev, data->virtual_address, data->length,
  293. DMA_BIDIRECTIONAL);
  294. } else {
  295. struct indirect_descriptor *indirect =
  296. (struct indirect_descriptor *)cmd->additional_data;
  297. int num_mapped = indirect->head.length /
  298. sizeof(indirect->list[0]);
  299. for (i = 0; i < num_mapped; ++i) {
  300. struct memory_descriptor *data = &indirect->list[i];
  301. dma_unmap_single(dev,
  302. data->virtual_address,
  303. data->length, DMA_BIDIRECTIONAL);
  304. }
  305. }
  306. }
  307. /**
  308. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  309. * @cmd: Scsi_Cmnd with the scatterlist
  310. * @srp_cmd: srp_cmd that contains the memory descriptor
  311. * @dev: device for which to map dma memory
  312. *
  313. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  314. * Returns 1 on success.
  315. */
  316. static int map_sg_data(struct scsi_cmnd *cmd,
  317. struct srp_cmd *srp_cmd, struct device *dev)
  318. {
  319. int i, sg_mapped;
  320. u64 total_length = 0;
  321. struct scatterlist *sg = cmd->request_buffer;
  322. struct memory_descriptor *data =
  323. (struct memory_descriptor *)srp_cmd->additional_data;
  324. struct indirect_descriptor *indirect =
  325. (struct indirect_descriptor *)data;
  326. sg_mapped = dma_map_sg(dev, sg, cmd->use_sg, DMA_BIDIRECTIONAL);
  327. if (sg_mapped == 0)
  328. return 0;
  329. set_srp_direction(cmd, srp_cmd, sg_mapped);
  330. /* special case; we can use a single direct descriptor */
  331. if (sg_mapped == 1) {
  332. data->virtual_address = sg_dma_address(&sg[0]);
  333. data->length = sg_dma_len(&sg[0]);
  334. data->memory_handle = 0;
  335. return 1;
  336. }
  337. if (sg_mapped > MAX_INDIRECT_BUFS) {
  338. printk(KERN_ERR
  339. "ibmvscsi: More than %d mapped sg entries, got %d\n",
  340. MAX_INDIRECT_BUFS, sg_mapped);
  341. return 0;
  342. }
  343. indirect->head.virtual_address = 0;
  344. indirect->head.length = sg_mapped * sizeof(indirect->list[0]);
  345. indirect->head.memory_handle = 0;
  346. for (i = 0; i < sg_mapped; ++i) {
  347. struct memory_descriptor *descr = &indirect->list[i];
  348. struct scatterlist *sg_entry = &sg[i];
  349. descr->virtual_address = sg_dma_address(sg_entry);
  350. descr->length = sg_dma_len(sg_entry);
  351. descr->memory_handle = 0;
  352. total_length += sg_dma_len(sg_entry);
  353. }
  354. indirect->total_length = total_length;
  355. return 1;
  356. }
  357. /**
  358. * map_single_data: - Maps memory and initializes memory decriptor fields
  359. * @cmd: struct scsi_cmnd with the memory to be mapped
  360. * @srp_cmd: srp_cmd that contains the memory descriptor
  361. * @dev: device for which to map dma memory
  362. *
  363. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  364. * Returns 1 on success.
  365. */
  366. static int map_single_data(struct scsi_cmnd *cmd,
  367. struct srp_cmd *srp_cmd, struct device *dev)
  368. {
  369. struct memory_descriptor *data =
  370. (struct memory_descriptor *)srp_cmd->additional_data;
  371. data->virtual_address =
  372. dma_map_single(dev, cmd->request_buffer,
  373. cmd->request_bufflen,
  374. DMA_BIDIRECTIONAL);
  375. if (dma_mapping_error(data->virtual_address)) {
  376. printk(KERN_ERR
  377. "ibmvscsi: Unable to map request_buffer for command!\n");
  378. return 0;
  379. }
  380. data->length = cmd->request_bufflen;
  381. data->memory_handle = 0;
  382. set_srp_direction(cmd, srp_cmd, 1);
  383. return 1;
  384. }
  385. /**
  386. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  387. * @cmd: struct scsi_cmnd with the memory to be mapped
  388. * @srp_cmd: srp_cmd that contains the memory descriptor
  389. * @dev: dma device for which to map dma memory
  390. *
  391. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  392. * Returns 1 on success.
  393. */
  394. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  395. struct srp_cmd *srp_cmd, struct device *dev)
  396. {
  397. switch (cmd->sc_data_direction) {
  398. case DMA_FROM_DEVICE:
  399. case DMA_TO_DEVICE:
  400. break;
  401. case DMA_NONE:
  402. return 1;
  403. case DMA_BIDIRECTIONAL:
  404. printk(KERN_ERR
  405. "ibmvscsi: Can't map DMA_BIDIRECTIONAL to read/write\n");
  406. return 0;
  407. default:
  408. printk(KERN_ERR
  409. "ibmvscsi: Unknown data direction 0x%02x; can't map!\n",
  410. cmd->sc_data_direction);
  411. return 0;
  412. }
  413. if (!cmd->request_buffer)
  414. return 1;
  415. if (cmd->use_sg)
  416. return map_sg_data(cmd, srp_cmd, dev);
  417. return map_single_data(cmd, srp_cmd, dev);
  418. }
  419. /* ------------------------------------------------------------
  420. * Routines for sending and receiving SRPs
  421. */
  422. /**
  423. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  424. * @evt_struct: evt_struct to be sent
  425. * @hostdata: ibmvscsi_host_data of host
  426. *
  427. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  428. * Note that this routine assumes that host_lock is held for synchronization
  429. */
  430. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  431. struct ibmvscsi_host_data *hostdata)
  432. {
  433. struct scsi_cmnd *cmnd;
  434. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  435. int rc;
  436. /* If we have exhausted our request limit, just fail this request.
  437. * Note that there are rare cases involving driver generated requests
  438. * (such as task management requests) that the mid layer may think we
  439. * can handle more requests (can_queue) when we actually can't
  440. */
  441. if ((evt_struct->crq.format == VIOSRP_SRP_FORMAT) &&
  442. (atomic_dec_if_positive(&hostdata->request_limit) < 0)) {
  443. /* See if the adapter is disabled */
  444. if (atomic_read(&hostdata->request_limit) < 0)
  445. goto send_error;
  446. printk(KERN_WARNING
  447. "ibmvscsi: Warning, request_limit exceeded\n");
  448. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  449. hostdata->dev);
  450. free_event_struct(&hostdata->pool, evt_struct);
  451. return SCSI_MLQUEUE_HOST_BUSY;
  452. }
  453. /* Copy the IU into the transfer area */
  454. *evt_struct->xfer_iu = evt_struct->iu;
  455. evt_struct->xfer_iu->srp.generic.tag = (u64)evt_struct;
  456. /* Add this to the sent list. We need to do this
  457. * before we actually send
  458. * in case it comes back REALLY fast
  459. */
  460. list_add_tail(&evt_struct->list, &hostdata->sent);
  461. if ((rc =
  462. ibmvscsi_send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  463. list_del(&evt_struct->list);
  464. printk(KERN_ERR "ibmvscsi: failed to send event struct rc %d\n",
  465. rc);
  466. goto send_error;
  467. }
  468. return 0;
  469. send_error:
  470. unmap_cmd_data(&evt_struct->iu.srp.cmd, hostdata->dev);
  471. if ((cmnd = evt_struct->cmnd) != NULL) {
  472. cmnd->result = DID_ERROR << 16;
  473. evt_struct->cmnd_done(cmnd);
  474. } else if (evt_struct->done)
  475. evt_struct->done(evt_struct);
  476. free_event_struct(&hostdata->pool, evt_struct);
  477. return 0;
  478. }
  479. /**
  480. * handle_cmd_rsp: - Handle responses from commands
  481. * @evt_struct: srp_event_struct to be handled
  482. *
  483. * Used as a callback by when sending scsi cmds.
  484. * Gets called by ibmvscsi_handle_crq()
  485. */
  486. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  487. {
  488. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  489. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  490. if (unlikely(rsp->type != SRP_RSP_TYPE)) {
  491. if (printk_ratelimit())
  492. printk(KERN_WARNING
  493. "ibmvscsi: bad SRP RSP type %d\n",
  494. rsp->type);
  495. }
  496. if (cmnd) {
  497. cmnd->result = rsp->status;
  498. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  499. memcpy(cmnd->sense_buffer,
  500. rsp->sense_and_response_data,
  501. rsp->sense_data_list_length);
  502. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  503. evt_struct->hostdata->dev);
  504. if (rsp->doover)
  505. cmnd->resid = rsp->data_out_residual_count;
  506. else if (rsp->diover)
  507. cmnd->resid = rsp->data_in_residual_count;
  508. }
  509. if (evt_struct->cmnd_done)
  510. evt_struct->cmnd_done(cmnd);
  511. }
  512. /**
  513. * lun_from_dev: - Returns the lun of the scsi device
  514. * @dev: struct scsi_device
  515. *
  516. */
  517. static inline u16 lun_from_dev(struct scsi_device *dev)
  518. {
  519. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  520. }
  521. /**
  522. * ibmvscsi_queue: - The queuecommand function of the scsi template
  523. * @cmd: struct scsi_cmnd to be executed
  524. * @done: Callback function to be called when cmd is completed
  525. */
  526. static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
  527. void (*done) (struct scsi_cmnd *))
  528. {
  529. struct srp_cmd *srp_cmd;
  530. struct srp_event_struct *evt_struct;
  531. struct ibmvscsi_host_data *hostdata =
  532. (struct ibmvscsi_host_data *)&cmnd->device->host->hostdata;
  533. u16 lun = lun_from_dev(cmnd->device);
  534. evt_struct = get_event_struct(&hostdata->pool);
  535. if (!evt_struct)
  536. return SCSI_MLQUEUE_HOST_BUSY;
  537. init_event_struct(evt_struct,
  538. handle_cmd_rsp,
  539. VIOSRP_SRP_FORMAT,
  540. cmnd->timeout);
  541. evt_struct->cmnd = cmnd;
  542. evt_struct->cmnd_done = done;
  543. /* Set up the actual SRP IU */
  544. srp_cmd = &evt_struct->iu.srp.cmd;
  545. memset(srp_cmd, 0x00, sizeof(*srp_cmd));
  546. srp_cmd->type = SRP_CMD_TYPE;
  547. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(cmnd->cmnd));
  548. srp_cmd->lun = ((u64) lun) << 48;
  549. if (!map_data_for_srp_cmd(cmnd, srp_cmd, hostdata->dev)) {
  550. printk(KERN_ERR "ibmvscsi: couldn't convert cmd to srp_cmd\n");
  551. free_event_struct(&hostdata->pool, evt_struct);
  552. return SCSI_MLQUEUE_HOST_BUSY;
  553. }
  554. /* Fix up dma address of the buffer itself */
  555. if ((srp_cmd->data_out_format == SRP_INDIRECT_BUFFER) ||
  556. (srp_cmd->data_in_format == SRP_INDIRECT_BUFFER)) {
  557. struct indirect_descriptor *indirect =
  558. (struct indirect_descriptor *)srp_cmd->additional_data;
  559. indirect->head.virtual_address = evt_struct->crq.IU_data_ptr +
  560. offsetof(struct srp_cmd, additional_data) +
  561. offsetof(struct indirect_descriptor, list);
  562. }
  563. return ibmvscsi_send_srp_event(evt_struct, hostdata);
  564. }
  565. /* ------------------------------------------------------------
  566. * Routines for driver initialization
  567. */
  568. /**
  569. * adapter_info_rsp: - Handle response to MAD adapter info request
  570. * @evt_struct: srp_event_struct with the response
  571. *
  572. * Used as a "done" callback by when sending adapter_info. Gets called
  573. * by ibmvscsi_handle_crq()
  574. */
  575. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  576. {
  577. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  578. dma_unmap_single(hostdata->dev,
  579. evt_struct->iu.mad.adapter_info.buffer,
  580. evt_struct->iu.mad.adapter_info.common.length,
  581. DMA_BIDIRECTIONAL);
  582. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  583. printk("ibmvscsi: error %d getting adapter info\n",
  584. evt_struct->xfer_iu->mad.adapter_info.common.status);
  585. } else {
  586. printk("ibmvscsi: host srp version: %s, "
  587. "host partition %s (%d), OS %d, max io %u\n",
  588. hostdata->madapter_info.srp_version,
  589. hostdata->madapter_info.partition_name,
  590. hostdata->madapter_info.partition_number,
  591. hostdata->madapter_info.os_type,
  592. hostdata->madapter_info.port_max_txu[0]);
  593. if (hostdata->madapter_info.port_max_txu[0])
  594. hostdata->host->max_sectors =
  595. hostdata->madapter_info.port_max_txu[0] >> 9;
  596. }
  597. }
  598. /**
  599. * send_mad_adapter_info: - Sends the mad adapter info request
  600. * and stores the result so it can be retrieved with
  601. * sysfs. We COULD consider causing a failure if the
  602. * returned SRP version doesn't match ours.
  603. * @hostdata: ibmvscsi_host_data of host
  604. *
  605. * Returns zero if successful.
  606. */
  607. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  608. {
  609. struct viosrp_adapter_info *req;
  610. struct srp_event_struct *evt_struct;
  611. memset(&hostdata->madapter_info, 0x00, sizeof(hostdata->madapter_info));
  612. evt_struct = get_event_struct(&hostdata->pool);
  613. if (!evt_struct) {
  614. printk(KERN_ERR "ibmvscsi: couldn't allocate an event "
  615. "for ADAPTER_INFO_REQ!\n");
  616. return;
  617. }
  618. init_event_struct(evt_struct,
  619. adapter_info_rsp,
  620. VIOSRP_MAD_FORMAT,
  621. init_timeout * HZ);
  622. req = &evt_struct->iu.mad.adapter_info;
  623. memset(req, 0x00, sizeof(*req));
  624. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  625. req->common.length = sizeof(hostdata->madapter_info);
  626. req->buffer = dma_map_single(hostdata->dev,
  627. &hostdata->madapter_info,
  628. sizeof(hostdata->madapter_info),
  629. DMA_BIDIRECTIONAL);
  630. if (dma_mapping_error(req->buffer)) {
  631. printk(KERN_ERR
  632. "ibmvscsi: Unable to map request_buffer "
  633. "for adapter_info!\n");
  634. free_event_struct(&hostdata->pool, evt_struct);
  635. return;
  636. }
  637. if (ibmvscsi_send_srp_event(evt_struct, hostdata))
  638. printk(KERN_ERR "ibmvscsi: couldn't send ADAPTER_INFO_REQ!\n");
  639. };
  640. /**
  641. * login_rsp: - Handle response to SRP login request
  642. * @evt_struct: srp_event_struct with the response
  643. *
  644. * Used as a "done" callback by when sending srp_login. Gets called
  645. * by ibmvscsi_handle_crq()
  646. */
  647. static void login_rsp(struct srp_event_struct *evt_struct)
  648. {
  649. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  650. switch (evt_struct->xfer_iu->srp.generic.type) {
  651. case SRP_LOGIN_RSP_TYPE: /* it worked! */
  652. break;
  653. case SRP_LOGIN_REJ_TYPE: /* refused! */
  654. printk(KERN_INFO "ibmvscsi: SRP_LOGIN_REQ rejected\n");
  655. /* Login failed. */
  656. atomic_set(&hostdata->request_limit, -1);
  657. return;
  658. default:
  659. printk(KERN_ERR
  660. "ibmvscsi: Invalid login response typecode 0x%02x!\n",
  661. evt_struct->xfer_iu->srp.generic.type);
  662. /* Login failed. */
  663. atomic_set(&hostdata->request_limit, -1);
  664. return;
  665. }
  666. printk(KERN_INFO "ibmvscsi: SRP_LOGIN succeeded\n");
  667. if (evt_struct->xfer_iu->srp.login_rsp.request_limit_delta >
  668. (max_requests - 2))
  669. evt_struct->xfer_iu->srp.login_rsp.request_limit_delta =
  670. max_requests - 2;
  671. /* Now we know what the real request-limit is */
  672. atomic_set(&hostdata->request_limit,
  673. evt_struct->xfer_iu->srp.login_rsp.request_limit_delta);
  674. hostdata->host->can_queue =
  675. evt_struct->xfer_iu->srp.login_rsp.request_limit_delta - 2;
  676. if (hostdata->host->can_queue < 1) {
  677. printk(KERN_ERR "ibmvscsi: Invalid request_limit_delta\n");
  678. return;
  679. }
  680. send_mad_adapter_info(hostdata);
  681. return;
  682. }
  683. /**
  684. * send_srp_login: - Sends the srp login
  685. * @hostdata: ibmvscsi_host_data of host
  686. *
  687. * Returns zero if successful.
  688. */
  689. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  690. {
  691. int rc;
  692. unsigned long flags;
  693. struct srp_login_req *login;
  694. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  695. if (!evt_struct) {
  696. printk(KERN_ERR
  697. "ibmvscsi: couldn't allocate an event for login req!\n");
  698. return FAILED;
  699. }
  700. init_event_struct(evt_struct,
  701. login_rsp,
  702. VIOSRP_SRP_FORMAT,
  703. init_timeout * HZ);
  704. login = &evt_struct->iu.srp.login_req;
  705. login->type = SRP_LOGIN_REQ_TYPE;
  706. login->max_requested_initiator_to_target_iulen = sizeof(union srp_iu);
  707. login->required_buffer_formats = 0x0006;
  708. /* Start out with a request limit of 1, since this is negotiated in
  709. * the login request we are just sending
  710. */
  711. atomic_set(&hostdata->request_limit, 1);
  712. spin_lock_irqsave(hostdata->host->host_lock, flags);
  713. rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
  714. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  715. return rc;
  716. };
  717. /**
  718. * sync_completion: Signal that a synchronous command has completed
  719. * Note that after returning from this call, the evt_struct is freed.
  720. * the caller waiting on this completion shouldn't touch the evt_struct
  721. * again.
  722. */
  723. static void sync_completion(struct srp_event_struct *evt_struct)
  724. {
  725. /* copy the response back */
  726. if (evt_struct->sync_srp)
  727. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  728. complete(&evt_struct->comp);
  729. }
  730. /**
  731. * ibmvscsi_abort: Abort a command...from scsi host template
  732. * send this over to the server and wait synchronously for the response
  733. */
  734. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  735. {
  736. struct ibmvscsi_host_data *hostdata =
  737. (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
  738. struct srp_tsk_mgmt *tsk_mgmt;
  739. struct srp_event_struct *evt;
  740. struct srp_event_struct *tmp_evt, *found_evt;
  741. union viosrp_iu srp_rsp;
  742. int rsp_rc;
  743. u16 lun = lun_from_dev(cmd->device);
  744. /* First, find this command in our sent list so we can figure
  745. * out the correct tag
  746. */
  747. found_evt = NULL;
  748. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  749. if (tmp_evt->cmnd == cmd) {
  750. found_evt = tmp_evt;
  751. break;
  752. }
  753. }
  754. if (!found_evt)
  755. return FAILED;
  756. evt = get_event_struct(&hostdata->pool);
  757. if (evt == NULL) {
  758. printk(KERN_ERR "ibmvscsi: failed to allocate abort event\n");
  759. return FAILED;
  760. }
  761. init_event_struct(evt,
  762. sync_completion,
  763. VIOSRP_SRP_FORMAT,
  764. init_timeout * HZ);
  765. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  766. /* Set up an abort SRP command */
  767. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  768. tsk_mgmt->type = SRP_TSK_MGMT_TYPE;
  769. tsk_mgmt->lun = ((u64) lun) << 48;
  770. tsk_mgmt->task_mgmt_flags = 0x01; /* ABORT TASK */
  771. tsk_mgmt->managed_task_tag = (u64) found_evt;
  772. printk(KERN_INFO "ibmvscsi: aborting command. lun 0x%lx, tag 0x%lx\n",
  773. tsk_mgmt->lun, tsk_mgmt->managed_task_tag);
  774. evt->sync_srp = &srp_rsp;
  775. init_completion(&evt->comp);
  776. if (ibmvscsi_send_srp_event(evt, hostdata) != 0) {
  777. printk(KERN_ERR "ibmvscsi: failed to send abort() event\n");
  778. return FAILED;
  779. }
  780. wait_for_completion(&evt->comp);
  781. /* make sure we got a good response */
  782. if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
  783. if (printk_ratelimit())
  784. printk(KERN_WARNING
  785. "ibmvscsi: abort bad SRP RSP type %d\n",
  786. srp_rsp.srp.generic.type);
  787. return FAILED;
  788. }
  789. if (srp_rsp.srp.rsp.rspvalid)
  790. rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
  791. else
  792. rsp_rc = srp_rsp.srp.rsp.status;
  793. if (rsp_rc) {
  794. if (printk_ratelimit())
  795. printk(KERN_WARNING
  796. "ibmvscsi: abort code %d for task tag 0x%lx\n",
  797. rsp_rc,
  798. tsk_mgmt->managed_task_tag);
  799. return FAILED;
  800. }
  801. /* Because we dropped the spinlock above, it's possible
  802. * The event is no longer in our list. Make sure it didn't
  803. * complete while we were aborting
  804. */
  805. found_evt = NULL;
  806. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  807. if (tmp_evt->cmnd == cmd) {
  808. found_evt = tmp_evt;
  809. break;
  810. }
  811. }
  812. if (found_evt == NULL) {
  813. printk(KERN_INFO
  814. "ibmvscsi: aborted task tag 0x%lx completed\n",
  815. tsk_mgmt->managed_task_tag);
  816. return SUCCESS;
  817. }
  818. printk(KERN_INFO
  819. "ibmvscsi: successfully aborted task tag 0x%lx\n",
  820. tsk_mgmt->managed_task_tag);
  821. cmd->result = (DID_ABORT << 16);
  822. list_del(&found_evt->list);
  823. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt->hostdata->dev);
  824. free_event_struct(&found_evt->hostdata->pool, found_evt);
  825. atomic_inc(&hostdata->request_limit);
  826. return SUCCESS;
  827. }
  828. /**
  829. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  830. * template send this over to the server and wait synchronously for the
  831. * response
  832. */
  833. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  834. {
  835. struct ibmvscsi_host_data *hostdata =
  836. (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
  837. struct srp_tsk_mgmt *tsk_mgmt;
  838. struct srp_event_struct *evt;
  839. struct srp_event_struct *tmp_evt, *pos;
  840. union viosrp_iu srp_rsp;
  841. int rsp_rc;
  842. u16 lun = lun_from_dev(cmd->device);
  843. evt = get_event_struct(&hostdata->pool);
  844. if (evt == NULL) {
  845. printk(KERN_ERR "ibmvscsi: failed to allocate reset event\n");
  846. return FAILED;
  847. }
  848. init_event_struct(evt,
  849. sync_completion,
  850. VIOSRP_SRP_FORMAT,
  851. init_timeout * HZ);
  852. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  853. /* Set up a lun reset SRP command */
  854. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  855. tsk_mgmt->type = SRP_TSK_MGMT_TYPE;
  856. tsk_mgmt->lun = ((u64) lun) << 48;
  857. tsk_mgmt->task_mgmt_flags = 0x08; /* LUN RESET */
  858. printk(KERN_INFO "ibmvscsi: resetting device. lun 0x%lx\n",
  859. tsk_mgmt->lun);
  860. evt->sync_srp = &srp_rsp;
  861. init_completion(&evt->comp);
  862. if (ibmvscsi_send_srp_event(evt, hostdata) != 0) {
  863. printk(KERN_ERR "ibmvscsi: failed to send reset event\n");
  864. return FAILED;
  865. }
  866. spin_unlock_irq(hostdata->host->host_lock);
  867. wait_for_completion(&evt->comp);
  868. spin_lock_irq(hostdata->host->host_lock);
  869. /* make sure we got a good response */
  870. if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
  871. if (printk_ratelimit())
  872. printk(KERN_WARNING
  873. "ibmvscsi: reset bad SRP RSP type %d\n",
  874. srp_rsp.srp.generic.type);
  875. return FAILED;
  876. }
  877. if (srp_rsp.srp.rsp.rspvalid)
  878. rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
  879. else
  880. rsp_rc = srp_rsp.srp.rsp.status;
  881. if (rsp_rc) {
  882. if (printk_ratelimit())
  883. printk(KERN_WARNING
  884. "ibmvscsi: reset code %d for task tag 0x%lx\n",
  885. rsp_rc,
  886. tsk_mgmt->managed_task_tag);
  887. return FAILED;
  888. }
  889. /* We need to find all commands for this LUN that have not yet been
  890. * responded to, and fail them with DID_RESET
  891. */
  892. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  893. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  894. if (tmp_evt->cmnd)
  895. tmp_evt->cmnd->result = (DID_RESET << 16);
  896. list_del(&tmp_evt->list);
  897. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt->hostdata->dev);
  898. free_event_struct(&tmp_evt->hostdata->pool,
  899. tmp_evt);
  900. atomic_inc(&hostdata->request_limit);
  901. if (tmp_evt->cmnd_done)
  902. tmp_evt->cmnd_done(tmp_evt->cmnd);
  903. else if (tmp_evt->done)
  904. tmp_evt->done(tmp_evt);
  905. }
  906. }
  907. return SUCCESS;
  908. }
  909. /**
  910. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  911. * @hostdata: the adapter
  912. */
  913. static void purge_requests(struct ibmvscsi_host_data *hostdata)
  914. {
  915. struct srp_event_struct *tmp_evt, *pos;
  916. unsigned long flags;
  917. spin_lock_irqsave(hostdata->host->host_lock, flags);
  918. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  919. list_del(&tmp_evt->list);
  920. if (tmp_evt->cmnd) {
  921. tmp_evt->cmnd->result = (DID_ERROR << 16);
  922. unmap_cmd_data(&tmp_evt->iu.srp.cmd,
  923. tmp_evt->hostdata->dev);
  924. if (tmp_evt->cmnd_done)
  925. tmp_evt->cmnd_done(tmp_evt->cmnd);
  926. } else {
  927. if (tmp_evt->done) {
  928. tmp_evt->done(tmp_evt);
  929. }
  930. }
  931. free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
  932. }
  933. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  934. }
  935. /**
  936. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  937. * @crq: Command/Response queue
  938. * @hostdata: ibmvscsi_host_data of host
  939. *
  940. */
  941. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  942. struct ibmvscsi_host_data *hostdata)
  943. {
  944. unsigned long flags;
  945. struct srp_event_struct *evt_struct =
  946. (struct srp_event_struct *)crq->IU_data_ptr;
  947. switch (crq->valid) {
  948. case 0xC0: /* initialization */
  949. switch (crq->format) {
  950. case 0x01: /* Initialization message */
  951. printk(KERN_INFO "ibmvscsi: partner initialized\n");
  952. /* Send back a response */
  953. if (ibmvscsi_send_crq(hostdata,
  954. 0xC002000000000000LL, 0) == 0) {
  955. /* Now login */
  956. send_srp_login(hostdata);
  957. } else {
  958. printk(KERN_ERR
  959. "ibmvscsi: Unable to send init rsp\n");
  960. }
  961. break;
  962. case 0x02: /* Initialization response */
  963. printk(KERN_INFO
  964. "ibmvscsi: partner initialization complete\n");
  965. /* Now login */
  966. send_srp_login(hostdata);
  967. break;
  968. default:
  969. printk(KERN_ERR "ibmvscsi: unknown crq message type\n");
  970. }
  971. return;
  972. case 0xFF: /* Hypervisor telling us the connection is closed */
  973. printk(KERN_INFO "ibmvscsi: Virtual adapter failed!\n");
  974. atomic_set(&hostdata->request_limit, -1);
  975. purge_requests(hostdata);
  976. ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
  977. return;
  978. case 0x80: /* real payload */
  979. break;
  980. default:
  981. printk(KERN_ERR
  982. "ibmvscsi: got an invalid message type 0x%02x\n",
  983. crq->valid);
  984. return;
  985. }
  986. /* The only kind of payload CRQs we should get are responses to
  987. * things we send. Make sure this response is to something we
  988. * actually sent
  989. */
  990. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  991. printk(KERN_ERR
  992. "ibmvscsi: returned correlation_token 0x%p is invalid!\n",
  993. (void *)crq->IU_data_ptr);
  994. return;
  995. }
  996. if (atomic_read(&evt_struct->free)) {
  997. printk(KERN_ERR
  998. "ibmvscsi: received duplicate correlation_token 0x%p!\n",
  999. (void *)crq->IU_data_ptr);
  1000. return;
  1001. }
  1002. if (crq->format == VIOSRP_SRP_FORMAT)
  1003. atomic_add(evt_struct->xfer_iu->srp.rsp.request_limit_delta,
  1004. &hostdata->request_limit);
  1005. if (evt_struct->done)
  1006. evt_struct->done(evt_struct);
  1007. else
  1008. printk(KERN_ERR
  1009. "ibmvscsi: returned done() is NULL; not running it!\n");
  1010. /*
  1011. * Lock the host_lock before messing with these structures, since we
  1012. * are running in a task context
  1013. */
  1014. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1015. list_del(&evt_struct->list);
  1016. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1017. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1018. }
  1019. /**
  1020. * ibmvscsi_get_host_config: Send the command to the server to get host
  1021. * configuration data. The data is opaque to us.
  1022. */
  1023. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1024. unsigned char *buffer, int length)
  1025. {
  1026. struct viosrp_host_config *host_config;
  1027. struct srp_event_struct *evt_struct;
  1028. int rc;
  1029. evt_struct = get_event_struct(&hostdata->pool);
  1030. if (!evt_struct) {
  1031. printk(KERN_ERR
  1032. "ibmvscsi: could't allocate event for HOST_CONFIG!\n");
  1033. return -1;
  1034. }
  1035. init_event_struct(evt_struct,
  1036. sync_completion,
  1037. VIOSRP_MAD_FORMAT,
  1038. init_timeout * HZ);
  1039. host_config = &evt_struct->iu.mad.host_config;
  1040. /* Set up a lun reset SRP command */
  1041. memset(host_config, 0x00, sizeof(*host_config));
  1042. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1043. host_config->common.length = length;
  1044. host_config->buffer = dma_map_single(hostdata->dev, buffer, length,
  1045. DMA_BIDIRECTIONAL);
  1046. if (dma_mapping_error(host_config->buffer)) {
  1047. printk(KERN_ERR
  1048. "ibmvscsi: dma_mapping error " "getting host config\n");
  1049. free_event_struct(&hostdata->pool, evt_struct);
  1050. return -1;
  1051. }
  1052. init_completion(&evt_struct->comp);
  1053. rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
  1054. if (rc == 0) {
  1055. wait_for_completion(&evt_struct->comp);
  1056. dma_unmap_single(hostdata->dev, host_config->buffer,
  1057. length, DMA_BIDIRECTIONAL);
  1058. }
  1059. return rc;
  1060. }
  1061. /* ------------------------------------------------------------
  1062. * sysfs attributes
  1063. */
  1064. static ssize_t show_host_srp_version(struct class_device *class_dev, char *buf)
  1065. {
  1066. struct Scsi_Host *shost = class_to_shost(class_dev);
  1067. struct ibmvscsi_host_data *hostdata =
  1068. (struct ibmvscsi_host_data *)shost->hostdata;
  1069. int len;
  1070. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1071. hostdata->madapter_info.srp_version);
  1072. return len;
  1073. }
  1074. static struct class_device_attribute ibmvscsi_host_srp_version = {
  1075. .attr = {
  1076. .name = "srp_version",
  1077. .mode = S_IRUGO,
  1078. },
  1079. .show = show_host_srp_version,
  1080. };
  1081. static ssize_t show_host_partition_name(struct class_device *class_dev,
  1082. char *buf)
  1083. {
  1084. struct Scsi_Host *shost = class_to_shost(class_dev);
  1085. struct ibmvscsi_host_data *hostdata =
  1086. (struct ibmvscsi_host_data *)shost->hostdata;
  1087. int len;
  1088. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1089. hostdata->madapter_info.partition_name);
  1090. return len;
  1091. }
  1092. static struct class_device_attribute ibmvscsi_host_partition_name = {
  1093. .attr = {
  1094. .name = "partition_name",
  1095. .mode = S_IRUGO,
  1096. },
  1097. .show = show_host_partition_name,
  1098. };
  1099. static ssize_t show_host_partition_number(struct class_device *class_dev,
  1100. char *buf)
  1101. {
  1102. struct Scsi_Host *shost = class_to_shost(class_dev);
  1103. struct ibmvscsi_host_data *hostdata =
  1104. (struct ibmvscsi_host_data *)shost->hostdata;
  1105. int len;
  1106. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1107. hostdata->madapter_info.partition_number);
  1108. return len;
  1109. }
  1110. static struct class_device_attribute ibmvscsi_host_partition_number = {
  1111. .attr = {
  1112. .name = "partition_number",
  1113. .mode = S_IRUGO,
  1114. },
  1115. .show = show_host_partition_number,
  1116. };
  1117. static ssize_t show_host_mad_version(struct class_device *class_dev, char *buf)
  1118. {
  1119. struct Scsi_Host *shost = class_to_shost(class_dev);
  1120. struct ibmvscsi_host_data *hostdata =
  1121. (struct ibmvscsi_host_data *)shost->hostdata;
  1122. int len;
  1123. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1124. hostdata->madapter_info.mad_version);
  1125. return len;
  1126. }
  1127. static struct class_device_attribute ibmvscsi_host_mad_version = {
  1128. .attr = {
  1129. .name = "mad_version",
  1130. .mode = S_IRUGO,
  1131. },
  1132. .show = show_host_mad_version,
  1133. };
  1134. static ssize_t show_host_os_type(struct class_device *class_dev, char *buf)
  1135. {
  1136. struct Scsi_Host *shost = class_to_shost(class_dev);
  1137. struct ibmvscsi_host_data *hostdata =
  1138. (struct ibmvscsi_host_data *)shost->hostdata;
  1139. int len;
  1140. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1141. return len;
  1142. }
  1143. static struct class_device_attribute ibmvscsi_host_os_type = {
  1144. .attr = {
  1145. .name = "os_type",
  1146. .mode = S_IRUGO,
  1147. },
  1148. .show = show_host_os_type,
  1149. };
  1150. static ssize_t show_host_config(struct class_device *class_dev, char *buf)
  1151. {
  1152. struct Scsi_Host *shost = class_to_shost(class_dev);
  1153. struct ibmvscsi_host_data *hostdata =
  1154. (struct ibmvscsi_host_data *)shost->hostdata;
  1155. /* returns null-terminated host config data */
  1156. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1157. return strlen(buf);
  1158. else
  1159. return 0;
  1160. }
  1161. static struct class_device_attribute ibmvscsi_host_config = {
  1162. .attr = {
  1163. .name = "config",
  1164. .mode = S_IRUGO,
  1165. },
  1166. .show = show_host_config,
  1167. };
  1168. static struct class_device_attribute *ibmvscsi_attrs[] = {
  1169. &ibmvscsi_host_srp_version,
  1170. &ibmvscsi_host_partition_name,
  1171. &ibmvscsi_host_partition_number,
  1172. &ibmvscsi_host_mad_version,
  1173. &ibmvscsi_host_os_type,
  1174. &ibmvscsi_host_config,
  1175. NULL
  1176. };
  1177. /* ------------------------------------------------------------
  1178. * SCSI driver registration
  1179. */
  1180. static struct scsi_host_template driver_template = {
  1181. .module = THIS_MODULE,
  1182. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1183. .proc_name = "ibmvscsi",
  1184. .queuecommand = ibmvscsi_queuecommand,
  1185. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1186. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1187. .cmd_per_lun = 16,
  1188. .can_queue = 1, /* Updated after SRP_LOGIN */
  1189. .this_id = -1,
  1190. .sg_tablesize = MAX_INDIRECT_BUFS,
  1191. .use_clustering = ENABLE_CLUSTERING,
  1192. .shost_attrs = ibmvscsi_attrs,
  1193. };
  1194. /**
  1195. * Called by bus code for each adapter
  1196. */
  1197. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1198. {
  1199. struct ibmvscsi_host_data *hostdata;
  1200. struct Scsi_Host *host;
  1201. struct device *dev = &vdev->dev;
  1202. unsigned long wait_switch = 0;
  1203. vdev->dev.driver_data = NULL;
  1204. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1205. if (!host) {
  1206. printk(KERN_ERR "ibmvscsi: couldn't allocate host data\n");
  1207. goto scsi_host_alloc_failed;
  1208. }
  1209. hostdata = (struct ibmvscsi_host_data *)host->hostdata;
  1210. memset(hostdata, 0x00, sizeof(*hostdata));
  1211. INIT_LIST_HEAD(&hostdata->sent);
  1212. hostdata->host = host;
  1213. hostdata->dev = dev;
  1214. atomic_set(&hostdata->request_limit, -1);
  1215. hostdata->host->max_sectors = 32 * 8; /* default max I/O 32 pages */
  1216. if (ibmvscsi_init_crq_queue(&hostdata->queue, hostdata,
  1217. max_requests) != 0) {
  1218. printk(KERN_ERR "ibmvscsi: couldn't initialize crq\n");
  1219. goto init_crq_failed;
  1220. }
  1221. if (initialize_event_pool(&hostdata->pool, max_requests, hostdata) != 0) {
  1222. printk(KERN_ERR "ibmvscsi: couldn't initialize event pool\n");
  1223. goto init_pool_failed;
  1224. }
  1225. host->max_lun = 8;
  1226. host->max_id = max_id;
  1227. host->max_channel = max_channel;
  1228. if (scsi_add_host(hostdata->host, hostdata->dev))
  1229. goto add_host_failed;
  1230. /* Try to send an initialization message. Note that this is allowed
  1231. * to fail if the other end is not acive. In that case we don't
  1232. * want to scan
  1233. */
  1234. if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0) {
  1235. /*
  1236. * Wait around max init_timeout secs for the adapter to finish
  1237. * initializing. When we are done initializing, we will have a
  1238. * valid request_limit. We don't want Linux scanning before
  1239. * we are ready.
  1240. */
  1241. for (wait_switch = jiffies + (init_timeout * HZ);
  1242. time_before(jiffies, wait_switch) &&
  1243. atomic_read(&hostdata->request_limit) < 2;) {
  1244. msleep(10);
  1245. }
  1246. /* if we now have a valid request_limit, initiate a scan */
  1247. if (atomic_read(&hostdata->request_limit) > 0)
  1248. scsi_scan_host(host);
  1249. }
  1250. vdev->dev.driver_data = hostdata;
  1251. return 0;
  1252. add_host_failed:
  1253. release_event_pool(&hostdata->pool, hostdata);
  1254. init_pool_failed:
  1255. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_requests);
  1256. init_crq_failed:
  1257. scsi_host_put(host);
  1258. scsi_host_alloc_failed:
  1259. return -1;
  1260. }
  1261. static int ibmvscsi_remove(struct vio_dev *vdev)
  1262. {
  1263. struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
  1264. release_event_pool(&hostdata->pool, hostdata);
  1265. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
  1266. max_requests);
  1267. scsi_remove_host(hostdata->host);
  1268. scsi_host_put(hostdata->host);
  1269. return 0;
  1270. }
  1271. /**
  1272. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1273. * support.
  1274. */
  1275. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1276. {"vscsi", "IBM,v-scsi"},
  1277. {0,}
  1278. };
  1279. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1280. static struct vio_driver ibmvscsi_driver = {
  1281. .name = "ibmvscsi",
  1282. .id_table = ibmvscsi_device_table,
  1283. .probe = ibmvscsi_probe,
  1284. .remove = ibmvscsi_remove
  1285. };
  1286. int __init ibmvscsi_module_init(void)
  1287. {
  1288. return vio_register_driver(&ibmvscsi_driver);
  1289. }
  1290. void __exit ibmvscsi_module_exit(void)
  1291. {
  1292. vio_unregister_driver(&ibmvscsi_driver);
  1293. }
  1294. module_init(ibmvscsi_module_init);
  1295. module_exit(ibmvscsi_module_exit);