ibmvscsi.c 48 KB

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