ibmvscsi.c 68 KB

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